今天在測試MySQL的鏈接時候,發現鏈接不經過,並報錯ERROR 2003 (HY000): Can't connect to mysql server on '192.168.10.210' (111)
測試代碼:mysql
require 'mysql2' client = Mysql2::Client.new(:host=>"192.168.10.210",:username=>'root',:password=>"root") puts results = client.query("show databases;")
谷歌了一下以後,原來是在mysql的my.cnf中有下面一段代碼:sql
# 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的用戶名和密碼)less
GRANT ALL PRIVILEGES ON *.* TO 'username'@'192.168.10.83' IDENTIFIED BY 'password' WITH GRANT OPTION;
若是要想全部的外部ip地址都可以訪問使用mysql,能夠執行下面:(此處是對root用戶受權mysql全部數據庫)測試
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
若是針對某個特定的用戶test受權某個指定的數據testdb用下面例子中的命令ui
mysql>GRANT ALL PRIVILEGES ON `testdb`.* TO 'test'@'%' IDENTIFIED BY 'newpwd' WITH GRANT OPTION;
以後執行刷新數據庫:this
flush privileges;
若是要查看用戶的權限,能夠執行:spa
> show grants for 'root'@192.168.10.83