1. 程式人生 > >MYSQL ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.10.210' (111) 解決方法

MYSQL ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.10.210' (111) 解決方法

ges -a data more name working 其他 如果 puts

今天在測試MySQL的連接時候,發現連接不通過,並報錯ERROR 2003 (HY000): Can‘t connect to mysql server on ‘192.168.10.210‘ (111)
測試代碼:

require ‘mysql2‘
client = Mysql2::Client.new(:host=>"192.168.10.210",:username=>‘root‘,:password=>"root")
puts results = client.query("show databases;")

谷歌了一下之後,原來是在mysql的my.cnf中有下面一段代碼:

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address           = 127.0.0.1  #這裏默認監聽本地localhost

如果要讓mysql監聽到其他的地址,可以將bind-address = 127.0.0.1註釋掉。
或者將bind-address = 0.0.0.0監聽所有的地址

屏蔽掉之後再次運行代碼又出現:Host ‘192.168.10.83‘ is not allowed to connect to this MySQL server
技術分享圖片
解決方法:
如果想讓192.168.10.83能夠連接到本地的這個數據庫,要讓數據庫給其分配權限,登錄mysql,執行:(username 和 password是登錄mysql的用戶名和密碼)

GRANT ALL PRIVILEGES ON *.* TO ‘username‘@‘192.168.10.83‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;

如果要想所有的外部ip地址都能夠訪問使用mysql,可以執行下面:

GRANT ALL PRIVILEGES ON *.* TO ‘username‘@‘%‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;

之後執行刷新數據庫:

flush privileges;

如果要查看用戶的權限,可以執行:

> show grants for ‘root‘@192.168.10.83

技術分享圖片

MYSQL ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.10.210' (111) 解決方法