mysql connect

Started by
6 comments, last by AbelCorver 13 years, 6 months ago
Hey everyone,

I'm using MySQL Connector/C to connect to my local MySQL database. It keeps saying:

Quote:Can't connect to MySQL server on 'localhost' (0)


Even when I fill out all the information, like:

mysql_real_connect(&mysql, "localhost", "root", "", "abelcorver_com", 3306, "C:/xampp/mysql/mysql.sock", 0)


I find it strange it says "'localhost' (0)", since I explicitly specified a port number.

Thanks in advance!
Advertisement
I can't guarantee that this is you issue because I haven't done a great deal of work with mysql on windows but under unix, mysql is really good at automatically assuming that if you are connecting to localhost then it should use the unix socket. If you really want to use TCP then you have to tell it before hand.

Either check that you can connect via the unix type socket "C:/xampp/mysql/mysql.sock" or call mysql_options() with MYSQL_OPT_PROTOCOL and set the protocol to MYSQL_PROTOCOL_TCP before you call mysql_real_connect().
Hi RobotGuy,

thank you for your advice. Unfortunately, it didn't work.
I was wondering, could a version mismatch between the MySQL server and MySQL client cause the problem?
Shouldn't be a version mismatch as as far as I know both the server and client libraries are all backwardly compatible and I believe negotiate an appropriate protocol version between themselves. At least, I've never had to change the library I link against when changing the server version. I suppose if the versions where really far apart there could be a problem but you'd really have to work at it.

As a suggestion, try changing the host parameter ("localhost") to your real IP address (i.e. not 127.0.0.1) and see if it works (or at least gives a different error message). If so then it is some issue with the library attempting to talk via the unix socket and it isn't working.

Out of interest, can you connect with the mysql.exe program?

cd C:/xampp/mysql/binmysql.exe --user=root --password= abelcorver_com


Thanks a lot for your help.
I've tried connecting to MySQL on the command line, and everything worked fine.

I've just converted my .exe project to a .lib project, so that I can use it in a browser plugin. I need to reach a point again, where I'll be able to incorporate the MySQL database calls. Unfortunately, until then, I can't test out your other suggestion. I'll get back to you asap, hopefully within 2 days.

Thanks again for your help, I really appreciate it!
Wow, it took me a bit longer than I expected (understatement) =D.

However, today I was able to try out your suggestions, and unfortunately, they didn't work. I still got the following message:

Quote:Can't connect to MySQL server on '84.86.162.41' (0)


Any other suggestions?
I must admit I'm beginning to run out of ideas, are you sure that mysql has networking enabled?

As you said that the command line tool connected, try:
mysql.exe --user=root --password= --host=localhost --protocol=TCP abelcorver_com


This will try to connect via network sockets and if it fails when it worked without the host and protocol parameters then I'd have to be pretty sure that mysql isn't listening on the network.

If so then you're going to have to alter your my.cnf to allow networking (check the mysql manual and make sure that skip-networking isn't included) or connect some other way. See if mysql is listening on a named pipe with:
mysql.exe --user=root --password= --protocol=PIPE abelcorver_com


If that works then either (and I have no experience with this as I use Unix) use "." instead of "localhost" in the call to mysql_real_connect or call mysql_options with MYSQL_OPT_NAMED_PIPE before the connect call.

If none of this works then I'm afraid I'm out of ideas. Does XAMPP have a forum? They would probably have a better idea about the default configuration of mysql that is used.
Both command lines you suggested worked. However, my application still doesn't. However, I've had a change of mind now that I realise that directly connecting to a database from the client (which would someone need to know the password), is extremely unsafe. I'll try a socket connection to a PHP script instead.

I'd still really like to know what's actually wrong here, so if any of you guys know what's been causing the headaches the past 1.5 week or so, feel free to let me know =P

Robotguy, thanks again for your efforts!

Regards,
Abel.

This topic is closed to new replies.

Advertisement