Unable to Connect to mysql db through php in GCE instance with ubuntu


Question: 

I'm a little bit frustrated with a problem I'm having in a system migration from AWS to GCE.

I've an ec-2 instance running on amazon web services, perfectly working and configured.

The problem is that google is offering a 100.000$ credit in a special plan for Startups, my company's CEO asks for a migration, so here I am.

I have all running and almost fully configured in a ubuntu 14.04 LTS instance, I said almost, because I can't achieve a simple task such as connect to a Mysql db :(, and I think I'm missing something in someplace and my mind gets offuscated, so I need your help.

First of all, I only having this problem on remote server, I've tested it like a hundred times in local, and of course in aws, where it is currently in production state, so I have no clue what or where exactly the problem is.

My GCE config:

OS: Ubuntu 14.04 LTS HHD: 30GB persistence ssd disk CPU : 2x RAM : 7,5 GB Zone: europe-west1-c Static Ip

So, I installed Lamp and phpmyadmin, all works like a charm. I disable root access from remote clients to Mysql db, configured php and apache modules.

The Web app is a Rest Api, based on PHP(Slim), that, like I just said, is now working on AWS.

When I call to and endpoint that doesn't need to retrieve any data from the db, it works well, the problem comes when the endpoint called try to access the db. In that case the request never respond, it keeps loading forever until I abort the request. It is kind of weird, because I can manipulate all the data through phpmyadmin, which access to the db the same way mysqli does.

If I checked the logs, nothing is printed on them. Only when I change the "locahost" host param, it prints a log saying: "No mysql db on XXX.XXX.XXX.XXX"(IP).

config.php File

define('DB_USERNAME', 'user');
define('DB_PASSWORD', 'pass'); 
define('DB_HOST', 'localhost'); 
define('DB_NAME', 'db_name'); 

DbConnect.php

function connect() {
    include_once dirname(__FILE__) . '/Config.php';

    // Connecting to mysql database
    $this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

    // Check for database connection error
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    /* change character set to utf8 */
    if (!$this->conn->set_charset("utf8")) {
        echo "Error loading character set utf8: " . $this->conn->error;
    } 

    // returing connection resource
    return $this->conn;
}

loops forever img

Expected Response img

Any help is welcome!




1 Answer: 

Have you changed the bind-address parameter in the my.cnf MySQL config file? This has caught me a few times. MySQL by default in Ubuntu 14.04 will only listen on localhost and will ignore remote connections.

Also, check out the answer to this question and it might give you some help as well: Remote Connections Mysql Ubuntu - bind address failed

 

More Articles


shell - Gmail imap can't access folders with space (or other escapted characters) in the title

Lets say i'm connection to imap.gmail.com via shell>> openssl s_client -connect imap.gmail.com:993 -crlf>> a1 login asdasd@gmail.com asdasdasdasdGetting the list of the folders:>> a1 list "" *Respond:....* LIST (\HasChildren) "/" "github"* LIST (\HasNoChildren) "/" "Wizz&AKA-Ai

realtime data communication between two c++ executeables one windows and other in virtual machine ubuntu

I am running ros to control a robot using ubuntu. But the drivers are available for windows only.I have setup a virtual machine where the robot controller is built on roscppI want pass these values generated using ros to a cpp in windows.it should be realtime. data should be flowing back and forth.d

postgresql - Modifying pg_hba.conf

I want to modify the linelocal all all peertolocal all all md5within a shell script. What is the best way to go about this? If I am to search for the line, what string should I be looking for? There are a lot of spaces between each word in the pg_hba.confOr is there a better way at doing this?Thank


c++ - Header location issues while porting Qt code from Windows to Ubuntu

I'm trying to port code written in Qt on Windows to Qt on Ubuntu. The problem I'm facing is that it gives me this error for my ui_windowform.h header:error: QtWidgets/QApplication: No such file or directoryI searched on Google for solutions but didn't find anything relevant. I've also added INCLUDEP

mysql - MariaDB refusing remote connections

I am currently using digitalocean ubuntu 16.04 . I am new to this version because I used older versions before. I have a problem in connecting to the database remotely but when I logged in to the phpmyadmin it is all ok.by the way I followed the tutorial in this article by Dan Costinelhttps://askubu

node.js - Ubuntu + Nodejs + curl: (52) Empty reply from server

I have a nodejs process listening a port on my server:app.get('/statistic', function(req, res) { log_get_connection(req); getStatistic().then(statistic => { res.json(statistic); });});When I try to access it via curl being on the same machine:curl "http://127.0


How do I insert PHP variables in a MySQL table?

This question already has an answer here: Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors? 3 answers

shell - UNIX: copy entire directory structure and only specific file type

How i can recreate entire directory structure and copy only files with .txt extension if present. I would like to copy the directory or sub-directory even if its empty.

php - Ubuntu Server Virtual Host not redirecting properly

I have installed Web site on my Ubuntu Server. The following things are installed on my Server for running web service. 1) Apache22) PHP53) MySql My main website is redirected to https.Example (changed for security purpose) <VirtualHost *.80>ServerName www.example.comServerAlias example.co

shell - Move output of Find command unix

Im learning unix programming , i want to find all files whose size is greater than 1M an print them into a file.here is my code find. -size +1M -print0 | xargs -I -O '{}' mv '{}' files all sites i have found refer to this one as right one , but it does not work . currently im working on ubuntu