不同的应用程序可能依赖于不同版本的 MySQL 或具有不同的配置需求。通过 Docker,每个 MySQL 实例都可以运行在独立的容器中,与宿主机以及其他容器的环境相互隔离。这有效避免了因不同应用对 MySQL 版本、依赖库等方面的差异而导致的冲突。例如,应用 A 需要 MySQL 5.7 版本,而应用 B 需要 MySQL 8.0 版本,使用 Docker 可以轻松创建两个不同版本的 MySQL 容器,满足各自的需求,且不会相互干扰。
在 Docker 中,可以根据应用的实际需求为每个 MySQL 容器分配特定的 CPU、内存等资源。这样能确保关键应用的数据库获得足够的资源,保证性能稳定,同时也能避免资源的过度分配或浪费。
使用 Docker 安装 MySQL 只需一条简单的命令即可从 Docker Hub 拉取官方镜像并创建容器,相比传统的在服务器上手动安装 MySQL 及其依赖,大大节省了安装和配置的时间。
Docker 容器具有高度的可移植性。一旦在一个环境中创建并配置好 MySQL 容器,就可以将该容器及其包含的数据和配置轻松迁移到其他支持 Docker 的环境中,无论是在本地开发环境、测试环境还是生产环境之间迁移,都能快速实现,无需在新环境中重新进行复杂的安装和配置过程
在开发和测试过程中,可能需要频繁切换 MySQL 版本以测试应用在不同版本下的兼容性。通过 Docker,只需拉取相应版本的 MySQL 镜像,创建新的容器即可实现版本切换,而无需在宿主机上进行繁琐的版本卸载和安装操作。
1.拉取MySQL镜像
1.1 拉取最新版本
如果不标注版本,就是拉取最新版本
docker pull mysql:latest
# 等同于上一句命令docker pull mysql
1.2 拉取指定版本
docker pull mysql:5.7
我们使用mysql5.7作为示例操作
[root@192 /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
20e4dcae4c69: Pull complete
1c56c3d4ce74: Pull complete
e9f03a1c24ce: Pull complete
68c3898c2015: Pull complete
6b95a940e7b6: Pull complete
90986bb8de6e: Pull complete
ae71319cb779: Pull complete
ffc89e9dfd88: Pull complete
43d05e938198: Pull complete
064b2d298fba: Pull complete
df9a4d85569b: Pull complete
Digest: sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bb
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@192 /]#
查看镜像,mysql5.7已经存在
2.运行MySQL容器
2.1 一般操作
命令:
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
[root@192 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@192 /]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
2e1854c7aa11abfce0a325672e3196e08125b6024d19155cb44cfac61c8e954a
[root@192 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e1854c7aa11 mysql:5.7 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp stupefied_wiles
[root@192 /]#
进入容器并打开数据库
docker exec -it 容器ID /bin/bash
[root@192 /]# docker exec -it 2e1854c7aa11 /bin/bash
bash-4.2# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 MySQL Community Server (GPL)Copyright (c) 2000, 2023, 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>
测试数据库:
创建数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)mysql> create database shop;
Query OK, 1 row affected (0.00 sec)mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| shop |
| sys |
+--------------------+
5 rows in set (0.00 sec)mysql>
创建数据表:
mysql> use shop;
Database changed
mysql> create table users(id int,name varchar(20));
Query OK, 0 rows affected (0.00 sec)mysql> show tables;
+----------------+
| Tables_in_shop |
+----------------+
| users |
+----------------+
1 row in set (0.00 sec)mysql>
数据操作 - 插入数据
mysql> insert into users values(1,"tom");
Query OK, 1 row affected (0.01 sec)
数据操作 - 查询数据
mysql> select * from users;
+------+------+
| id | name |
+------+------+
| 1 | tom |
+------+------+
1 row in set (0.00 sec)
数据操作 - 更新数据
mysql> update users set name = "tom1" where id= 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from users;
+------+------+
| id | name |
+------+------+
| 1 | tom1 |
+------+------+
1 row in set (0.00 sec)
数据操作 - 删除数据
mysql> delete from user where id = 1-> ;
ERROR 1146 (42S02): Table 'shop.user' doesn't exist
mysql> delete from users where id = 1;
Query OK, 1 row affected (0.00 sec)mysql> select * from users;
Empty set (0.00 sec)
这种方法创建的数据库,一旦容器被误操作删除,那么数据就被删除不能恢复了。
2.2 数据卷映射操作
命令:
docker run -d -p 3306:3306
--privileged=true
-v /home/mysql/log:/var/log/mysql
-v /home/mysql/data:/var/lib/mysql
-v /home/mysql/conf:/etc/mysql/conf.d
-eMYSQL_ROOT_PASSWORD=123456
--name mysql2 mysql:5.7
[root@192 /]# docker run -d -p 3306:3306 --privileged=true -v /home/mysql/log:/var/log/mysql -v /home/mysql/data:/var/lib/mysql -v /home/mysql/conf:/etc/mysql/conf.d -eMYSQL_ROOT_PASSWORD=123456 --name mysql2 mysql:5.7
ed45beb0dafd8cfc2e763707ad766cc4bae15aaaf0b103c4a81391b0d0d70639
[root@192 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ed45beb0dafd mysql:5.7 "docker-entrypoint.s…" 6 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql2
[root@192 /]# cd /home
[root@192 home]# ls
mysql
[root@192 home]# cd mysql
[root@192 mysql]# ls
conf data log
[root@192 mysql]#
新建my.cnf,通过容器卷同步给mysql容器实例,我们修改下默认的字符集
vi my.cnf[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
重新启动mysql容器实例
[root@192 conf]# docker restart mysql2
mysql2
[root@192 conf]#
进入数据库容器
docker exec -it mysql2 bash
[root@192 /]# docker exec -it mysql2 bash
bash-4.2# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 MySQL Community Server (GPL)Copyright (c) 2000, 2023, 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>
测试数据库:
创建数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)mysql> create database advs;
Query OK, 1 row affected (0.00 sec)mysql>
创建数据表:
mysql> use advs;
Database changed
mysql> show tables;
Empty set (0.00 sec)mysql> create table adv(id int,title varchar(100));
Query OK, 0 rows affected (0.01 sec)mysql> show tables;
+----------------+
| Tables_in_advs |
+----------------+
| adv |
+----------------+
1 row in set (0.00 sec)mysql>
插入数据:
mysql> insert into adv values(1,"tom is tom");
Query OK, 1 row affected (0.01 sec)mysql>
查询数据:
mysql> select * from adv;
+------+------------+
| id | title |
+------+------------+
| 1 | tom is tom |
+------+------------+
1 row in set (0.00 sec)mysql>
更新数据:
mysql> update adv set title = "tom is not tom" where id =1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from adv;
+------+----------------+
| id | title |
+------+----------------+
| 1 | tom is not tom |
+------+----------------+
1 row in set (0.00 sec)mysql>
我们看下宿主机中是否有映射的数据:
宿主机中已经有我们创建的数据库和数据表了。
[root@192 mysql]# ls
conf data log
[root@192 mysql]# cd data
[root@192 data]# ls
advs ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 mysql.sock private_key.pem server-cert.pem sys
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql performance_schema public_key.pem server-key.pem
[root@192 data]# cd advs
[root@192 advs]# ls
adv.frm adv.ibd db.opt
[root@192 advs]#
删除数据:
mysql> delete from adv where id = 1;
Query OK, 1 row affected (0.00 sec)mysql> select * from adv;
Empty set (0.00 sec)mysql>
数据库容器数据被映射到宿主机中,相当于备份。即使容器被删除,数据也会存在。
3.MySQL容器状态
docker ps 查询
docker ps --filter "name=mysql2"
容器日志
docker logs mysql2
容器资源情况
docker stats mysql2
检查容器inspect
docker inspect mysql2
4. 进入MySQL容器
docker exec -it 容器ID /bin/bash
[root@192 /]# docker exec -it ed45beb0dafd /bin/bash
bash-4.2# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
bash-4.2# cd var
bash-4.2# ls
adm cache db empty games gopher kerberos lib local lock log mail nis opt preserve run spool tmp yp
bash-4.2# cd lib
bash-4.2# ls
alternatives games misc mysql mysql-files mysql-keyring rpm rpm-state supportinfo yum
bash-4.2# cd mysql
bash-4.2# ls
advs ca-key.pem client-cert.pem ib_buffer_pool ib_logfile1 ibtmp1 mysql.sock private_key.pem server-cert.pem sys
auto.cnf ca.pem client-key.pem ib_logfile0 ibdata1 mysql performance_schema public_key.pem server-key.pem
bash-4.2#
使用这个命令,可以进入容器内部,管理mysql设置与数据,但是一般情况下,我们都是通过映射数据卷来管理。
5. 管理MySQL数据库
我们使用数据库管理工具Navicat管理MySQL数据库
链接成功后,我们看到之前创建的数据库与数据表
创建数据表
插入数据:
查询数据:
更新数据:
删除数据:
6.最后
感谢大家,请大家多多支持!