user293895
user293895

Reputation: 1527

MySQL Connector C API Unable to connect to MySQL server

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.

Upvotes: 1

Views: 1847

Answers (1)

xda1001
xda1001

Reputation: 2459

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

Upvotes: 1

Related Questions