How to use MySQL with Ubuntu ‘C’? - mysql

I am using Ubuntu Linux 20.04.2 and MySQL 8.0.30 but when I try to compile using a Linux Terminal command like:
gcc -o program program.c ‘mysql_config —libs’
I get a message like:
“Error: mysql. . No such file or directory”
It appears that gcc doesn’t recognize the directory where the ‘Include’ ( .h ) files are located.
That is: ‘/usr/include/mysql’.
I have tried several variations of the gcc command without success. There is probably a simple solution and I would appreciate any help.

It's $(pkg-config --libs --cflags mysql...), replace the mysql... with the name of library.

Thank you to those who answered my question. What seems to have solved the problem, is adding the -I option to the gcc command. I used:
$ gcc -o progrm -I /usr/include/mysql program.c

Related

change socket address mariadb_config

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:
why my application run successfully with the first make file but it doesn't with the second one ?
what can i do for let my application works with both make files ?
My OS is Ubuntu 18.04.3 LTS.
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");

How to solve a problem of including json-glib.h in a c++ file

I'm trying to compile a c++ file that requires the header json-glib.h and the compilation log says that such header is not found. I'm in ubuntu 18.04 LTS.
I've tried executing dpkg -l '*json-glib*' to check that i have such file in my system and the result is:
||/ Nombre Versión Arquitectura
ii libjson-glib-1.0-0:amd64 1.4.2-3 amd64
ii libjson-glib-1.0-common 1.4.2-3 all
So next, i executed the command pkg-config --cflags libjson-glib-1.0 and got:
Package libjson-glib-1.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libjson-glib-1.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libjson-glib-1.0' found
I tried pkg-config --cflags json-glib-1.0 , pkg-config --cflags json-glib, and pkg-config --cflags libjson-glib as well and the result is similar as above.
I also followed the instructions of the answers of Fatal error: json-glib.h file not found and https://unix.stackexchange.com/questions/297277/make-doesnt-find-an-installed-library/298035#298035 and still i have the problem.
My include in the code is:
#include <json-glib/json-glib.h>
...
I will appreciate any help you can give me, Thanks.
Try to install the package libjson-glib-dev, after that run pkg-config --cflags json-glib-1.0
Same issue... I've resolved with the following:
sudo apt-get install libjson-glib-dev

fatal error: 'mysql/mysql.h' file not found

I have installed mysql using
brew install mysql
Then compiled using
gcc -I/usr/include mydb.c
However it gives me the error:
fatal error: 'mysql/mysql.h' file not found
What am I doing wrong here? Am on macosx 10.12
edit: tried gcc -I/usr/local/include/mysql/ mydb.c still broken
On ubuntu16.04,mysql.h locates in /usr/include/mysql.
You can find it after install libmysqlclient20 or libmysqlclient-dev.
Verify at where are install mysql, for this, using the command for see the file list of directory
ls
and using
cd
The installation, in my example, are is in directory:
/usr/local/mysql-<version_my_mysql>/include/
second step is indicate the directory everytime compiller:
gcc my_program_code_file.c -I /usr/local/mysql-5.7.9-osx10.9-x86_64/include/ -o my_program_executable
the includers in code are:
#include <mysql.h>

Why does mariaDB wrongfully ask for missing component

I'm trying to build from source MariaDB on CentOS 7. I have only done the following commands before I get the error shown in this image:
$ cd /usr/local/src
$ wget https://downloads.mariadb.org/interstitial/mariadb-5.5.40/source/mariadb-5.5.40.tar.gz/from/http%3A//ftp.ddg.lth.se/mariadb
$ tar xzvf mariadb
$ cd mariadb-5.5.40/
$ chown -R root.root .
$ cmake . -DCMAKE_INSTALL_PREFIX:PATH=/usr/local/mariadb
proof that libaio-devel is installed
For anyone that is in a similar situation, cmake apparently does a pretty bad job of re-checking dependencies, even removing the cache file doesn't work very well.
Just delete and re-extract from the .tar.gz and then cmake again, if you actually have the dependencies then they will be found.

cannot find -lmysqlclient

I'm trying to compile a C++ program and one of the classes uses . g++ is not able to find the libraries would be my guess. The command i use to compile is -
g++ c1.cpp c2.cpp c3.cpp c4.cpp -o c4 -lm -lmysqlclient
c3.cpp is the file that needs mysql.h. This works perfectly on my local machine, but refuses to run on the server with the error
cannot find -lmysqlclient
I tried finding the libmysqlclient.so files on the server using the find command, I don't think they are present there
uname -a
reveals
SunOS opteron 5.10 Generic_139556-08 i86pc i386 i86pc
user#opteron 12:26:02 ~/c++/projname/
I realize that i need to link some libraries, but where and how?
Any help would be appreciated.
Thanks.
Whatever library packages u think is not installed can be installed using sudo apt-get install. But the problem is to find the right name of the package apt-get can understand. So how to do that ?! simple
use command : sudo apt-cache search <filename>
For eg.: in this case lmysqlclient
sudo apt-cache search mysqlclient
(remember to exclude 'l' from the actual name ,ie, mysqlclient and not lmysqlclient).
This outputs:
libmysqlclient-dev - MySQL database development files
In the above -libmysqlclient-dev is the name that apt-get can recognize and solve our cannot find lmysqlclient problem
so now type: sudo apt-get install libmysqlclient-dev from interface.
After its done, try making your required file.
Simplifying #SriHariY.S's answer-
Try installing it with sudo apt-get install libmysqlclient-dev.
Do you have the MySQL client libraries? Can you look for it as
find / -name "libmysqlclient.so" -type f -print 2>/dev/null
Also, you can use the -R flag on linker to hardlink the libmysqlclient as
g++ -R/usr/local/mysql/lib ....
Or, you can export the LD_LIBRARY_PATH_32 or LD_LIBRARY_PATH_64 as
export LD_LIBRARY_PATH_32=$MYSQL_HOME/lib
Urko,
On Ubuntu 18 I used this command to find a name of required package for fixing this error:
apt search lmysqlclient
After this I installed missing package:
sudo apt install libmariadbclient-dev-compat