Docker部署mysql遠程連接 解決2003的問題

連接MySQL

這裡我使用navicat遠程連接,連接MySQL前需要防火墻開放端口或者關閉防火墻。

開放端口

firewall-cmd --add-port=3306/tcp --permanent
// --permanent 永久生效,沒有此參數重啟後失效
firewall-cmd --reload
// 重新載入

關閉防火墻

systemctl stop firewalld
systemctl enable iptables
// 設置開機啟動
firewall-cmd --reload
// 重新載入

firewalld的基本使用

啟動: systemctl start firewalld

關閉: systemctl stop firewalld

查看狀態: systemctl status firewalld

開機禁用 : systemctl disable firewalld

開機啟用 : systemctl enable firewalld

systemctl是CentOS7的服務管理工具中主要的工具,它融合之前service和chkconfig的功能於一體。

啟動一個服務:systemctl start firewalld.service

關閉一個服務:systemctl stop firewalld.service

重啟一個服務:systemctl restart firewalld.service

顯示一個服務的狀態:systemctl status firewalld.service

在開機時啟用一個服務:systemctl enable firewalld.service

在開機時禁用一個服務:systemctl disable firewalld.service

查看服務是否開機啟動:systemctl is-enabled firewalld.service

查看已啟動的服務列表:systemctl list-unit-files|grep enabled

查看啟動失敗的服務列表:systemctl --failed

配置firewalld-cmd

查看版本: firewall-cmd --version

查看幫助: firewall-cmd --help

顯示狀態: firewall-cmd --state

查看所有打開的端口: firewall-cmd --zone=public --list-ports

更新防火墻規則: firewall-cmd --reload

查看區域信息: firewall-cmd --get-active-zones

查看指定接口所屬區域: firewall-cmd --get-zone-of-interface=eth0

拒絕所有包:firewall-cmd --panic-on

取消拒絕狀態: firewall-cmd --panic-off

查看是否拒絕: firewall-cmd --query-panic

補充:Docker部署mysql遠程連接 解決1251

原因:

mysql 8.0 默認使用 caching_sha2_password 身份驗證機制,客戶端不支持新的加密方式。

解決方案:

修改用戶(root)的加密方式

步驟:

1. 進入mysql容器內部

[root@localhost ~]# docker exec -it javakfmysql bash ## javakfmysql是容器的別名,這裡也可以用容器的id代替

2. 登錄mysql

root@e285125c99d6:/# mysql -u root -p

3. 設置用戶配置項

查看用戶信息

mysql> select host,user,plugin,authentication_string from mysql.user; 
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host  | user    | plugin    | authentication_string             |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| %   | root    | caching_sha2_password | $A$005$HF7;krfwhkKHp5fPenQm4J2dm/RJtbbyjtCUVdDCcboXQw3ALxsif/sS1 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys  | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root    | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9        |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+

備註:host為 % 表示不限制ip localhost表示本機使用 plugin非mysql_native_password

修改加密方式

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; ### 123456 mysql的登錄密碼
flush privileges;

然後再查看用戶信息

mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host  | user    | plugin    | authentication_string             |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| %   | root    | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9        |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys  | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root    | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9        |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: