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)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

你可能感兴趣的:(Docker部署mysql远程连接 解决2003的问题)