Solved

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

Posted on 2007-11-28
23
6,314 Views
Last Modified: 2013-12-12
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
Comment
Question by:galbarnissan
  • 9
  • 7
  • 6
  • +1
23 Comments
 
LVL 17

Expert Comment

by:nplib
Comment Utility
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
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
 
LVL 20

Expert Comment

by:steelseth12
Comment Utility
Make sure that the mysql server is running and that windows firewall is not blocking it.
0
 

Author Comment

by:galbarnissan
Comment Utility
to steelseth12:

mysql is running but how do I check the firewall?
0
 

Author Comment

by:galbarnissan
Comment Utility
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
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
 

Author Comment

by:galbarnissan
Comment Utility
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
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
 

Author Comment

by:galbarnissan
Comment Utility
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
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 21

Accepted Solution

by:
nizsmo earned 250 total points
Comment Utility
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
 
LVL 20

Expert Comment

by:steelseth12
Comment Utility
Hi galbarnissan.
Try connecting on 127.0.0.1 instead of localhost
0
 

Author Comment

by:galbarnissan
Comment Utility
how do I check that mysql is running on the correct port?
how do I open the port?
0
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
[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
 

Author Comment

by:galbarnissan
Comment Utility
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
 
LVL 20

Expert Comment

by:steelseth12
Comment Utility
3306 is the default but you can connect on other ports as well.

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

will connect on 3307
0
 

Author Comment

by:galbarnissan
Comment Utility
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
 
LVL 20

Expert Comment

by:steelseth12
Comment Utility
If it automatically changed 3306 to 3307 then it might be because another service was already using 3306
0
 
LVL 20

Expert Comment

by:steelseth12
Comment Utility
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
 

Author Comment

by:galbarnissan
Comment Utility
how can I find out if  I have another service on 3306?
0
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 250 total points
Comment Utility
open a new command window

and type netstat -ab to see all active connection and which port they are using
0
 

Author Comment

by:galbarnissan
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now