I am writing a program in C which accesses a mysql database. Everything functions great except one simple problem. On my development machine the database is hosted locally (localhost) and I have no problems. When I moved the program to a production system and altered the IP to that of the production mysql database, the machine still attempts to access mysql on localhost. I am rather confused and I don't even know where to begin with searching for the cause.
#define server "192.168.0.1"
mysql_init(&mysql);
connection = mysql_real_connect(&mysql, server, user, password, database, 3306, NULL, 0);
if (!connection) {
printf("%s", mysql_error(&mysql));
return 1;
}
I also tried it like this:
mysql_init(&mysql);
connection = mysql_real_connect(&mysql, "192.168.0.1", user, password, database, 3306, NULL, 0);
if (!connection) {
printf("%s", mysql_error(&mysql));
return 1;
}
And no matter what, the machine throws this error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
Which I believe is an attempt to bind to a local pipe instead of a remote socket. When I move back to my development machine and deliberately use the 192.168.0.1 address (which isn't even an address that exists in the development network) the program operates perfectly again, as it is just connecting to localhost. Even though I specified an IP address.
I know that when I connect to mysql via command line $(mysql -hlocalhost) it defaults to using a pipe instead of a socket such as if I used $(mysql -h127.0.0.1). I may have those backwards.. But it appears to be doing that. It's completely ignoring the IP and connecting via the local pipe. What on earth am I doing wrong??
I hope I explained that correctly. And I hope it's a stupid simple mistake that is obvious. Thank you guys.
**Note: addresses changed for security purposes
EDIT:
Interestingly I ran strings
on the binary and it didn't output the DNS address I put in as a response to the most recent post. This suggests to me maybe something about the compile process. For some reason it is seeing the string I am entering as perhaps NULL and automatically converts it to 'localhost' in the binary.
It appears to be a problem either with the compile or my source. But I don't see why it would be interpreting my string incorrectly and replacing it I guess on the assumption that it is unreadable or 'NULL'.
What am I doing wrong? :/
EDIT: Ok! Im an idiot. I rewrote the entire gcc command from scratch and all works well. Apparently my fingers don't work at 5am and using the up arrow to reuse a broken command all day doesn't fix the problem no matter how much code I write.
Thank you guys. Sorry for wasting your time!