今天在測試MySQL的連接時候,發現連接不通過,並報錯ERROR 2003 (HY000): Can't connect to mysql server on '192.168.10.210' (111) 測試代碼: 谷歌了一下之後,原來是在mysql的my.cnf中有下麵一段代碼: 如果要讓mys ...
今天在測試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