Solved

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

Posted on 2007-11-28
23
6,322 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
ID: 20368834
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
ID: 20369048
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
ID: 20370763
Make sure that the mysql server is running and that windows firewall is not blocking it.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:galbarnissan
ID: 20371917
to steelseth12:

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

Author Comment

by:galbarnissan
ID: 20371931
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
ID: 20371978
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
ID: 20372059
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
ID: 20372130
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
ID: 20372133
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
ID: 20372193
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
ID: 20372210
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
 
LVL 21

Accepted Solution

by:
nizsmo earned 250 total points
ID: 20372215
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
ID: 20372216
Hi galbarnissan.
Try connecting on 127.0.0.1 instead of localhost
0
 

Author Comment

by:galbarnissan
ID: 20372346
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
ID: 20372435
[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
ID: 20372483
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
ID: 20372513
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
ID: 20372552
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
ID: 20372560
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
ID: 20372561
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
ID: 20372663
how can I find out if  I have another service on 3306?
0
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 250 total points
ID: 20372816
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
ID: 20376893
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

821 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