PassJava (佳必过) 项目全套学习教程连载中。
文档在线地址:www.passjava.cn
docker 安装 mysql
1.下载镜像
sudo docker pull mysql:5.7
ubuntu@VM-0-13-ubuntu:~$ sudo docker pull mysql:5.7
5.7: Pulling from library/mysql
c499e6d256d6: Pull complete
22c4cdf4ea75: Pull complete
6ff5091a5a30: Pull complete
2fd3d1af9403: Pull complete
0d9d26127d1d: Pull complete
54a67d4e7579: Pull complete
fe989230d866: Pull complete
466a91a95e2f: Pull complete
3e4554c238f1: Pull complete
603b48ead88c: Pull complete
1e86a9aa7171: Pull complete
Digest: sha256:fbaeced79cfdae5d3c8d4a8c41e883f254f72ed7428c6b93a498824b76d97121
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
2.查看下载的镜像
sudo docker images
3.创建mysql实例并启动
- 创建mysql实例并启动
sudo docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
参数说明
-p 3306:3306 将容器的3306端口映射到主机
-v /mydata/mysql/log:/var/log/mysql\ 将日志文件挂载到主机
-v /mydata/mysql/data:/var/lib/mysql\ 将数据文件挂载到主机
-v /mydata/mysql/conf:/etc/mysql\ 将配置文件挂载到主机
- 查看docker容器
mysql容器已启动
4.连接数据库
- 用Workbench连接数据库
- 查看数据库
5.进入mysql 容器
sudo docker exec -it mysql /bin/bash
6.查看虚拟机映射文件
cd /mydata/mysql
ls
7.修改mysql账号密码
1.进入mysql容器
docker exec -it mysql /bin/bash
2.登录mysql
mysql -u root -p
输入密码:root
3.切换数据库
use mysql
4.查询root用户
select * from user where user = root;
5.修改密码
update user set authentication_string = password('新的密码'), password_expired = 'N', password_last_changed = now() where user = 'root';
6.这条命令暂不清楚
update user set plugin="mysql_native_password";
7.刷新权限
flush privileges;
8.退出
quit;
9.重新登录
mysql -u root -p
输入新的密码,登录成功
8.其他命令
- 设置容器在机器重启后自动启动
docker update 84c --restart=always