天天看點

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)​

​ 

測試代碼:

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​

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

解決方法: 

如果想讓​

​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) 解決方法