- 拉取镜像 ( 指定版本 5.7.35 )
docker pull mysql:5.7
- 运行 mysql 容器
docker run -d --name mysql-dev -v mysql:/data/mysql -p 33066:3306 -e MYSQL_ROOT_PASSWORD=zhang33060 mysql:5.7.35
参数详解:
-d 后台运行
-v 指定volume 挂载
-p 端口映射 容器内部 3306 端口映射到本地 33060 端口
-e 设置环境变量 MYSQL_ROOT_PASSWORD 设置root 密码 ( mysql更多环境变量可参考:https://hub.docker.com/_/mysql )
–name 设置容器名
也可以使用
docker run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql:5.7.35
启动的时候,将root 密码设置为空
- 验证
root@0fb4592ec66d:/# mysql -h localhost -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>