Connect to Cloud SQL in Google App Engine using mysqli_connect - php

I am trying to configure my Wordpress site in the Google App Engine standard environment. I have configured a Cloud SQL for MySQL Second Generation instance and can access it using Cloud SQL Proxy.
The problem I have is connected to the Cloud SQL instance after the app has been deployed to the Google App Engine (GAE) environment. Here are two different connection strings; one for the GAE environment and another for the local environment.
if (isset($_SERVER['GAE_ENV'])) {
$dbConn = mysqli_connect (null, DB_USER, DB_PASSWORD, DB_NAME, 3306, DB_SOCK);
} else { // local environment
$dbConn = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
}
Local environment
The connection string for the local environment works perfectly when I use these 4 parameters: DB_HOST, DB_USER, DB_PASSWORD, DB_NAME. When I try to use the same connection string with four parameters in the GAE environment, it throws an error:
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known
GAE Environment
In this question, I was told to call mysqli_connect using all 6 of the optional parameters, with null for parameter 1 and 3306 for parameter 5, being the port. When I try to make the connection using the GAE connection string with these 6 parameters: null, DB_USER, DB_PASSWORD, DB_NAME, 3306, DB_SOCK, I get a slightly different error:
Fatal error: Uncaught mysqli_sql_exception: No such file or directory
Here is the code from wp-config.php which sets the connection string variables depending on the environment:
if ($onGae) {
/** GAE Environment */
define('DB_NAME', 'database');
define('DB_HOST', ':/cloudsql/project_id:region:instance_id');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
} else {
/** Local environment */
define('DB_NAME', 'database');
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
}
No matter what I try, I can't seem to get mysqli_connect to connect to CloudSQL from GAE. Can someone please tell me what I am doing wrong? Thank you.

I suspect the problem is the colon in the path of your socket is preventing it from recognizing it is an absolute path. Try define('DB_HOST', '/cloudsql/<project_id:region:instance_id>'); instead.
A couple of other tips: You can use the Cloud SQL proxy to create a local unix socket at /cloudsql/<CONNECTION_NAME>, which means you can test the same code deployed and locally.
Also as general programming advice, I would try to avoid using DB_HOST for different types of values (because they really aren't the same). This will help you from inadvertently using $DB_HOST somewhere else and expecting an IP address. Try something like this instead:
define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
if ($onGae) {
/** GAE Environment */
define('DB_HOST', null);
define('DB_SOCKET', '/cloudsql/project_id:region:instance_id');
} else {
/** Local environment */
define('DB_HOST', '127.0.0.1');
define('DB_SOCKET', null);
}

Related

How can I access the localhost/phpmyadmin because all day I got This site can’t be reached?

I am using windows and trying to develop an app in php where I connect a database mySQL created on mySQL workbench. I connect with root and a saved pass. But it seems I cannot connect to the page afterwards.
I use Windows 10.
<?php
define('db_user', 'what is the user?');
define('db_pass', 'mypass');
define('db_host', 'localhost');
define('db_name', 'movies');
&db_conn = mysqli_connect(db_host, db_user, db_pass, db_name);
if(!&db_conn){
die('error connecting to database');
}
echo 'you have connected succesfully';
?>
My database is called movies. I don't know what is the user?
phpMyAdmin is not a database; it is just one of the tools you use to manage what is inside. Your database is MySQL, which should be started and listening on a particular port. The default is usually 3306 or for MariaDB 3307. When MySQL server is located on the same machine as your web server, you can connect via localhost or 127.0.0.1.
If you haven't set up any users in you MySQL server, then the default usually is root with no password. Check your users using this command:
SELECT * FROM mysql.user;
The user must also be authorized for an access via either localhost or IP, whichever you use.
Then to open a connection to MySQL in PHP using the mysqli class, you would usually use this code:
<?php
// your connection code
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'root', '', 'movies');
$mysqli->set_charset('utf8mb4');

Connection to database remotely from a server to another differently server

I have webhost from ABC Incorporate.
I bought a mysql database from XYZ Incorporate.
When I try to acces via PHP, the MySQL server from XYZ Incorporate, using the ABC Incorporate's server, it gives me this error:
connection refused in
I tried to talk with those two incorporates, and they said that.
No IP restriction was given, so I could've easly connect.
My question is, why is connecting?
My config.php
<?php
DEFINE ('DB_USER', 'yes');
DEFINE ('DB_PASSWD', 'somepassword');
DEFINE ('DB_HOST', 'IPv4');
DEFINE ('DB_NAME', 'dbname');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWD, DB_NAME);
if(!$con){
die('Database connection error');
}
?>
allow database host to connect remotely.It will be accessible.

How to connect to AWS RDS with php?

Can't connect to RDS with PHP. I have a simple script:
<?php
define('DB_SERVER', 'rdsendPointName:3306');
define('DB_USERNAME', 'actualusername');
define('DB_PASSWORD', 'passwordThatISetforRDS');
define('DB_DATABASE', 'RDSinstancenamehere');
echo "hello"
/* Connect to MySQL and select the database. */
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
?>
The php file is placed in the www/html folder. I can open the browser and get to the php page but then I get the connection error.
Failed to connect to MySQL: Unknown MySQL server host
Security group: I have opened the port 3306 and all tcp port but still no luck.
I run the php file from the ec2 instance itself and the result is the same.
It can't get any simpler than this. All I have is a php file, the RDS instance is running (at least that is what I can see from the aws console).
If you look at the documentation for mysqli_connect, the first parameter should be the hostname, and there are optional parameters if you want to specify a non-standard port.
So if you remove the :3306 from the DB_SERVER value, it should work.

php - mysql database connection

I'm trying to connect to my database to retrieve the table from sql workbench, but when I do it comes up with the following error message
could not connect to MySQL Unknown MySQL server host "Guada"
The php script is as follows:
<?php
//Using details to establish database connection
DEFINE ('DB_USER', 'i7421760');
DEFINE ('DB PASSWORD', '*********');
DEFINE ('DB_HOST', 'guada');
DEFINE ('DB_NAME', 'i7421760');
$dbc = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('could not connect to MySQL ' .
mysqli_connect_error());
/*
If the connection couldn't be established it
will print a display message and the error report
*/
?>
If it is the case of just getting the host name wrong, where can I find it? The table I want to retrieve is on sql workbench.
Any help is appreciated
thanks in advance
Update your code:
//Using details to establish database connection
DEFINE ('DB_USER', 'i7421760');
DEFINE ('DB PASSWORD', '*********');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'i7421760');
$dbc = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('could not connect to MySQL ' .
mysqli_connect_error());
/*
If the connection couldn't be established it
will print a display message and the error report
Is it possible your php and MySQL both run on a hosting service somewhere?
Many hosting services configure their internal networks so MySQL can be reached from php running on their internal servers, but not directly from customers' machines outside the hosting service networks. If you must access your MySQL directly from your home or office, ask your hosting service tech support for help.
But keep in mind that you're running a security risk by opening up your MySQL database to access by the broader internet.

mysqli_connect_error "Could not connect to mysqli:php_network_getaddresses: getaddrinfo failed: Name or service not known"

I'm new to php and mysqli and trying to connect to database but I got this error
"Could not connect to mysqli:php_network_getaddresses: getaddrinfo failed: Name or service not known"
stick in ubuntu and use xampp
this my code
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_NAME', 'first_db');
$dbc = mysqli_connect(DB_HOST,DB_USER,DB_NAME) OR die('Could not connect to mysqli:');
?>
Yes, your missing the password and since the error was php_network_getaddresses try to replace localhost with 127.0.0.1
<?php
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'first_db');
$dbc = mysqli_connect(DB_HOST,DB_USER, DB_PASS, DB_NAME) OR die('Could not connect to mysqli:');
?>
If you want to use secured connection you can use PDO:
$pdo= new PDO("mysql:host=127.0.0.1;dbname=first_db", 'root', 'password');
What mean php_network_getaddresses ?
It means PHP is trying to detect a hostname/address of a REMOTE PC
(eg. a user browsing your page) but it cannot resolve the address
(usually means that your server cannot query a DNS server or perform a
lookup.)
If this is annoying you, you can surpess the error message by adding a
'#' at the start of the method... like so..
by ballen

Categories