Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Connect to MySQL Server on IPAddress (10061)

Tags:

php

mysqli

I need to connect to a remote MySQL database and have created the following connection code:

<?php
/* Set Variables */
$host="myipaddress";
$db="mydbname"; 
$username="dbuser";
$pass="mypass";

/* Attempt to connect */
$mysqli=new mysqli($host,$username,$pass,$db);
if (mysqli_connect_error()){
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
echo 'Success... ' . $mysqli->host_info . "\n";

$mysqli->close();

}
?>

For security reasons, I've not provided the actual variable values. When I run this on my development system, I receive

Connect Error (2003) Can't connect to MySQL server on 'myipaddress' (10061)

My PHP is a bit rusty, can someone identify where my code is faulty? Note that dbuser has select, insert and update privileges on the database name set as the variable.

Thanks, Sid

Edit I made changes to my.cnf and restarted mysql. I now receive access denied for user 'dbuser'@'mycurrenthostname' (using password YES). When I use mysql -u dbuser -p from command line, I can login. I granted insert, update and select to dbuser with host '%' so that dbuser could connect from anywhere.

I've read the MySQL Reference guide about this error, but am still stuck. Is there a problem with my code, now that my.cnf has been fixed?

like image 809
SidC Avatar asked Aug 19 '10 05:08

SidC


People also ask

Can't connect to MySQL server on localhost 3306 10061 No connection could be made because the target machine actively refused it?

The error above means that your request to connect to the MySQL server has been refused. There are two things you need to check to fix this error: Make sure that your MySQL service is actually running. Check that you are attempting to connect to the right port number.

Can't connect to MySQL server on remote host?

To allow remote access to MySQL, you have to comment out bind-address (you did) and skip-networking in the configuration file. Next, you have to make sure the user is allowed remote access. Check your user with this: SELECT User, Host FROM mysql.

Can't connect to server on localhost?

What Is the Localhost Refused to Connect Error? The localhost simulates a web server running on your computer. When the “localhost refused to connect” error appears, it is likely due to misconfigured port. Other common reasons include insufficient permissions and the Apache webserver not running properly.


2 Answers

Check that your firewall is allowing connections through on port 3306.

Check the MySQL configuration parameter bind-address in my.cnf to ensure that it is allowing remote connections.

There's information and troubleshooting tips here:

http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html

like image 172
Mike Avatar answered Oct 23 '22 23:10

Mike


According to the MySql documentation , The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, and that the network port you specified is the one configured on the server.

I'm not familiar with php , but the problem might not be in your code.

like image 38
amal Avatar answered Oct 24 '22 00:10

amal