docker 安装最新mysql

268 阅读1分钟

环境是ubuntu
首先换源

echo >/etc/docker/daemon.json
cat>/etc/docker/daemon.json <<END
{
  "registry-mirrors": [
    "https://hub-mirror.c.163.com",
    "https://ustc-edu-cn.mirror.aliyuncs.com",
    "https://ghcr.io",
    "https://mirror.baidubce.com"
  ]
}
END
systemctl restart docker

然后下载mysql镜像

docker pull mysql

查看

docker images

在这里插入图片描述
创建容器

#创建mysql 设置开机自启  root权限
docker run \
 -itd  \
 -p 3306:9836 \
 --name cadmysql \
  --restart always \
 -e MYSQL_ROOT_PASSWORD=123456 \
-d mysql

上述命令的参数,有如下含义:

--name指定了你要取的名字。
-p对应,需要映射出来的端口。
	比如:3306:3306,意识表示为testmysql的容器里面的3306端口对应我外面这个虚拟机的9806端口。
-e是mysql的命令,设置root的密码为123456
-d是运行的镜像,这里是mysql 容器镜像
 --restart always \ 自动启动

进入mysql

docker exec -it testmysql bash

直接mysql登录即可

ps:
1.修改密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

2.开启远程登录

use mysql;
 update user set host = '%' where user = 'root';
 flush privileges;