下载mysql8.0docker镜像
docker pull mysql:8.0
查看下载的docker镜像
docker images
创建挂载目录
mkdir -p /data/mysql/conf
mkdir -p /data/mysql/data
mkdir -p /data/mysql/logs
运行
docker run -p 3306:3306 --name mysql --restart=always --privileged=true \
-v /data/mysql/log:/var/log/mysql \
-v /data/mysql/data:/var/lib/mysql \
-v /data/mysql/conf:/etc/mysql/conf.d \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0
创建my.cnf文件,放在 /data/mysql/conf 目录中,注意配置文件中的端口号、字符集、时区
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
# 设置东八区时区
default-time_zone = '+8:00'
# 设置密码验证规则,default_authentication_plugin参数已被废弃
# 改为authentication_policy
#default_authentication_plugin=mysql_native_password
authentication_policy=mysql_native_password
#secure_file_priv=/var/lib/mysql
secure_file_priv=
init_connect='SET collation_connection = utf8mb4_0900_ai_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_0900_ai_ci
skip-character-set-client-handshake
skip-name-resolve
重启配置文件生效
# 重启mysql容器
docker restart mysql
# 设置docker启动时启动mysql
docker update mysql --restart=always
# 重启mysql容器
docker restart mysql
# 进入mysql容器内部
docker exec -it mysql /bin/bash
# 退出mysql容器
exit
远程登录
# 进入容
docker exec -it mysql8.0 bash
# 链接数据库
mysql -uroot -p123456
# 切换数据库节点
use mysql
# 查询数据库用户表关键信息
SELECT Host, User, plugin from user;
# 修改加密规则
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
# 立即生效配置
FLUSH PRIVILEGES;