• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6407
  • Last Modified:

Can't connect to MySQL server on 'localhost' (10061)

I know this question was asked many times before but I couldn't find a solution that works for me.
I've installed an apache server (2.2.6), PHP (5.2) and MySQL (5.0.45) in my windows XP SP2.
I've configured everything as needed (or at least I think so)
and tested my MySQL with a small php page on the localhost  but the browser showed the error:

"Can't connect to MySQL server on 'localhost' (10061)"

I've tried many things but and now I'm pretty stuck.  

thanks.  

gal







<?php
 
# Define MySQL Settings
define("MYSQL_HOST", "localhost");
define("MYSQL_USER", "root");
define("MYSQL_PASS", "galbarn");
define("MYSQL_DB", "test");
 
$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());
mysql_select_db("".MYSQL_DB."",$conn) or die(mysql_error());
 
$sql = "SELECT * FROM test";
$res = mysql_query($sql);
 
while ($field = mysql_fetch_array($res))
{
$id = $field['id'];
$name = $field['name'];
 
echo 'ID: ' . $field['id'] . '<br />';
echo 'Name: ' . $field['name'] . '<br /><br />';
}
 
?>

Open in new window

0
galbarnissan
Asked:
galbarnissan
  • 9
  • 7
  • 6
  • +1
2 Solutions
 
nplibCommented:
have you tried something simple like
$conn = mysql_connect("sever", "root", "password") or die(mysql_error());
instead of using the define constant variables method
0
 
nizsmoDeveloperCommented:
hi galbarnissan:

Looks like something wrong with your setup, as it looks like you cannot connect to your mysql server.

My suggestion would be to use a very easy 1 click installer which installs everything and auto-configures everything for you so that you know php and mysql will work.

http://www.wampserver.com/

WAMP will install MySQL database, PHP, Apache and phpmyadmin along with a few more which is very useful, does not take much configuration at all, and works after the install.

Hope this helps.
0
 
steelseth12Commented:
Make sure that the mysql server is running and that windows firewall is not blocking it.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
galbarnissanAuthor Commented:
to steelseth12:

mysql is running but how do I check the firewall?
0
 
galbarnissanAuthor Commented:
to nplib:

the same error happened when I've tried to install drupal so I believe it's not a coding problem

to nizsmo :

if nothing would work I'll do that.
0
 
nizsmoDeveloperCommented:
galbarnissan,

to check the firewall it is dependent on what firewall software you have.

If you do end up trying WAMP or similar, you will be surprised how easy and quick it is to setup everything.

Goodluck!
0
 
galbarnissanAuthor Commented:
I just want to try to fix it manually before I install wamp or similar.
I have the windows firewall. why should it bother my MySQL if everything is on the local machine
(no remote computers involved)?
0
 
nizsmoDeveloperCommented:
In a way you are correct, it shouldn't, and I haven't had any problems myself.

Just to be sure though, you can go to Control Panel>>Windows Firewall
and click on the "exceptions" tab. You should be able to see the "APACHE HTTP Server" on the list (should be near the first on the list as it is alphabetically ordered). Also you can check out what other programs are on the list, I don't see MySQL on my exceptions list, but my PHP MySQL connects fine, so I suspect only Apache HTTP Server has to be there.

If it is still not fixed, you can try and uninstall and reinstall just MySQL and see if this fixes the issue.
0
 
nizsmoDeveloperCommented:
Also make sure your mysql service is running in the background.

Go to Start Menu>>Run
and type in services.msc
in the services list try and look for the mysql service, it should be named something like ..mysqld...
Make sure it is actually started, if not try and start it and then try to connect again.
0
 
galbarnissanAuthor Commented:
there is an exception for "APACHE HTTP Server" in the exceptions tab in my firewall
and mySQL is running (automatic) in the services.msc
perhaps it's something in the my.ini in the MySQL folder?
should I enabling networking there?
0
 
nizsmoDeveloperCommented:
Hmmm very strange.
Please try the things in here and let me know how you go:
http://www.experts-exchange.com/Databases/Mysql/Q_20737391.html
0
 
nizsmoDeveloperCommented:
If above link doesn't help, see if this will:
http://forge.mysql.com/wiki/Error2003-CantConnectToMySQLServer

The main thing to check for at this stage is that MySQL is running on the correct port.
0
 
steelseth12Commented:
Hi galbarnissan.
Try connecting on 127.0.0.1 instead of localhost
0
 
galbarnissanAuthor Commented:
how do I check that mysql is running on the correct port?
how do I open the port?
0
 
nizsmoDeveloperCommented:
[snippet from link above]
If you can't connect, you can still find the port number by checking the my.ini or my.cnf configuration file on the server. Look for the port option within the [mysqld] section:

[mysqld]
...
port=3306
...

The port can also be specified on the command-line. This overrides the entry in the options file. So double-check the arguments to the mysqld program, which you can see in the ps output. You can also read the output of netstat to see the port that the server process is listening to.
0
 
galbarnissanAuthor Commented:
In my my.ini in the c://program files/MySQL/MySQL server 5.0
it's written:

# CLIENT SECTION
....
[client]

port=3307

[mysql]

and

#server section
...
[mysqld]

# The TCP/IP Port the MySQL Server will listen on
port=3307


should I change it to 3306?
0
 
steelseth12Commented:
3306 is the default but you can connect on other ports as well.

define("MYSQL_HOST", "localhost:3307");

will connect on 3307
0
 
galbarnissanAuthor Commented:
O.K but if I don't want to write every time  "...localhost:3307" (or if want to use some CMS like drupal)
should I change it to 3306?
another thing:
when I configured MySQL (with the configuration wizard) I kept it 3306 so why it changed to 3307?
0
 
steelseth12Commented:
If it automatically changed 3306 to 3307 then it might be because another service was already using 3306
0
 
steelseth12Commented:
If you are sure you dont have another service on 3306 then change the mysql port to 3306 if you are not sure then leave it as it is. You can NOT have 2 services on the same port.
0
 
galbarnissanAuthor Commented:
how can I find out if  I have another service on 3306?
0
 
steelseth12Commented:
open a new command window

and type netstat -ab to see all active connection and which port they are using
0
 
galbarnissanAuthor Commented:
I think I managed to fix the problem.
I don't know why but the port configuration changed from 3306 to 3307 so I had just had to change it back
thank you steelset12 and nizsmo you've helped me a lot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 9
  • 7
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now