#为docker容器的时间和宿主机同步
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#为后面docker数据卷挂载做准备
mkdir -p /docker/data/mysql/log
mkdir -p /docker/data/mysql/data
mkdir -p /docker/data/mysql/conf
阿里云控制台——》点左上角的菜单(产品与服务),看右边——》容器——》容器服务——》容器镜像服务——》镜像工具——》镜像加速
复制镜像加速器地址,输入如下命令:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload sudo systemctl restart docker
从Oracle的镜像库拉取mysql community-server镜像
docker pull container-registry.oracle.com/mysql/community-server:8.0
参数有(端口、容器名、挂载数据卷、设置root密码、时区)
docker run -d -p 33306:3306 --name mysql8.0 \
-v /docker/data/mysql/log:/var/log/mysql \
-v /docker/data/mysql/data:/var/lib/mysql \
-v /docker/data/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
container-registry.oracle.com/mysql/community-server:8.0
查看是否成功
docker ps
#获取mysql交互式终端
docker exec -it mysql8.0 /bin/bash
#输入用户名和密码
mysql -uroot -p
#mysql命令建库、建用户、授权
show databases;
create database db1;
use db1;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxx';
CREATE USER 'user1'@'%' IDENTIFIED BY 'xxxx';
grant all privileges on db1.* to 'user1'@'%' with grant option;