0

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!

  • Locahost normally does a connection with the computer name and not necessarily with an IP address. Are you still connecting through the same computer? or you have two different computers. You might also need to connect with a different connection string. – Juniar Sep 26 '14 at 19:11
  • On the development system, it is on the same physical system. In production, two separate systems. What I discovered is that either way it ignores the IP I provide and attempts to connect through a "Unix socket file" rather than hitting the network stack. It behaves as though I am passing 'localhost' or NULL as the host string. – user3546886 Sep 26 '14 at 20:13
  • Are you calling; mysql_init() to initialize MYSQL structure first before calling the function; mysql_real_connect(). – Juniar Sep 26 '14 at 20:47
  • Yes, and I updated the question to reflect that on the line immediately prior. I had the same question and went back to look myself earlier in case I forgot. – user3546886 Sep 26 '14 at 21:13
  • You might have to try connecting using local IP address: 127.0.0.1. Otherwise read this question which addresses the same error message "Can't connect to local MySQL server through socket":http://stackoverflow.com/questions/5376427/cant-connect-to-local-mysql-server-through-socket-var-mysql-mysql-sock-38 – Juniar Sep 26 '14 at 21:50
  • Yes, but that thread is about someone who WANTS to connect to a local mysql server and was unable to do so. I do NOT want to connect to the local mysql server as in production it is not on that server. And it refuses to recognize that I am providing an address OTHER than localhost. If I was seeking to resolve that error I definitely could install mysqld on that machine and the error would go away. But in both production and development it ignores the remote address and ONLY connects locally. This is not the desired behavior. With a local server it functions great. I don't want a local server. – user3546886 Sep 27 '14 at 00:38
  • You might have to connect via the Internet IP address. You will need to use a domain such as; Server = www.domain.com this is for global connection not local connection. Do you have a web public domain? – Juniar Sep 27 '14 at 01:08

0 Answers0