Link to home
Start Free TrialLog in
Avatar of alex_wareing
alex_wareing

asked on

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

I'm creating a web app and i using a new host, i have created a very simple PHP page that connects to the database grabs all the rows from one table and prints them to a page. I have used this same test code many times i know the code is fine, in fact its pretty much the basic PHP MySQL connection code you'll find in any simple tutorial. However on my new host i get the following error messages:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in H:\myfile\myfolder\index.php on line 27

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in H:\myfile\myfolder\index.php on line 28
Database NOT Found
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in H:\myfile\myfolder\index.php on line 54

Where is this error coming from, could it be the users and permissions in MySQL. I am very new to MySQL i am using PHPMyAdmin. I don't really know how to setup user correctly to access data from PHP pages. Could wrong user settings be the problem or is this error something else?
Avatar of paulp75
paulp75
Flag of Australia image

make sure you have set priveledges for the user to use that database in phpmyadmin
Avatar of mankowitz
Fire up the MySQL Query Browser, and see if your login ID and password are correct If they are, then you may have a problem in your php code (let's see it).

If you can not log in, try logging in as root. If that doesn't work, make sure the MySQL server is actually running.

If you can log in as root, but not as the specified usrname, then make sure that the username exists in mysql.
Avatar of rhysp
rhysp

Very odd. Usually if it was username or password related it would say. 10061 indicates a network error. Check with your host that you should be using localhost as the server. You might also want to try 127.0.0.1 instead to force it to use TCP/IP instead of local sockets. (If you are using something other than 127.0.0.1 or localhost, try using localhost, then 127.0.0.1)

Of course, it is also possible that your host's MySQL server was down. Check the service status pages (you'll have to ask your host where to find them) to monitor the status of key services.
Avatar of alex_wareing

ASKER

I don't think the server is down because i can access the PHPMyAdmin and i don't know why there is a problem with the localhost because i am using an address for the server, here is my code:
<?php
function db_connect() {
      $user_name = "myusername";
      $password = "myPassw0rd";
      $database = "my_database";
      $server = "mysql.myhost.co.uk";
      
      $db_handle = mysql_connect($server, $user_name, $password);
}
?>
probably because localhost is not allowed to your user?
If your phpmyadmin works... please browse any database or table in it.
Then check the path showing in the top

just like
  Server: localhost  -   Database: test or
  Server: myserver  -   Database: mytest test or something...

Take the name of server then test with that name
normally third party hosting companies would never give you access to localhost, they all instruct you to access your databases through your own domain name
did they change the port?
Change
   $server = "mysql.myhost.co.uk";
to
   $server = "localhost";

My experience with hosting companies is that you always use localhost (which is in contrast to sakuya_su's observation).

The problem is still a connection one. It isn't rejecting the username and password it is simply denying the connection entirely (before you even have the chance to submit a username/password).
http://dev.mysql.com/doc/refman/5.0/en/can-not-connect-to-server.html

If you continue to have troubles, I'd recommend contacting your hosting provider or checking out their website/forums.
Check what PHP extension are you using to access DB.
If you are mixing mysqli (improved) and mysql you may get errors like "invalid link resource".
If you use mysqli, you must use mysqli_select_db, ... etc.
ASKER CERTIFIED SOLUTION
Avatar of Suat M. Ozgur
Suat M. Ozgur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
smozgur makes a good point. In fact, if you are trying to access the server remotely (from your development machine) it may not accept any connections from the outside world.
ummm... the other thing you could do, rather than creating a remote user is to tunnel into the server using the port-forwarding capabilities on a tool like PuTTy