1

I am attempting to connect to a MySQL database setup by XAMPP using the MySQL Connector C API on OSX. Using this code:

MYSQL mysql;
mysql_init(&mysql);
if(!mysql_real_connect(&mysql,"localhost","root","",NULL,0,NULL,0))
{
    printf("Failed to connect to database: Error (%d): %s",mysql_errno(&mysql),mysql_error(&mysql));
}
else printf("Connection success!");

I get the following result:

Failed to connect to database: Error (2002): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

I can confirm the database is operational because phpMyAdmin connects to it fine, here is the config.inc.php file:

/* Server parameters */
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';
$cfg['Servers'][$i]['connect_type'] = 'socket'; 
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = true;

Does anyone know why phpMyAdmin can connect but my C program cannot? I have tried forcing TCP/IP connections to no avail.

1 Answer 1

1

You can connect the default port 3306 mysql_real_connect(&mysql,"localhost","root","",NULL,3306,NULL,0)

or reconfig the my.cnf (suppose you use linux) and restart mysql.

[mysqld]
socket=/tmp/mysql.sock

[client]
socket=/tmp/mysql.sock
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it turns out XAMPP changes the socket MySQL listens on, after finding the my.cnf it states the path to the socket.
Tried using port 3306 as you suggested. Did not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.