mysql遠端連線錯誤1130的解決方法
轉載:https://my.oschina.net/maliang0130/blog/207823
mysql中的user表中User = mysql.sys 對應的Host = localhost,這樣才可以通過mysql -u root -p 登陸
解決遠端連線mysql錯誤1130程式碼的方法
今天在用遠端連線Mysql伺服器的資料庫,不管怎麼弄都是連線不到,錯誤程式碼是1130,ERROR 1130: Host 192.168.2.159 is not allowed to connect to this MySQL server
猜想是無法給遠端連線的使用者許可權問題。結果這樣子操作mysql庫,即可解決。在本機登入mysql後,更改 “mysql” 資料庫裡的 “user” 表裡的 “host” 項,從”localhost”改稱'%'。。
mysql -u root -p
mysql>use mysql;
mysql>select 'host' from user where user='root';
mysql>update user set host = '%' where user ='root';
mysql>flush privileges;
mysql>select 'host' from user where user='root';
第一句是以許可權使用者root登入
第二句:選擇mysql庫
第三句:檢視mysql庫中的user表的host值(即可進行連線訪問的主機/IP名稱)
第四句:修改host值(以萬用字元%的內容增加主機/IP地址),當然也可以直接增加IP地址
第五句:重新整理MySQL的系統許可權相關表
第六句:再重新檢視user表時,有修改。。
重起mysql服務即可完成。
==============================================================
本機的mysql資料庫中有兩條user='root'的記錄,將其中一條host='localhost'的host修改為'%'後,雖然可以通過遠端訪問資料庫了,但是使用localhost或者127.0.0.1又無法訪問資料庫了。
經過一番折騰,發現可以通過建立使用者的方法來解決這個問題。不需要修改user表中的任何資料,在本地用root登陸mysql後,執行下面的語句,建立使用者名稱為'root',密碼為'123456'的使用者。執行完以後,檢視user表,發現新增了一條host='%',user='root'的記錄,並且各項許可權與其它'root'一樣,再次使用192.168.1.13進行訪問,發現可以正常訪問了。
grant allon
*.*to rootidentified by '123456'
with grant option;
如果訪問還有問題,可以執行一下flush privileges;
================================================================
授權法。例如,你想myuser使用mypassword從任何主機連線到mysql伺服器的話。
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED
BY 'mypassword' WITH GRANT OPTION;
如果你想允許使用者myuser從ip為192.168.1.3的主機連線到mysql伺服器,並使用mypassword作為密碼
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.0.1' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
給使用者授權
我用的使用者是root 密碼123456
首選語法為:
Sql程式碼
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
示例:
Sql程式碼
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
執行完後,再
mysql:flush privileges;
重新整理一下許可權就可以了,不用重啟
====================================================================
遠端連線MySQL沒有許可權的原因有兩種,一個是因為MySQL的限制,一個是防火牆的限制。
1,解決防火牆限制:
在MySQL服務主機上將防火牆關閉或者在防火牆高階設定裡面加入出入站規則,加上MySQL的埠,允許通過MySQL的埠進行訪問主機。
2,解決MySQL的限制,在MySQL服務主機上執行下列sql
-- 更改root使用者的登入主機為任意主機
UPDATE USER
SET Host = '%'
WHERE User = 'root';
-- 更改許可權(root為賬戶名,%為主機名(任意主機),525099302為密碼)
GRANT ALL PRIVILEGES ON *.* TO [email protected]'%' IDENTIFIED BY "525099302";
-- 重新整理許可權,mysql直接生效
flush privileges;
將上述兩個限制突破,就可以通暢無阻地遠端訪問mysql主機了。
=======================================================================
- Open up the file under 'etc/mysql/my.cnf'
-
Check for:
- port (by default this is 'port = 3306')
- bind-address (by default this is 'bind-address = 127.0.0.1'; if you want to open to all then just comment out this line. For my example, I'll say the actual server is on 10.1.1.7)
-
Now access the MySQL Database on your actual server (say your remote address is 123.123.123.123 at port 3306 as user 'root' and I want to change permissions on database 'dataentry'. Remember to change the IP Address, Port, and database name to your settings)
mysql -u root -p Enter password:<enter password> mysql>GRANTALLON*.*to root@'123.123.123.123' IDENTIFIED BY'put-your-password'; mysql>FLUSH PRIVILEGES; mysql>exit
-
sudo service mysql restart
- You should now be able to remote connect to your database. For example, I'm using MySQL Workbench and putting in 'Hostname:10.1.1.7', 'Port:3306', 'Username:root'