拉取镜像
docker pull mysql
创建四个文件夹
运行容器(先不要挂载)
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
复制容器内的my.cnf文件到本地
docker cp mysql:/etc/mysql/my.cnf E:\docker\mysql\conf
删除容器
docker rm -f mysql
重新生成镜像
docker run -p 3366:3306 --name mysql -v E:/docker/mysql/mysql-files:/var/lib/mysql-files -v E:/docker/mysql/logs:/var/log/mysql -vE:/docker/mysql/data:/var/lib/mysql -v E:/docker/mysql/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=123456 -e TZ=Asia/Shanghai -d mysql:latest
进入容器 && 修改远程连接权限
docker exec -it mysql /bin/bash
update user set host='%' where user='root';
如果这个时候直接用mysql客户端工具连接,会报错
连接会报错2059 Authentication plugin 'caching_sha2_password' cannot be loaded
再修改一次
alter user 'root'@'%' identified with mysql_native_password by '123456';
可能会报错(因为mysql5.7和mysql8.0的密码加密方式不一样了)
查看加密方式(加密方式为 caching_sha2_password)
select user,plugin from user where user='root';
mysql配置文件(my.cnf)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
default_authentication_plugin=mysql_native_password
# Custom config should go here
#!includedir /etc/mysql/conf.d/
[mysqld]
default_authentication_plugin=mysql_native_password
测试连接
连接成功!mysql配置完成