Basically, I am trying to connect to a MySQL Database using Connector C drivers for MySQL. I am writing the code in Netbeans on a Linux system. But here is the mystery: I can connect to my database in Debug Mode, but not Release Mode??? I am using Netbeans as my IDE on Linux, and I set up all the libraries and include folders using the dropdown under project properties so it applies the properties to both the Debug Configuration and Release Configuration, and yet it will connect to the database when I execute in Debug, but not when I execute in Run - ??? I simplified the code to show you the actual connection code:
int readDB(void) {
MYSQL *mysql = NULL;
char *server = "localhost";
char *user = "root";
char *password = "";
char *database = "myDB";
int port = 3306;
mysql = mysql_init(mysql);
if (!mysql) {
puts("Init failed, out of memory?");
return EXIT_FAILURE;
} else {
puts("SUCCESS!\n");
}
if (!mysql_real_connect(mysql,server,user,password,database,port,NULL,0)) {
puts("Connect failed\n");
}
return 1;
}
So when I run it in Debug, it prints "SUCCESS" and nothing else. When I run it, it prints "SUCCESS" followed by "Connect failed". I am at a loss. Could this be a problem with the IDE? Is there another IDE for C that works well on Linux that I should consider? Or am I missing something quite obvious?
Update: calling mysql_error() revealed
"Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"
When using a mysql client library in most languages including C, if the hostname is specified as localhost
the client library will attempt to connect through a unix domain socket. You can force connection using TCP/IP by specifying the hostname as 127.0.0.1
Yes they are one and the same but
If unix_socket is not NULL, the string specifies the socket or named pipe to use. Note that the host parameter determines the type of the connection.
Alternatively you can examine your mysql configuration to find out where the socket has been created when the server starts and use that as the unix_socket parameter.
Third alternative is to change your server configuration to create the socket on /tmp/