MySQL8.0允許外部訪問


繼上一篇安裝mysql 后外部訪問mysql 時會有日下錯誤提示

 

 下面就來解決一下

1,登進MySQL,使用如下命令

mysql -uroot -p

#輸入密碼
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

 

2,輸入以下語句,進入mysql庫:

use mysql;

3,更新域屬性,'%'表示允許外部訪問:

mysql> update user set host='%' where user ='root';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

4,執行以上語句之后再執行:

FLUSH PRIVILEGES;  #刷新權限

5,再執行授權語句:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION;
Query OK, 0 rows affected (0.02 sec)

然后外部就可以通過賬戶密碼訪問了。

 

 

 

6,其它說明:

FLUSH PRIVILEGES; 命令本質上的作用是:

將當前user和privilige表中的用戶信息/權限設置從mysql庫(MySQL數據庫的內置庫)中提取到內存里。

MySQL用戶數據和權限有修改后,希望在"不重啟MySQL服務"的情況下直接生效,那么就需要執行這個命令。

通常是在修改ROOT帳號的設置后,怕重啟后無法再登錄進來,那么直接flush之后就可以看權限設置是否生效。

而不必冒太大風險。

 

三、可能存在的其它問題:

執行完之后,再用Navicat連接mysql,報錯如下:

Client does not support authentication protocol requested by server;

 

報錯原因:

mysql8.0 引入了新特性 caching_sha2_password;這種密碼加密方式Navicat 12以下客戶端不支持;

Navicat 12以下客戶端支持的是mysql_native_password 這種加密方式;

解決方案:

1,用如下語句查看MySQL當前加密方式

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host      | user             | plugin                |
+-----------+------------------+-----------------------+
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)

看第一行,root加密方式為caching_sha2_password。

2,使用命令將他修改成mysql_native_password加密模式:

update user set plugin='mysql_native_password' where user='root';

再次連接的時候,就成功了。

四、如果還連接不上

通過以上操作后,依然無法連接上,問題可能出在了防火牆上。

1,MySQL部署在實體服務器上解決方案如下:
a.開放MySQL的端口號,默認端口號是3306。

參照防火牆命令7、8
b.直接關閉防火牆(慎重操作,不建議。)

參照防火牆知識 2、3

五、防火牆相關知識

Centos7默認安裝了firewalld,如果沒有安裝的話,可以使用

 yum install firewalld firewalld-config

進行安裝。

1:查看防火狀態

systemctl status firewalld

service  iptables status

 

2:暫時關閉防火牆

systemctl stop firewalld

service  iptables stop

 

3:永久關閉防火牆

systemctl disable firewalld

chkconfig iptables off

4:打開防火牆

systemctl enable firewalld

service iptables restart  

5、將接口添加到區域(默認接口都在public)

firewall-cmd --zone=public --add-interface=eth0(永久生效再加上 --permanent 然后reload防火牆)

6、查看指定區域所有打開的端口

firewall-cmd --zone=public --list-ports

7、在指定區域打開端口(記得重啟防火牆)

firewall-cmd --zone=public --add-port=8080/tcp(永久生效再加上 --permanent)

說明:

–zone 作用域
–add-port=8080/tcp 添加端口,格式為:端口/通訊協議
–permanent #永久生效,沒有此參數重啟后失效

8、重啟防火牆

firewall-cmd --reload

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM