Search code examples
c++mysqlmariadbgoogle-cloud-platformmysql++

Trying to connect to a MariaDB database in google-cloud but cant connect nor show any error report


I'm trying to connect a simple program to a MariaDB database created in a VM in Google Cloud. The VM already has a working installation of MariaDB and a small working demo DB.

The code used is the following:

#include <mysql++/mysql++.h>
#include <iostream>

// Nombres
using namespace std;
using namespace mysqlpp;

// INICIO
int main ()
{

    //Declara variables
    char server[]   = "104.197.112.189:3306";
    char user[]     = "root";
    char pass[]     = "xxxxxxxx";
    char db[]       = "resst";
    unsigned int i;

    //conexión
    Connection con;
    con.connect("", server, user, pass);
    con.select_db(db);

    //pedido SQL
    string consulta = "select * from  productos";

    //realización de pedido
    Query pedido = con.query(consulta);
    StoreQueryResult resp = pedido.store();
    Row fila;
    for (i=0;i<resp.num_rows();i++)
        {
            cout << "res " << i+1 << " " << resp[i]["nombre"] << endl;
        }

    con.disconnect();

    return 0;
}

This code works perfectly with an exact copy of this DB created in my localhost, and prints a result when I run it locally.

On the other hand when I run it with the connection to the online DB, the program shows absolutely nothing, no window, no line, no error, just the terminal where I run it with a blank line, so i dont know where to start looking. The error reports are active, and show any other errors that happen.

Please,can anybody give me a hint on this?


Solution

  • Ok for someone with a similar problem, Mariadb (probably also works for MySQL) and Google Cloud have all external connections locked by default. It can be solved in two steps:

    In google cloud: going into the developer console/ your project/ connections/ firewall rules and create a new rule allowing port 3306, that is default for MySQL and Mariadb.

    In Mariadb: change my.conf, following the first part of this tutorial. And that's it, now it's working. Thanks to @mrunion for the help.