User can connect through PDO but not through C mysql_real_connect - mysql

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.

Related

Superset with Docker cannot add connection to external mysql

I am newbie on using Ubuntu, I am trying to install apache-superset and successfully installed it by using Docker by directly sudo docker pull apache/superset, but I am stuck at adding Database Connector to the running superset
As for my local database I am using MySQL and I happen to use SSH Tunnel in localhost to access it in server. So I think at Docker container perspective this must be an "external" databases
What I have tried:
I installed mysqlclient from pip3
By following this references: https://devopsheaven.com/docker/devops/add-host/link/2017/10/04/connect-external-services-from-docker-container.html
I tried to type: sudo docker run -it mysql -h 192.168.100.1 -P 33063 -u czjovan --password=mypw cz_payment_merged but then i get:
2021-03-04 11:34:53+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
2021-03-04 11:34:53+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
command was: mysqld -h 192.168.100.1 -P 33063 -u czjovan --password=mypw cz_payment_merged --verbose --help --log-bin-index=/tmp/tmp.EV6L0jrspQ
2021-03-04T11:34:53.402148Z 0 [ERROR] [MY-010124] [Server] Fatal error: Can't change to run as user 'czjovan' ; Please check that the user exists!
2021-03-04T11:34:53.403355Z 0 [ERROR] [MY-010119] [Server] Aborting
By Following this also: From inside of a Docker container, how do I connect to the localhost of the machine?
I tried to type: sudo docker run --rm it --network=host mysql mysql -h 127.0.0.1 but got error 2003 (hy000) can't connect to mysql server on 127.0.0.1
I tried to add mysqlconnector to sqlalchemy uri, but the driver not found
I am not an expert by setting these, I lack of Docker mechanism.. appreciate it if anyone willingly to direct me how to step by step resolving this..
UPDATE-------------------------------------------------:
-> Following Mustafa Guler to add -p 3306:3306, the mysql container now starts..
but I still cannot add database in Superset, what should I do next?
You need to provide the IP of the docker host machine. If you're using default networking, use the static IP 172.17.0.1 for the host.
Alternatively you can try host.docker.internal as the hostname.
You need to define user and pass as environment to create new user during start of mysql container. Maybe problem is user and pass MYSQL_USER, MYSQL_PASSWORD Also you do not expose 3306 port on docker run. Please use -p 3306:3306
I found out that installing Superset manually from scratch than using Superset from Docker Container are more a solution to me, since configuring Superset from docker manually can be a little cumbersome, what I did:
#-- Install Superset and MySQL Locally from Scratch ---#
sudo pip install apache-superset (
sudo apt install mysql-client-core-8.0
sudo apt install mysql-server
sudo pip install mysqlclient (for Superset Database Connectors)
#NOTE: there is also a package called 'superset' in pip, in my case i uninstalled this to ensure that only apache-superset is used
service mysql start
when mysql start, try to set password for the first time:
sudo mysqladmin -u root password
then to test it: mysql -u root -p, enter a created password
if it can enter mysql normally, the password set is successful,
CREATE DATABASE superset (this will save all config that superset progress will be saved)
Editing config.py in apache superset, which usually located on /usr/local/lib/python3.8/dist-packages/superset/config.py (this depends on pip installation from no 1),
8a) Edit the sqlalchemy uri part in config.py, so it can connect to a local installed mysql in ubuntu, to something like 'mysql://root:#localhost:3306/superset'
8b) Ensure that the database part in config.py is superset, or the same name with database name created in MySQL
After all database, sqlalchemy uri, and database name is prepared, its good to go to follow with Superset configs stated from: https://superset.apache.org/docs/installation/installing-superset-from-scratch
A) sudo superset db upgrade
B) sudo superset fab create-admin
C) sudo superset load examples
D) sudo superset init
E) sudo superset run -p 8088 --with-threads --reload --debugger (run this to start superset daily)
Run a Private SSH Tunnel Connecting to Database (Optional, in my case I use SSH Tunnel to access database)
And then I am able to add connection locally in Superset defining sqlalchemy uri
A) towards the SSH Tunnel mysql://(server_user):(server_password)#127.0.0.1:33063/(database_name)
(this is outside from local MySQL)
B) towards Local Installed MySQL 'mysql://root:rootpwd#127.0.0.1:3306/database_name'
I will recommend to use docker than manual installation by pip. Package dependencies especially flask are real problems.
Do not follow docker compose as beginner.
I had a problems using docker compose. Port is closed was annoying problem due to networking. Host.docker.internal doesn’t worked for me on Ubuntu 22 on Google cloud. I switched to manual installation and it was package versions hell on python 3.8. I would like to recommend to not follow official doc and use better approach with single docker image to start. Instead of running 5 containers by compose, run everything in one. Use official docker image, here image. Than modify docker file as follows to install custom db driver:
FROM apache/superset
USER root
RUN pip install mysqlclient
RUN pip install sqlalchemy-redshift
USER superset
Second step is to build new image based on docker file description. To avoid networking problems start both containers on same network (superset, your db) easier is to use host network. I used this on Google cloud, example as follow:
docker run -d --network host --name superset supers
The same command to start container with your database. —network host. This solved my problems. More about in whole step to step tutorial: medium or here blog

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

Orion Context Broker functional test failure

I have successfully forked and built the Context Broker source code on a CentOS 6.9 VM and now I am trying to run the functional tests as the official documentation suggests. First, I installed the accumulator-server.py script:
$ make install_scripts INSTALL_DIR=~
Verified that it is installed:
$ accumulator-server.py -u
Usage: accumulator-server.py --host <host> --port <port> --url <server url> --pretty-print -v -u
Parameters:
--host <host>: host to use database to use (default is '0.0.0.0')
--port <port>: port to use (default is 1028)
--url <server url>: server URL to use (default is /accumulate)
--pretty-print: pretty print mode
--https: start in https
--key: key file (only used if https is enabled)
--cert: cert file (only used if https is enabled)
-v: verbose mode
-u: print this usage message
And then run the functional tests:
$ make functional_test INSTALL_DIR=~
But the test fails and exits with the message below:
024/927: 0000_ipv6_support/ipv4_ipv6_both.test ........................................................................ (FAIL 11 - SHELL-INIT exited with code 1) testHarness.sh/IPv6 IPv4 Both : (0000_ipv6_support/ipv4_ipv6_both.test)
make: *** [functional_test] Error 11
$
I checked the file ../0000_ipv6_support/ipv4_ipv6_both.shellInit.stdout for any hint on what may be going wrong but error log does not lead me anywhere:
{ "dropped" : "ftest", "ok" : 1 }
accumulator running as PID 6404
Unable to start listening application after waiting 30
Does anyone have any idea about what may be going wrong here?
I checked the script which prints the error line Unable to start listening application after waiting 30 and noticed that stderr for accumulator-server.py is logged into the /tmp folder.
The accumulator_9977_stderr file had this log: 0000_ipv6_support/ipv4_ipv6_both.shellInit: line 27: accumulator-server.py: command not found
Once I saw this log I understood the mistake I made. I was running the
functional tests with sudo and the secure_path was being used instead of my PATH variable.
So at the end, running the functional tests with the command below solved the issue for me.
$ sudo "PATH=$PATH" make functional_test INSTALL_DIR=~
This can also be solved by editing the /etc/sudoers file by:
$ sudo visudo
and modifying the secure_path value.

Can't compile C program using MariaDB on CentOS

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

MySQL hell... can't connect to database /tmp/mysql.sock

Update: I've STOPPED the older mysql process which was running and causing some confusion. Now I think I have only the newer (5.1.40) version running. BUT, it's pointing to the wrong data file. It's pointing to a default install data file and I'd like it to point to the existing data file in /var/mysql. Here's a portion of /etc/my.cnf
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /var/mysql/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port = 3306
socket = /var/mysql/mysql.sock
this is pointing to the older mysql.sock. I can't seem to find in the directory tree of the newer MySQL install?!? unless it's somewhere obscure.
Anyone help? Basically I installed a newer MySQL and now need to get this new version to launch with my existing data. And sort out this mysql.sock thing....
I've recently updated MySQL on Mac OS X Server and am having a hell of a time connecting to it from a rails app. or consistently from the command-line for that matter.
i'm sure this is an obvious error on my part but I only have moderate command-line experience so hoping someone can help...
also related is my rails app no longer can connect. Failing to connect via /tmp/mysql.sock but I'm not sure why it's looking there because there is no mysql.sock in /tmp and I don't know what/where it should be....
Edit: adding results from mysql_config --sockets
$ mysql_config --sockets
Usage: /usr/local/mysql/bin/mysql_config [OPTIONS]
Options:
--cflags [-I/usr/local/mysql/include -g -Os -arch ppc -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL]
--include [-I/usr/local/mysql/include]
--libs [-arch ppc -L/usr/local/mysql/lib -lmysqlclient -lz -lm -lmygcc]
--libs_r [-arch ppc -L/usr/local/mysql/lib -lmysqlclient_r -lz -lm -lmygcc]
--plugindir [/usr/local/mysql/lib/plugin]
--socket [/tmp/mysql.sock]
--port [0]
--version [5.1.40]
--libmysqld-libs [-arch ppc -L/usr/local/mysql/lib -lmysqld -ldl -lz -lm -lmygcc]
Edit2 which mysql_config
$ which mysql_config
/usr/local/mysql/bin/mysql_config
You are trying to use different sockets for the server and client. Your Rails is trying to connect to /tmp/mysql.sock, by MySQL is listening on /var/mysql/mysql.sock.
Normally MySQL configuration is stored in /etc/my.cnf, but in your ps your output I see socket path is given as a parameter. So really depends on your system's specifics.
Anyway, look in /etc/my.cnf and your database.yml and make sure mysql.sock appears at the same path in both files.
Not sure whether this will completely solve your problem (you may need to fix the socket path as suggested by cababunga), but try 127.0.0.1 to bypass the socket connection and establish a TCP one.
The function real_connect in the Ruby MySQL module actually takes a bunch of parameters:
real_connect(host,user,password,db,port,socket,flags)
The one you need to change is the "socket" parameter. On my Python apps on Mac OS X, I have to set that socket parameter to be /var/mysql/mysql.sock.
So notice, it's not the host you need to change, it's the actual socket. Is there a socket: parameter you can use in your config?
So here's what you can do...
Run the command mysql_config --socket on your command line in a terminal in OS X and it should return you the value of the "socket" you need to use (probably /var/mysql/mysql.sock).
Then in your database.yml file, make sure you add the line:
socket: /var/mysql/mysql.sock
I've mostly run into this via Python on OS X and fixed it in a similar fashion, but I'm going to guess that Ruby will give you the same issue.