MySQL Error 111 Can't connect to server

浪尽此生 提交于 2019-12-24 13:06:39

问题


I have a connection.php file that is suppose to connect to a remote database. Here is the code:

<?php
try {
    $conn = new PDO('mysql:host=IP;port=PORT;dbname=DBNAME', 'USERNAME', 'PASSWORD');
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
} 
?>

Now all my variables I believe are correct since I can connect to the database through Toad. I used this same PDO format for my own database connection through localhost and it works fine. I am not sure what the problem is. Since i can use Toad i believe that the server already allows remote access to it, but i am not sure on that. Any input would be nice.

Also this is the Error that PDOException is coming back with:

SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'IP' (111)

回答1:


111 means connection refused,

It probably means that your MySQL server is only listening the localhost interface.

If you have lines like this :

skip-networking
bind-address = 127.0.0.1

You should comment them In your my.cnf configuration file (add a # at the beginning of the lines), and restart MySQL.




回答2:


well, in you /etc/mysql/my.cnf : you should find out this:

skip-networking
bind-address = 127.0.0.1

You gotta comment the first one, and change the ip to 0.0.0.0 or to the ip of your remote host that you want to allow (recommended) Also, you may don't have a user to connect to that host, i mean, mysql users are like 'root'@'192.168.1.1', so you may try to access with a user that doesn't exists. Try to create it and then grant to it all privilegies. Just execute this couple commands on mysql:

CREATE USER 'root'@'192.168.1.100' IDENTIFIED BY '***'; GRANT ALL PRIVILEGES ON * . * TO 'root'@'192.168.1.100' IDENTIFIED BY '***';

Also, make sure that mysql is listening to in the right port, if using linux:

netstat -tlpn

should do the work, at least that's how i fixed similar problems.



来源:https://stackoverflow.com/questions/19219338/mysql-error-111-cant-connect-to-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!