• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

246
Vistas
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 3 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

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 3 years ago · Santiago Trujillo Denunciar

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 3 years ago · Santiago Trujillo Denunciar

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 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda