After hosting, i get this error:"Can't connect to MySQL server on 'localhost' (10061)"
The code was working fine in my computer.
I have hosted it in windows platform and i am using mysql and php.
Here is my code for db connection

$connection = mysql_connect(localhost,uname,pword) or die (mysql_error());

        $db = mysql_select_db($databaseName, $connection);
        if (!$db) {
            print "error selecting db--" . mysql_error() . "--" ;
        }

Any help will be appreciated.

Dani AI

Generated

Error 10061 = connection refused (TCP socket could not be opened). This is different from an authentication error — it means the PHP client cannot reach a MySQL listener at the host/port used. That fits ' firewall/port suggestion; a bad password would normally give an "Access denied" error (so 's password idea is less likely to cause 10061). ' point about PHP/MySQL setup is still relevant — verify the extension is actually loaded if other checks pass.

Practical checks (ordered):

  • Confirm a MySQL TCP listener exists: on the server run netstat -an | findstr 3306 (or PowerShell Test-NetConnection -ComputerName 127.0.0.1 -Port 3306). No listener → check my.ini for skip-networking (disable it) or bind-address and restart MySQL.
  • If listening but still refused, inspect Windows Firewall / AV rules for inbound TCP 3306 and open it for the server/process.
  • If the web app is hosted by a provider, confirm whether the database is local or remote. Many hosts require a specific DB host name (not "localhost") or must enable remote MySQL access in the control panel.
  • If phpMyAdmin connects while the site cannot, open phpMyAdmin's config (config.inc.php) to compare host/port/socket settings — phpMyAdmin might be using a socket/named-pipe or a different port.

Force TCP and specify port (modern API example):

$mysqli = new mysqli('127.0.0.1', 'dbuser', 'dbpass', 'dbname', 3306);
if ($mysqli->connect_errno) {
    die('Connect failed: ' . $mysqli->connect_error);
}

Using 127.0.0.1 forces TCP (avoids socket/named-pipe behavior of "localhost"). Also consider moving from deprecated mysql_* to mysqli or PDO for compatibility with newer PHP versions.

Final notes: 10061 is a network-level refusal — focus on listener, port, and firewall first. After a successful TCP connect, authentication/privilege problems show up as "Access denied" and should be handled by checking the MySQL user@host grants.

Recommended Answers

All 7 Replies

10061 means connection refused. Are you sure mysql is running ? Perhaps a firewall won't let you connect. It can be a number of things.

Yes mysql is running. And i have created tables but get errors when i try to access them from the website.

Hello Anony

Other possible reason to error would be different password.
Is it same on your pc & from website?
Is the database named correctly??

Yes the password is correct. i can access it using phpmyadmin. Is it because of user privileges??

User priveledges don't stop users connecting.
has the php-mysql setup completed correctly?

yes it was setup correctly.

Check the config file for phpMyAdmin. Perhaps it is using a different port.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.