Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

331
Views
Can't connect to mysql using PHP on google cloud. No errors popup

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?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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")
about 4 years ago · Santiago Trujillo Report

0

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)

about 4 years ago · Santiago Trujillo Report

0

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";
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!