Docker version 20.10.10
Ubuntu 20.04
MySQL 8.0.16
docker pull mysql:8.0.16
拉取完后可使用以下命令查看已下载镜像。
docker images
mkdir -p /usr/mysql/conf /usr/mysql/data chmod -R 755 /usr/mysql/
vim /usr/mysql/conf/my.cnf
添加以下内容到配置文件中,并保存。
[client] #socket = /usr/mysql/mysqld.sock default-character-set = utf8mb4 [mysqld] datadir = /var/lib/mysql character_set_server = utf8mb4 collation_server = utf8mb4_bin secure-file-priv= NULL # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Custom config should go here !includedir /etc/mysql/conf.d/
docker run --restart=always -d --name mysql -v /usr/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /usr/mysql/data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.16
可使用以下命令查看容器运行状态。
docker ps -a
docker exec -it mysql /bin/bash
mysql -uroot -p
use mysql
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '新密码';
flush privileges