0

I can't understand what follows, can someone explain me and help me solve the problem?

I have a mariadb-server a front-end application in C.

I have 2 make files and i'd like that i can use both of them.

The first one is this

all:
    gcc -g src/*.c -o applicazione `mysql_config --cflags --include --libs`
clean:
    -rm applicazione

and it works. If i compile with this, my application runs without any trouble.

The second one is this

all:
    gcc -g src/*.c -o applicazione `mariadb_config --cflags --include --libs`
clean:
    -rm applicazione

The difference is that in the first I used mysql_config, while in the second I used mariadb_config.

My problem is that with the second makefile, (after some problems) I can successfully compile, but as soon as I try to connect to the server I get this error

fabiano@fabiano-HP-15-Notebook-PC:~/Scrivania/BackupProgetto/0226198$   ./applicazione
Inserisci Matricola: g1
Inserisci Password: *
Connection error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Reading on the net i understand that the problem is that the socket is not where my application try to find it. Indeed if i execute sudo mariadb and after that \system i can read this

UNIX socket:        /var/run/mysqld/mysqld.sock

Now my questions:

  1. why my application run successfully with the first make file but it doesn't with the second one ?
  2. what can i do for let my application works with both make files ?

My OS is Ubuntu 18.04.3 LTS.

fabianod
  • 501
  • 4
  • 17

1 Answers1

1

If you compare output from mysql_config --libs with output from mariadb_config --libs you will probably notice that different libraries from different locations will be used.

mariadb_config is part of MariaDB Connector/C, the default build uses /tmp/mysql.sock for the socket file:

IF(NOT MARIADB_UNIX_ADDR)
  SET(MARIADB_UNIX_ADDR "/tmp/mysql.sock")
ENDIF()

The libraries from mysql_config output were compiled with default socket located at /var/run/mysqld while the libraries from mariadb_config where compiled with socket located in the tmp directory.

There are several options to fix that:

1) Change the socket in your my.cnf file. This needs to be done in [mysqld] section, but also in [mysql] section to make sure that the client tools will work properly.

2) Set the environment variable MYSQL_UNIX_PORT to /var/run/mysqld/mysql.sock before running your application

3) If you build MariaDB Connector/C on your own:

cd mariadb-connector-c
mkdir bld
cd bld
cmake .. -DMARIADB_UNIX_ADDR=/var/run/mysqld/mysql.sock
cmake --build .

4) Before connecting you can specify the location of the socket in your application:

mysql= nysql_init(NULL);    
rc= mysql_options(mysql, MARIADB_OPT_UNIXSOCKET, "/var/run/mysqld/mysql.sock");
Georg Richter
  • 5,970
  • 2
  • 9
  • 15