Can't compile C program using MariaDB on CentOS - mysql

I want to run C programs with a connection to mariaDB, for that I have installed mariaDB version:
Server: MariaDB
Server version: 10.1.11-MariaDB MariaDB Server
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
But I came through an article
So, i didn't install C connector (due to similar nature of mysql-mariaDB) and tried this sample program from this link http://zetcode.com/db/mysqlc/.
But it says:
[root#localhost Desktop]# gcc version.c -o version `mysql_config --cflags --libs`
bash: mysql_config: command not found...
version.c:1:23: fatal error: my_global.h: No such file or directory
#include <my_global.h>
^
compilation terminated.
Then I tried installing libmysqlclient-dev/libmariaclient-dev & it says no package libmysqlclient-dev/libmariaclient-dev available.
Now what should I do?

You need to find out which package provides you with mysql_config. From what I've checked you can do it by yum whatprovides mysql_config. In case of Arch I got:
[root#main]# pkgfile mysql_config
extra/libmariadbclient

Try installing MariaDB-devel package from MariaDB repository.

I my case, the compiler couldn't find my_global.h
I tried:
$ mysql_config --include
-I/usr/include/mysql
But, the file was under /usr/include/mysql/server/my_global.h
This worked:
gcc [......] `mysql_config --include`/server

Its a pretty old thread and this probably has been resolved. However, I was able to make it work after deploying:
yum install mariadb-devel

You should #include <mysql/mysql.h> after installing the developer package. You should not include <my_globals.h> directly.
You might also be interested in MySQL Connector/C Developer Guide and 23.8 MySQL C API in the Oracle docs.
$ sudo dnf install mariadb-devel
...
And then:
$ find /usr/include -name my_global.h
/usr/include/mysql/my_global.h
/usr/include/mysql/server/my_global.h
$ cat /usr/include/mysql/my_global.h
/* Do not edit this file directly, it was auto-generated by cmake */
#warning This file should not be included by clients, include only <mysql.h>
And finally:
$ find /usr/include -name mysql.h
/usr/include/mysql/server/mysql.h
/usr/include/mysql/mysql.h
And a test:
$ cat test.c
#include <mysql/mysql.h>
int main(int argc, char* argv[])
{
return 0;
}
And:
$ gcc -Wall test.c -o test.exe -lmysqlclient

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");

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>

User can connect through PDO but not through C mysql_real_connect

I manage a MySQL database on my local machine and connect to it through C by running the following program:
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int main(int argc, char** argv ) {
my_init();
printf("MySQL client version: %s\n", mysql_get_client_info());
MYSQL mysql;
printf( "%p\n", &mysql );
MYSQL* mysql_initiate = mysql_init(&mysql);
printf( "%p\n", mysql_initiate );
MYSQL* attempt = mysql_real_connect(&mysql, "localhost","User","********","DB",0,NULL,0);
printf( "%p\n", attempt );
if(attempt != NULL) {
printf("OK\n");
}
else {
printf("Error: %s\n", mysql_error(&mysql));
}
mysql_close(&mysql);
return 0;
}
It is compiled by the following commands:
gcc -c `mysql_config --cflags` main.c
gcc -o a.run main.o `mysql_config --libs`
Where mysql_config --cflags outputs
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g -DNDEBUG
and mysql_config --libs outputs
-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl
The program compiles fine, but when executed, it outputs:
MySQL client version: 5.5.50
0x7fffcb8403e0
0x7fffcb8403e0
(nil)
Error: Access denied for user 'User'#'localhost' (using password: YES)
The user's name, password and database name have been changed for this present thread.
PHP connects successfully with the actual triplet of names and password, thus the credentials on User are not the cause of the problem. I made a plain copy-paste from the PHP code to the C code, there is no inaccuracy in the strings.
The same problem occurs when:
trying connecting as root,
trying with having granted all the possible privileges for User,
trying with a test user, granted all privileges with a one-letter password,
having changed the variables mysql and mysql_initiate into blip and blop (if some miraculous undetected collision would have occurred).
The Apache server is launched when I ran the executable, to testify I could connect to phpmyadmin. I tried after I logged out from phpmyadmin, same problem. The same problem occurs when I put NULL instead of "localhost", when I put the port 3306 (it is the MySQL port, told by netstat -natp) instead of 0.
Why could User connect to the database through PHP and why can't he connect to it through C?
I have googled the problem but there is nothing on the topic.
The version of gcc is
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
I am running under Linux Mint 17 Qiana.
MySQL is managed through XAMPP 7.0.6-0 and the database through phpmyadmin with the following versions and configurations (data retrieved from phpmyadmin welcome page):
Database server:
Server: Localhost via UNIX socket
Server type: MariaDB
Server version: 10.1.13-MariaDB - Source distribution
Protocol version: 10
User: root#localhost
Server charset: UTF-8 Unicode (utf8)
Web server:
Apache/2.4.18 (Unix) OpenSSL/1.0.2h PHP/7.0.6 mod_perl/2.0.8-dev Perl/v5.16.3
Database client version: libmysql - mysqlnd 5.0.12-dev - 20150407
PHP extension: mysqli
PHP version: 7.0.6
I am stuck facing a wall, any help appreciated.
If you need some extra info, feel free to ask.
Thanks in advance.
Beforehand I have browsed about similar problems and understood that some other users have solved their problems by removing XAMPP. I didn't know it was also my case. I wanted to try other things before resorting to such end.
I read that some XAMPP scripts about setting/changing the root password is deprecated.
I have gotten rid of XAMPP. XAMPP somehow corrupts the MySQL database when it comes to MySQL users and their rights.
In addition I have uninstalled mysql, apache and so on, then reinstalled them "by hand". It works.
I only now came accross that page:
https://community.apachefriends.org/f/viewtopic.php?f=16&t=72519
Maybe it would have solved my problem, maybe not. Anyway, I don't advise using XAMPP anymore.

Compiling nginx module with dependant library

I have followed the tutorial here to build a simple nginx module with success: https://github.com/perusio/nginx-hello-world-module
I can build and run that code with no issue and it functions correctly.
I now want to add a dependancy of MySQL to the simple nginx module so I have used the MySQL C Connector http://dev.mysql.com/doc/refman/5.6/en/c-api-building-clients.html.
The simplest program you can write is this (taken from http://zetcode.com/db/mysqlc/):
mysql_test.c
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
printf("MySQL client version: %s\n", mysql_get_client_info());
exit(0);
}
You then compile like so:
$ gcc mysql_test.c -o mysql_test `mysql_config --cflags --libs`
This works fine.
Now, when I try to include the two header files my_global.h and mysql.h with my simple nginx hello-world module, I need a way of specifying those build flags, else it won't find those header files. At the moment, when I run make, after it successfully builds all other modules, I now get:
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules -I src/mail \
-o objs/addon/nginx-hello-world-module/ngx_http_hello_world_module.o \
/vagrant/nginx-hello-world-module/ngx_http_hello_world_module.c
/vagrant/nginx-hello-world-module/ngx_http_hello_world_module.c:33:23: fatal error: my_global.h: No such file or directory
#include <my_global.h>
^
compilation terminated.
make[1]: *** [objs/addon/nginx-hello-world-module/ngx_http_hello_world_module.o] Error 1
make[1]: Leaving directory `/vagrant/nginx'
make: *** [build] Error 2
My question is: How do I get nginx to include those build flags when building my module?
I haven't found any information regarding the building of dependencies with Nginx.
Thanks!
Compilation Controls
--with-cc-opt=parameters
sets additional parameters that will be added to the CFLAGS variable.
--with-ld-opt=parameters
sets additional parameters that will be used during linking.
Source
For the MySQL Connector C it will be something like:
--with-cc-opt="-I/usr/include/mysql"
--with-ld-opt="-L/usr/lib64/mysql"
And in you module's config file:
CORE_LIBS="$CORE_LIBS -lmysqlclient"
During compilation it might also complain about _GNU_SOURCE being redefined and it will fail if you use -Werror without implementing a workaround.

How do I fix "Symbol not found: _is_prefix" when compiling Perl's DBD::mysql?

First I wanted to build the DBD::mysql package. That kept failing because whatever make resulted in could not be loaded for the tests with a Symbol not found: _is_prefix. So I assumed that cpan might be a tad old. I know it's a random assumption, but cpan did tell me to install the latest Bundle::CPAN.
Who's successfully installed either DBD::mysql or Bundle::CPAN on Mac OS X 10.5? Could you recommend any thing I could be doing differently?
This is perl, v5.8.8 built for darwin-thread-multi-2level
(with 4 registered patches, see perl -V for more detail)
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.1.36,
for apple-darwin9.5.0 (i386) using readline 5.1
Here's a log of the CPAN output for DBD::mysql:
Writing Makefile for DBD::mysql
cc -c -I/Library/Perl/5.8.8/darwin-thread-multi-2level/auto/DBI -I/usr/local/mysql/include -DDBD_MYSQL_INSERT_ID_IS_GOOD -g -arch ppc -arch i386 -g -pipe -fno-common -DPERL_DARWIN -no-cpp-precomp -fno-strict-aliasing -Wdeclaration-after-statement -I/usr/local/include -O3 -DVERSION=\"4.012\" -DXS_VERSION=\"4.012\" "-I/System/Library/Perl/5.8.8/darwin-thread-multi-2level/CORE" dbdimp.c
/usr/bin/perl -p -e "s/~DRIVER~/mysql/g" /Library/Perl/5.8.8/darwin-thread-multi-2level/auto/DBI/Driver.xst > mysql.xsi
Running Mkbootstrap for DBD::mysql ()
chmod 644 mysql.bs
/usr/bin/perl /System/Library/Perl/5.8.8/ExtUtils/xsubpp -typemap /System/Library/Perl/5.8.8/ExtUtils/typemap mysql.xs > mysql.xsc && mv mysql.xsc mysql.c
cp lib/DBD/mysql.pm blib/lib/DBD/mysql.pm
cp lib/DBD/mysql/GetInfo.pm blib/lib/DBD/mysql/GetInfo.pm
cp lib/DBD/mysql/INSTALL.pod blib/lib/DBD/mysql/INSTALL.pod
cp lib/Bundle/DBD/mysql.pm blib/lib/Bundle/DBD/mysql.pm
cp mysql.bs blib/arch/auto/DBD/mysql/mysql.bs
chmod 644 blib/arch/auto/DBD/mysql/mysql.bs
Warning: duplicate function definition 'do' detected in mysql.xs, line 225
Warning: duplicate function definition 'rows' detected in mysql.xs, line 650
cc -c -I/Library/Perl/5.8.8/darwin-thread-multi-2level/auto/DBI -I/usr/local/mysql/include -DDBD_MYSQL_INSERT_ID_IS_GOOD -g -arch ppc -arch i386 -g -pipe -fno-common -DPERL_DARWIN -no-cpp-precomp -fno-strict-aliasing -Wdeclaration-after-statement -I/usr/local/include -O3 -DVERSION=\"4.012\" -DXS_VERSION=\"4.012\" "-I/System/Library/Perl/5.8.8/darwin-thread-multi-2level/CORE" mysql.c
dbdimp.c: In function 'mysql_describe':
dbdimp.c:3309: warning: assignment from incompatible pointer type
dbdimp.c: In function 'mysql_describe':
dbdimp.c:3309: warning: assignment from incompatible pointer type
rm -f blib/arch/auto/DBD/mysql/mysql.bundle
LD_RUN_PATH="/usr/local/mysql/lib" /usr/bin/perl myld cc -mmacosx-version-min=10.5.7 -arch ppc -arch i386 -bundle -undefined dynamic_lookup -L/usr/local/lib dbdimp.o mysql.o -o blib/arch/auto/DBD/mysql/mysql.bundle \
-L/usr/local/mysql/lib -lmysqlclient -lz -lm \
chmod 755 blib/arch/auto/DBD/mysql/mysql.bundle
Manifying blib/man3/DBD::mysql.3pm
Manifying blib/man3/DBD::mysql::INSTALL.3pm
Manifying blib/man3/Bundle::DBD::mysql.3pm
CAPTTOFU/DBD-mysql-4.012.tar.gz
/usr/bin/make -j3 -j3 -- OK
Running make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/00base.t .................. 1/6 Bailout called. Further testing stopped: Unable to load DBD::mysql
# Failed test 'use DBD::mysql;'
# at t/00base.t line 21.
# Tried to use 'DBD::mysql'.
# Error: Can't load '/Users/dlamblin/.cpan/build/DBD-mysql-4.012-4n3pv8/blib/arch/auto/DBD/mysql/mysql.bundle' for module DBD::mysql: dlopen(/Users/dlamblin/.cpan/build/DBD-mysql-4.012-4n3pv8/blib/arch/auto/DBD/mysql/mysql.bundle, 2): Symbol not found: _is_prefix
# Referenced from: /Users/dlamblin/.cpan/build/DBD-mysql-4.012-4n3pv8/blib/arch/auto/DBD/mysql/mysql.bundle
# Expected in: dynamic lookup
# at (eval 7) line 2
# Compilation failed in require at (eval 7) line 2.
# BEGIN failed--compilation aborted at (eval 7) line 2.
FAILED--Further testing stopped: Unable to load DBD::mysql
make: *** [test_dynamic] Error 255
CAPTTOFU/DBD-mysql-4.012.tar.gz
/usr/bin/make test -- NOT OK
//hint// to see the cpan-testers results for installing this module, try:
reports CAPTTOFU/DBD-mysql-4.012.tar.gz
Running make install
make test had returned bad status, won't install without force
Failed during this command:
CAPTTOFU/DBD-mysql-4.012.tar.gz : make_test NO
Okay, if you get these errors I now know the following:
MySQL 5.1 for Mac OS X x86_64 is not compatible with DBD::mysql (yet). Install the 32-bit x86 version, and try again. You'll succeed. I wish the perl Makefile.pl would just tell you that in a banner.
Bundle::CPAN had issues because I wasn't installing as root. Why that makes it report circular references instead of installation permission issues, I'll never understand.
Please add a comment if and when this became outdated information.
Installing the (beta) 5.4.1 64 bit version of mysql, available from their developer website, fixes the issue. Tested on Snow Leopard.
Did you try installing Bundle::DBD::mysql?
I haven't dealt with this problem, but I found that MacPorts cleaned up all my UNIX incompatibility problems. You might want to try that before enduring too much pain and suffering.
Where is it complaining about a circular dependency? It looks like you are trying to link to an incompatible version of the mysql libraries. The symbol it's looking for isn't in the library you loaded. I don't think this is a problem caused by CPAN.pm or the cpan script.
Some questions:
Who compiled perl? Is this Apple's perl?
Who compiled mysql? Is that your own version since it's in /usr/local?
Did you previously compile other versions? I start with a compile to ensure everything points to the right places.
Installing latest beta 64bit version of Mysql fixed problem on my computer.