Reputation: 2453
This is my main.php
code just to try connecting to MySQL database and check if it can connect to it or not.
<?php
$USER = "root";
$PASS = "";// I do have a password. I just haven't mentioned on StackOverflow
$IP = "104.199.248.141";
$DB = "Contacts";
$conn = mysqli_connect($IP,$USER,$PASS,$DB,null,"/cloudsql/adiscontactbook:asia-east1:contacts-book");
if($conn != null) {
print("Connected! :*");
mysqli_close($conn);
} else {
print("Can't connect! :(");
}
?>
And this is app.yaml
:
runtime: php55
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.php
I CAN connect to my mysql instance via terminal by typing:
mysql --host=104.199.248.141 --user=root --password
and the output on terminal is Connected! :*
.
This is my app URL: https://adiscontactbook.appspot.com/
When I type the URL in the browser, it outputs Can't connect! :(
.
PS: I have given permissions to my app and as well as my local computer in the Authorize networks
section to access the mysql instance by entering the IP address 35.185.186.77
(external IP of my app) and 14.97.103.225
respectively.
Why is it so that I have no problem accessing it via terminal but can't connect via browser by typing the URL?
Upvotes: 4
Views: 1433
Reputation: 91
I spent a whole day trying to figure this out....was converting from mysql to mysqli in preparation for php7 and 2nd generation CloudSQL from appengine. I couldn't find anywhere in the doc that you had to use this 6 parameter format for the mysqli_connect() function.
In addition, the ":" that precedes the server name in the mysql_connect needs to be removed (but not the forward slash '/') when using it with mysqli. Hence:
$servername=":/cloudsql/project:us-east1:database";
becomes:
$servername="/cloudsql/project:us-east1:database";
Upvotes: 0
Reputation: 2259
You should pass null to the $host
parameter, as you're connecting through a socket.
$conn = mysqli(null, $USER, $PASS, $DB,null, "/cloudsql/adiscontactbook:asia-east1:contacts-book")
Upvotes: 2
Reputation: 3773
The fifth parameter of mysqli_connect is the port number and the sixth parameter is the socket path. You can try to leave out the fifth and sixth parameters. See the documentation for mysqli_connect (http://php.net/manual/en/function.mysqli-connect.php)
Upvotes: 0