MySQL镜像安装和测试
https://hub.docker.com/_/mysql
拉取mysql 5.7版本的镜像:
sudo docker pull mysql:5.7
在宿主机上创建一个目录存放数据(作为存储卷):
sudo mkdir -p /data/mysql
创建mysql容器时,加入-v [宿主机存储卷]:[容器存储路径] 指定绑定的存储卷位置,例如:
1
sudo docker run -v /data/mysql:/var/lib/mysql --name mysql1 -it mysql:5.7
2
2020-04-23 06:13:59+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.29-1debian10 started.
3
2020-04-23 06:13:59+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
4
2020-04-23 06:13:59+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.29-1debian10 started.
5
2020-04-23 06:13:59+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
6
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
Copied!
这里创建出来的容器是停止状态的,而且没有启动成功,因为没有指定数据库的ROOT PASSWORD
所以命令改为加--env -e --env-file指定密码等参数运行(加-d 参数,使容器以后台方式运行,不占用终端):
1
sudo docker run --env MYSQL_ROOT_PASSWORD='test1234' -d -v /data/mysql:/var/lib/mysql --name mysql2 -it mysql:5.7
Copied!
ubuntu中,如果不想一直用sudo操作,可以切换为root,再查看一下状态(ubuntu里首次使用root用户,记得通过sudo passwd设置它的密码),容器mysql2已经正常运作起来了:
1
[email protected]:~# docker ps -a
2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3
3ae0f16430b5 mysql:5.7 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 3306/tcp, 33060/tcp mysql2
4
96e04560482d mysql:5.7 "docker-entrypoint.s…" 12 minutes ago Exited (1) 12 minutes ago mysql1
Copied!
同时可以用-exec参数进入交互界面:
docker exec -it mysql2 /bin/sh
1
[email protected]:~# docker exec -it mysql2 /bin/sh
2
#
3
# mysql -uroot -p
4
Enter password:
5
Welcome to the MySQL monitor. Commands end with ; or \g.
6
Your MySQL connection id is 2
7
Server version: 5.7.29 MySQL Community Server (GPL)
8
9
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
10
11
Oracle is a registered trademark of Oracle Corporation and/or its
12
affiliates. Other names may be trademarks of their respective
13
owners.
14
15
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
16
17
mysql>
18
mysql> SHOW DATABASES;
19
+--------------------+
20
| Database |
21
+--------------------+
22
| information_schema |
23
| mysql |
24
| performance_schema |
25
| sys |
26
+--------------------+
27
4 rows in set (0.00 sec)
Copied!
在宿主机上,mysql的文件会保留在存储卷上,删除容器也不会丢失了:(停止mysql的容器时,使用命令docker stop 确保数据保存完整)
1
# exit
2
[email protected]:/data/mysql# docker stop mysql2
3
mysql2
4
[email protected]:/data/mysql# docker rm mysql2
5
mysql2
6
[email protected]:/data/mysql# ls
7
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
8
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
Copied!
额外补充:mysql 5.7的dockerfile可以在这里查看:
Last modified 1yr ago
Copy link