cant connect user to google cloud sql database - php

I am attempting to connect to a database in an instance on Google cloud sql but it is telling me access is denied.
Im just wondering am I missing something here...
I have requested an IP and inserted that into the authorized networks
I have created a database and set a user using SQL prompt (found in original view of Google APIs Console) and granted all privileges to that user for that database.
I have listed the users in that database to make sure "adrian" exists and that I have done that correctly.
I have insured the app being used to connect is authorized in the access control
I have set a root password in the access control section.
Here is my app.yaml file...
application: guestbookadrian12
version: 1
runtime: php
api_version: 1
handlers:
- url: .*
script: main.php
env_variables:
DEVELOPMENT_DB_HOST: ''
DEVELOPMENT_DB_USERNAME: 'root'
DEVELOPMENT_DB_PASSWORD: 'password'
DEVELOPMENT_DB_NAME: 'guestbookadrian12'
PRODUCTION_CLOUD_SQL_INSTANCE: '/cloudsql/guestbookadrian12:my-cloudsql-instance'
PRODUCTION_DB_USERNAME: 'adrian'
PRODUCTION_DB_PASSWORD: 'password'
PRODUCTION_DB_NAME: 'guestbookadrian12'
Here is some php code where it creates the connection...
if (strpos(getenv('SERVER_SOFTWARE'), 'Development') === false) {
$conn = mysqli_connect(null,
getenv('PRODUCTION_DB_USERNAME'),
getenv('PRODUCTION_DB_PASSWORD'),
null,
null,
getenv('PRODUCTION_CLOUD_SQL_INSTANCE'));
$db = getenv('PRODUCTION_DB_NAME');
//echo 'Database = : '.$db;
} else {
$conn = mysqli_connect(getenv('DEVELOPMENT_DB_HOST'),
getenv('DEVELOPMENT_DB_USERNAME'),
getenv('DEVELOPMENT_DB_PASSWORD'));
$db = getenv('DEVELOPMENT_DB_NAME');
}
if ($conn->connect_error) {
die("Could not connect to database: $conn->connect_error " .
"[$conn->connect_errno]");
}
if ($conn->query("CREATE DATABASE IF NOT EXISTS $db") === FALSE) {
die("Could not create database: $conn->error [$conn->errno]");
}
The error Im getting is
Could not create database: Access denied for user 'adrian'#'localhost' to database 'guestbookadrian12' [1044]
Thank you

Related

App Engine (PHP) connection to Cloud SQL failed

I used the environment of the "hello world" app engine example by google to create my own php app. Then i configured the existing app.yaml file to this:
runtime: php55
api_version: 1
handlers:
- url: /
script: index.php
env_variables:
MYSQL_DSN: mysql:dbname=sampledb;unix_socket=/cloudsql/sampleproject:europe-west3:sampleinstance
MYSQL_USER: root
MYSQL_PASSWORD: admin
My index.php is a typical login page. I want to load user data from my cloud sql to check, whether the user is allowed to enter the next page or not. The next page is a dashboard which includes company data. I also want to load this data from my cloud sql databse.
That's my php code to read the data (i copied it from the google bookshelf tutorial, because i am completly new into these things..)
<?php
$dsn = getenv('MYSQL_DSN');
$user = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
if (!isset($dsn; $user) || false=== $password) {
throw new Exception('Set MYSQL_DSN');
}
$db = new PDO($dsn, $user, $password);
$statement = $db->prepare("SELECT clicks from display_inbox");
$statement->execute();
$all = $statement->fetchAll();
foreach ($all as $data) {
echo $data["clicks"], "<br>";
}
?>
It's not a specific query,i just want to check the connection.
After deploying my app i get the following result:
HTTP-500 error: sampleproject.appspot.com is currently unable to handle this request.
I simply don't get if it's my app.yaml or my php code. Would be very helpful if anybody has an example code for a app.yaml file and a typical query to read the cloud sql data.
You have some syntax errors in your app.yaml and index.php files
Try this:
app.yaml:
runtime: php55
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: index.php
# [START env]
env_variables:
MYSQL_DSN: mysql:unix_socket=/cloudsql/sampleproject:europe-west3:sampleinstance;dbname=sampledb
MYSQL_USER: root
MYSQL_PASSWORD: admin
# [END env]
index.php:
<?php
$dsn = getenv('MYSQL_DSN');
$user = getenv('MYSQL_USER');
$password = getenv('MYSQL_PASSWORD');
if (!isset($dsn, $user) || false === $password) {
throw new Exception('Set MYSQL_DSN');
}
$db = new PDO($dsn, $user, $password);
$statement = $db->prepare("SELECT clicks from display_inbox");
$statement->execute();
$all = $statement->fetchAll();
foreach ($all as $data) {
echo $data["clicks"]."<br>";
}
?>

Php mongodb not able to connect using username and password

I am able to login to mongo using the following command:
mongo host_name:port/admin -u 'USERNAME' -p 'PASSWORD'
For PHP I tried below code to connect to mongo:
try{
$m = new MongoClient("mongodb://USERNAME:PASSWORD#host_name:27017");
echo "Connection to database successfully";
}
catch(Exception $ex)
{
echo '<pre>'; print_r($ex->getMessage());
}
But it is not connecting. Gives following error:
Failed to connect to: 10.7.0.225:27017: Authentication failed on database 'admin' with username 'USERNAME': auth failed
I have already created user root with role as root.
Any help would be appreciable. Thanks!

Connecting phalcon webapp to an AWS RDS mysql database

I am trying to connect my RDS to my phalcon app. I have settings in my security group that allows access to the database from my server and from localhost. Here is some of my code from my index.php file
$config = new Phalcon\Config\Adapter\Ini("../config/config.ini");
$di->set('db', function() use ($config){
try {
$db = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => $config->development->host,
"username" => $config->development->username,
"password" => $config->development->password,
"dbname" => $config->development->dbname
));
} catch (Exception $e) {
die("<b>Error when initializing database connection:</b> " . $e->getMessage());
}
return $db;
});
Then I try a call to my database in one of my controllers here:
public function indexAction()
{
$users = Users::find();
echo "There are ", count($users), "\n";
}
When I run that controller the page times out and throws this error:
Error when initializing database connection: SQLSTATE[HY000] [2002] Operation timed out
The data in the config file is correct. I am able to connect to it just fine via the terminal. What am I doing wrong? Any thoughts/suggestions/insights? Thanks

Trouble with configuring users and connecting to Mongo with PHP

I installed mongo 3.0.0 on my ubuntu server.
I tried to use PHP (with appropriate lib installed) to connect to mongo to learn sth new. Unfortunaltey I cannot go any further.
My user configuration looks like this:
> use testdb
switched to db testdb
> show users
{
"_id" : "testdb.testdb",
"user" : "testdb",
"db" : "testdb",
"roles" : [
{
"role" : "readWrite",
"db" : "testdb"
}
]
}
Then I try to execute the following PHP code:
try{
$uri = "mongodb://testdb:password#xxx.xxx.xxx.xxx:27017/testdb";
$options = array("connectTimeoutMS" => 30000);
$client = new MongoClient($uri, $options );
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
die();
}
$db = $client->selectDB("testdb");
I get "Message: Failed to connect to: xxx.xxx.xxx.xxx:27017: Authentication failed on database 'testdb' with username 'testdb': auth failed".
In /etc/mongod.conf I have "auth = true" uncommented
I also verified the conncetion with:
> nc -w 3 -v xxx.xxx.xxx.xxx 27017 Connection to xxx.xxx.xxx.xxx 27017
> port [tcp/*] succeeded!
I dig through Internet, i spent few hours already on this, I even re-installed mongo and set everything up again without any success.
Could you point to where to look for solution?
The authentication schema of 3.0 is not compatible with 2.x drivers. One way you should be able to make it work is by downgrading the authentication mechanism:
use "admin"
var schema = db.system.version.findOne({"_id" : "authSchema"})
schema.currentVersion = 3
db.system.version.save(schema)
Then create a user and you should be able to connect with it.

how to connect to the database in openshift application

I did as following
MySQL 5.1 database added. Please make note of these credentials:
Root User: xxxxxxx
Root Password: xxxxxxx
Database Name: php
Connection URL: mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/
You can manage your new MySQL database by also embedding phpmyadmin-3.4.
The phpmyadmin username and password will be the same as the MySQL credentials above.
phpMyAdmin 3.4 added. Please make note of these MySQL credentials again:
Root User: xxxxxxx
Root Password: xxxxxxx
URL: https://php-doers.rhcloud.com/phpmyadmin/
and i try to connect db using bellow PDO code .but it does not work
$dbh = new PDO('mysql:host=mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/;dbname=php', "xxxxxx, "xxxxxx");
I don't know what is the connection URL mean ?
There is an error in your connection string plus $OPENSHIFT_MYSQL_DB_* are env variables and need to be fetched via getenv php function.
So try the following:
define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT',getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER',getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS',getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME',getenv('OPENSHIFT_GEAR_NAME'));
$dsn = 'mysql:dbname='.DB_NAME.';host='.DB_HOST.';port='.DB_PORT;
$dbh = new PDO($dsn, DB_USER, DB_PASS);
Open php myadmin from webconsole , and on left corner you will get some Ip
That is like xx.xx.xx.xx:3306 , so your hostname is xx.xx.xx.xx
Ip address wil work , I have tried and worked for me
below code worked for me ,try this it might help you
var mysql = require('mysql');
var mysql = require('mysql');
exports.pool = mysql.createPool({
host: process.env.OPENSHIFT_MYSQL_DB_HOST || 'localhost',
port: process.env.OPENSHIFT_MYSQL_DB_PORT || '3306',
user: process.env.OPENSHIFT_MYSQL_DB_USERNAME || 'root',
password: process.env.OPENSHIFT_MYSQL_DB_PASSWORD || '',
database: 'chichat' || 'chichat',
multipleStatements : true,
charset: 'utf8'
});
I would like to add some information to future reference.
Today I am trying to deploy a Drupal installation to Open Shift (OS) and tried to configure MySql.
I connected to OS via SSH (rhc ssh ), went to app-root/data/sites/default. Following cat settings.php to see how OS configure database.
Here it is:
// When run from Drush, only $_ENV is available. Might be a bug
if (array_key_exists('OPENSHIFT_APP_NAME', $_SERVER)) {
$src = $_SERVER;
} else {
$src = $_ENV;
}
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => $src['OPENSHIFT_APP_NAME'],
'username' => $src['OPENSHIFT_MYSQL_DB_USERNAME'],
'password' => $src['OPENSHIFT_MYSQL_DB_PASSWORD'],
'host' => $src['OPENSHIFT_MYSQL_DB_HOST'],
'port' => $src['OPENSHIFT_MYSQL_DB_PORT'],
'driver' => 'mysql',
'prefix' => '',
),
),
);
$conf['file_private_path'] = $src['OPENSHIFT_DATA_DIR'] . 'private/';
$conf['file_temporary_path'] = $src['OPENSHIFT_TMP_DIR'] . 'drupal/';
So I created a php folder into git repository (see OS documentation about Drupal), copied a clean Drupal install to it and paste code above to settings.php.
After pushing to git, restarted OS app and worked!

Categories