(1)先docker images查看本机的镜像有没有mysql
- docker images命令
[root@Master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 987b78fc9e38 11 months ago 104MB
rabbitmq 3.7.7-management 2888deb59dfc 2 years ago 149MB
(2)没有mysql镜像的话拉取mysql镜像,可以通过docker search mysql来查看mysql相关镜像,不过具体版本信息还是要在http://hub.docker.com上查询,这里选择拉取mysql:5.7版本的镜像。
- docker search mysql命令和docker pull mysql:5.7命令
[root@Master ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10760 [OK]
mariadb MariaDB Server is a high performing open sou… 4050 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 791 [OK]
percona Percona Server is a fork of the MySQL relati… 533 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 87
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 81
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
bitnami/mysql Bitnami MySQL Docker Image 50 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 43
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
prom/mysqld-exporter 37 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 27
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
circleci/mysql MySQL is a widely used, open-source relation… 20
mysql/mysql-router MySQL Router provides transparent routing be… 19
arey/mysql-client Run a MySQL client from a docker container 17 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 12 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
[root@Master ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
f7ec5a41d630: Pull complete
9444bb562699: Pull complete
6a4207b96940: Pull complete
181cefd361ce: Pull complete
8a2090759d8a: Pull complete
15f235e0d7ee: Pull complete
d870539cd9db: Pull complete
7310c448ab4f: Pull complete
4a72aac2e800: Pull complete
b1ab932f17c4: Pull complete
1a985de740ee: Pull complete
Digest: sha256:e42a18d0bd0aa746a734a49cbbcc079ccdf6681c474a238d38e79dc0884e0ecc
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
(3)最后再docker images查看下镜像信息,确保本地同步好mysql镜像,然后开始启动mysql容器,因为docker容器的特性,所以要双开mysql非常之方便,只需做好端口映射即可,每个容器都拥有自己的文件系统、CPU、内存、进程空间等资源,故无需在服务器上安装两个mysql数据库,然后修改my.cnf配置文件的端口,只需在docker容器启动时做好服务器外部端口映射即可。
参数说明:
-p 3307:3306:服务器的3307端口映射到docker容器的3306端口。
--name mysqlone:运行容器服务名字。
-v /etc/mysql/conf.d:/etc/mysql/conf.d:对宿主机/etc/mysql/conf.d目录下数据库配置文件与容器/etc/mysql/conf.d目录下数据库配置文件进行映射挂载。
-v /var/log/mysql:/var/log/mysql:对宿主机/var/log/mysql目录下数据库日志与容器/var/log/mysql目录下数据库日志进行映射挂载。
-v /var/lib/mysql:/var/lib/mysql:对宿主机数据库/var/lib/mysql目录下与容器数据库目录/var/lib/mysql进行映射挂载。
-e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。
-d mysql:5.7:后台运行mysql5.7。
--character-set-server=utf8mb4 :设置字符集。
--collation-server=utf8mb4_unicode_ci:设置校对集。
- 比较重要的是-v xxx:xxx 挂载容器的数据库文件到宿主机上,相当于一次备份,假若容器被删除了,那么数据还在的,如果不挂载,重启容器是不会丢失数据的,但是为了安全保证,还是需要挂载备份,重启容器时也会从宿主机目录下去加载相关文件。
- docker run -p 3307:3306 --name mysqlone -v /etc/mysql/conf.d:/etc/mysql/conf.d -v /var/log/mysql:/var/log/mysql -v /var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci 命令一
- docker run -p 3308:3306 --name mysqltwo -v /etc/mysql/conf.d:/etc/mysql/conf.d -v /var/log/mysql:/var/log/mysql -v /var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci 命令二
[root@Master ~]# docker run -p 3307:3306 --name mysqlone -v /etc/mysql/conf.d:/etc/mysql/conf.d -v /var/log/mysql:/var/log/mysql -v /var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
6e64d2aefdd37c1bd76c64f45936ea272f2ac074906fb9b2b0a9ab69f8f6d41e
[root@Master ~]# docker run -p 3308:3306 --name mysqltwo -v /etc/mysql/conf.d:/etc/mysql/conf.d -v /var/log/mysql:/var/log/mysql -v /var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
8aef946b1dd270b1601174f4bec41af46753971c3307bd7568802fb3327bbd27
- docker ps -a 命令列出所有容器情况,可以看到有两个正在运行的mysql,服务器外部端口3307和3308,这样我们可以通过navicat来连接服务器的两个mysql数据库。
[root@Master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8aef946b1dd2 mysql:5.7 "docker-entrypoint.s…" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:3308->3306/tcp mysqltwo
6e64d2aefdd3 mysql:5.7 "docker-entrypoint.s…" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:3307->3306/tcp mysqlone
cf945a87496b 2888deb59dfc "docker-entrypoint.s…" 10 months ago Exited (255) 13 hours ago 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbitmq3.7.7
4903a31afb36 redis:latest "docker-entrypoint.s…" 10 months ago Exited (255) 13 hours ago 0.0.0.0:6379->6379/tcp determined_banach
-
navicat连接两个mysql
3307mysql.png
- docker logs查看mysqlone容器日志,参数 -t 表示显示时间戳,-f 跟踪日志输出,--tail 仅列出最新N条容器日志
[root@Master ~]# docker logs -t -f --tail 10 mysqlone
2021-04-17T15:08:17.272288827Z 2021-04-17T15:08:17.272248Z 0 [Warning] CA certificate ca.pem is self signed.
2021-04-17T15:08:17.272317126Z 2021-04-17T15:08:17.272292Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2021-04-17T15:08:17.272824510Z 2021-04-17T15:08:17.272786Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2021-04-17T15:08:17.272835327Z 2021-04-17T15:08:17.272820Z 0 [Note] IPv6 is available.
2021-04-17T15:08:17.272839017Z 2021-04-17T15:08:17.272828Z 0 [Note] - '::' resolves to '::';
2021-04-17T15:08:17.272862363Z 2021-04-17T15:08:17.272839Z 0 [Note] Server socket created on IP: '::'.
2021-04-17T15:08:17.273439643Z 2021-04-17T15:08:17.273404Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2021-04-17T15:08:17.280404233Z 2021-04-17T15:08:17.280357Z 0 [Note] Event Scheduler: Loaded 0 events
2021-04-17T15:08:17.280607320Z 2021-04-17T15:08:17.280525Z 0 [Note] mysqld: ready for connections.
2021-04-17T15:08:17.280616667Z Version: '5.7.33' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
- docker exec -it mysqlone /bin/bash命令进入mysqlone容器
[root@Master ~]# docker exec -it mysqlone /bin/bash
root@22e10973a47d:/#
- 在mysqlone容器中连接mysql,输入mysql -u root -p回车然后输入密码,可以看到就进入mysql了。
root@22e10973a47d:/# mysql -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.33 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>
mysql> quit
Bye
root@22e10973a47d:/#
- 退出容器
root@22e10973a47d:/# exit
exit
[root@Master ~]#
- docker inspec 容器名/容器ID命令查看容器挂载于宿主机目录
[root@Master ~]# docker inspect mysqlone
可在HostConfig的binds查看挂载映射目录
docker inspec命令.png
- docker stop和docker rm命令暂停和删除mysqlone容器
[root@Master ~]# docker stop mysqlone
mysqlone
[root@Master ~]# docker rm mysqlone
mysqlone
最后docker ps -a检查有没有删除成功即可。
网友评论