Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

338
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda