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

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?
0
alex_wareing
Asked:
alex_wareing
  • 3
  • 2
  • 2
  • +6
1 Solution
 
paulp75Commented:
make sure you have set priveledges for the user to use that database in phpmyadmin
0
 
mankowitzCommented:
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.
0
 
rhyspCommented:
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.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
alex_wareingAuthor Commented:
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);
}
?>
0
 
sakuya_suCommented:
probably because localhost is not allowed to your user?
0
 
cschandCommented:
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
0
 
sakuya_suCommented:
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
0
 
ebosscherCommented:
did they change the port?
0
 
rhyspCommented:
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.
0
 
hpetCommented:
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.
0
 
Suat OzgurWeb / Application DeveloperCommented:
>H:\myfile\myfolder\index.php

You are trying to access a mysql server from your local computer. And you need to setup a MySQL user that would be a remote user and granted by the MySQL server.

To see if you can do this or not, please go to PhpMyAdmin Home -> Privileges. If you can't see Privileges that means you are not allowed to setup a user. If you can see Privileges then click on the link and see if you have "Add New User" link in the Privileges page. If not, then again, you are not allowed by host for this operation.

If you can see Add New User then click on it. Enter a username (anything except existing users), then you need to specify a host for remote allowance. Normally we set this up as "localhost" which would work only for the same server that your scripts are running. However, in your case, you need to setup an IP number (if you have a static IP) or you need to allow all remote requests (%) by selecting Any Host.

Finally you need to enter your password twice and without setting anything else, Click Go. Now you have a new user and you just need to assign a database for this user.

In the leading page after save, you should see "Database-specific privileges". Select the database that you want to grant user and select the options that you want to grant user. For example, I would select all "Data" privileges like SELECT, INSERT, UPDATE and DELETE. Click go.

What I explained above is about how to create a remote user for MySQL server by using phpmyadmin and you can do this only if your hosting company lets remote database access.

At the other side, your script will work when you upload your script to the server and change server variable as "localhost" or "127.0.0.1" instead.

I hope it helps.
Suat
0
 
rhyspCommented:
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.
0
 
ebosscherCommented:
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
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
  • 2
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now