1拉取mysql8镜像并创建容器启动
docker run -d \
-p 3306:3306 \
-v /Users/neter/Desktop/docker-mysql/conf:/etc/mysql/conf.d \
-v /Users/neter/Desktop/docker-mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--name mysql-8 \
--restart=always \
mysql:8.0.29
其中/Users/neter/Desktop/docker-mysql为主机目录
2增加mysql配置文件
在/Users/neter/Desktop/docker-mysql为主机目录下新建my.conf文件:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
default-time-zone='+08:00'
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
3修改mysql容器密码
mysql8安装后 默认密码加密的插件不是mysql_native_password,所以之前老版本的mysql客户端工具不能直接使用密码连接必须升级到最新版本,或者修改mysql8密码加密的插件为mysql_native_password
#进入容器:env LANG=C.UTF-8 避免容器中显示中文乱码
docker exec -it mysql-8 env LANG=C.UTF-8 /bin/bash
#进入容器内的mysql命令行
mysql -uroot -p
#使用mysql_native_password密码插件创建新的账户root % 并使用它加密密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';