1.查看linux防火牆是否開放3306端口
執行iptables -nL --line-number
這裏顯示DROP代表防火牆阻止了3306端口。
2.添加防火牆例外
執行vim /etc/sysconfig/iptables
3.重啓防火牆
執行service iptables restart
查看是否變爲ACCEPT
4.創建遠程連接用戶並授權
mysql> use mysql
mysql> create user test identified by '123456';
授權
1、修改localhost
更改 "mysql" 數據庫裏的 "user" 表裏的 "host" 項,從"localhost"改成"%"
mysql>use mysql;
mysql>update user set host = '%' where user = 'test';
mysql>select host, user from user;
mysql>FLUSH PRIVILEGES;
2、指定授權
使用test/123456從任何主機連接到mysql服務器:
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
使用test/mypassword從ip爲192.168.2.42的主機連接到mysql服務器:
GRANT ALL PRIVILEGES ON *.* TO '123456'@'192.168.2.42' IDENTIFIED BY '123456' WITH GRANT OPTION;
3、泛授權
mysql -h localhost -u root
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; //賦予任何主機上以root身份訪問數據的權限
mysql>FLUSH PRIVILEGES;
修改用戶密碼
mysql>update mysql.user set password=password('新密碼') where User="test" and Host="%";
mysql>FLUSH PRIVILEGES;
刪除用戶
delete from user where User='test' and Host='localhost';
附:Mysql無法遠程連接的常見問題
1)查看Mysql的端口是否正確,通過netstat -ntlp查看端口占用情況,一般情況下端口是3306。
2)報錯:ERROR 2003 (HY000): Can’t connect to MySQL server on ’192.168.51.112′ (111)
查看/etc/my.cnf中,skip-networking 是否已被注掉,需要注掉。
3)報錯:ERROR 2003 (HY000): Can’t connect to MySQL server on ’192.168.51.112′ (113)
查看是否iptables沒有允許mysql連接,通過:service iptables stop臨時關閉測試是否可以正常遠程訪問,如果可以,按上面方面設置iptable允許3306端口
4)遠程訪問mysql速度很慢的解決方法
修改/etc/my.cnf或my.ini
[mysqld]下添加
skip-name-resolve
skip-grant-tables
希望此文能夠幫助到您!