hi.
i am senthilmurugan. i am learning to php language.
one error occur. i try to connect to database.
Can't connect to MySQL server on 'localhost' (10061). how to correct the error .
please reply the answer...

Dani AI

Generated

Quick troubleshooting note for , and others: the error "Can't connect to MySQL server on 'localhost' (10061)" is a low-level connection refusal — the PHP client cannot reach any MySQL process at the target host/port. Common causes are the MySQL service not running, a firewall blocking port 3306, the server bound to a different address, or the hosting environment using a different DB host. was right to flag credentials, but verifying server reachability should come first.

Checklist to work through (platform-appropriate):

  • Confirm the MySQL server process is running (Windows Services or systemctl status mysqld / service mysql status).
  • Verify MySQL is listening on the expected port/address: netstat -an | find "3306" (Windows) or ss -ltnp | grep 3306 (Linux).
  • Force a TCP test (avoids socket vs named-pipe ambiguity): try telnet 127.0.0.1 3306 or the mysql client mysql -u user -p -h 127.0.0.1 -P 3306.
  • If the server is remote (shared hosting), check the provider control panel for the correct DB host name — as noted, many hosts do not use "localhost" or allow external connections.
  • Check my.cnf/my.ini for bind-address and ensure firewall/antivirus rules allow connections. On Windows, check the MySQL Windows service name and the firewall settings.

A brief, modern PHP example using mysqli (forces TCP via 127.0.0.1):

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

Additional notes: enabling the correct PHP MySQL extension in php.ini (or using mysqli/PDO) and confirming the DB user has permission to connect from the host in mysql.user are common follow-ups. Avoid using root for web apps and prefer mysqli or PDO with prepared statements; the old mysql_* extension has been removed from modern PHP.

Recommended Answers

All 5 Replies

Your Database information is incorrect
In order to connect you will need to have the valid information for;

DB Name
DB Username
DB Userpass
DB Host

One of these is incorrect.

hallo ppl,

i also have the same problem,

my php connection with MySQL is not working, and couldnt connect to server is displayed y my PHP

should i alter PHP's php.ini to access mysql?

is there any restriction that i have to store the Mysql directly in my C drive... if so i have currently installed in C:\Program Files\MySQL\MySQL Server 4.1

how can i change?

thanks in advance


this is what i did

<?php
 // Connect to the database
 $dbhost = 'localhost';
 $dbusername = 'testuser';
 $dbpasswd = 'testpassword';
 $database_name = 'simple';
 $connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")
  or die ('Couldn\'t connect to server.');
 $db = mysql_select_db("$database_name", $connection)
  or die('Couldn\'t select database.');

 // Generate SQL code to store data on database.
 $insert_sql = 'INSERT INTO simple_table (text) VALUES (\'test text, 1,2,3\')';

 // Execute SQL code.
 mysql_query( $insert_sql )
  or die ( 'It Didn\’t Work: ' . mysql_error() );

 // Tell User we are done.
 echo 'Code Inserted';
?>

Create a new file called "test_select_mysql.php" and enter the following code into it:

<?php
 // Connect to the database
 $dbhost = 'localhost';
 $dbusername = 'testuser';
 $dbpasswd = 'testpassword';
 $database_name = 'simple';
 $connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")
  or die ('Couldn\'t connect to server.');
 $db = mysql_select_db("$database_name", $connection)
  or die('Couldn\'t select database.');

 // Generate code to retrieve data from database.
 $select_sql = 'SELECT text FROM simple_table';

 // Retrieve code from database.
 $result = mysql_query( $select_sql )
  or die ( 'It Didn\’t Work: ' . mysql_error() );

 // Display results to user.
 while ( $row = mysql_fetch_object ( $result ) )
 {
  echo $row->text . ‘<br>’;
 }
?>

Browse to http://localhost/test_insert_sql.php, then http://localhost/test_select_sql.php. Every time you view test_insert_sql.php, it adds a line to the database; viewable from test_select_sql.php. Now, even if you reset you computer the data is still stored in the database.

hi!
Just write according to my code.I think that will work.

<?php
$host="localhost";
$user="root"  //It may different
$pass=""    //If u use password that write between " "
$database="test"  //you can also use any any created database name.
@mysql_connect($host,$user,$pass) or die("Sorry ,User name Or Password problem);
@mysql_select_db($database);

//What's you want to do write in here.

//More comple Email me:   <EMAIL SNIPPED>

?>

You might want to check with your hosting company about the MySQL connection. Not all hosting companies allow you to connect via localhost.

With GoDaddy and some others the MySQL database resides on a different server so they use a different sever name.

hi!
Just write according to my code.I think that will work.

<?php
$host="localhost";
$user="root"  //It may different
$pass=""    //If u use password that write between " "
$database="test"  //you can also use any any created database name.
@mysql_connect($host,$user,$pass) or die("Sorry ,User name Or Password problem);
@mysql_select_db($database);

//What's you want to do write in here.

//More comple Email me:   <EMAIL SNIPPED>

?>

Thnx 4 sharin' the code ...I was havin' really havin' hard time connecting to mySql .... by the help of ur code.. my problem has been sorted out.

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.