1.准备目录
mkdir -p /data/tomcat/webapps
2.准备编排文件
[root@es1 data]# docker-compose --version
Docker Compose version v2.5.1
cat > docker-compose.yml << EOF
--需要严格遵循缩进机制。形成映射关系。
3.运行mysql 和tomcat 容器。
--后台启动:使用 docker-compose 工具。
cd /data/
docker-compose -f docker-compose.yml up -d
[root@es1 data]# docker-compose -f docker-compose.yml up -d
[+] Running 3/3
⠿ Network data_default Created 0.2s
⠿ Container app-tomcat Started 1.2s
⠿ Container app-mysql Started
--使用 docker-compose 重启容器。
[root@es1 data]# docker-compose -f docker-compose.yml down
[+] Running 3/3
⠿ Container app-tomcat Removed 0.6s
⠿ Container app-mysql Removed 2.3s
⠿ Network data_default Removed 0.2s
[root@es1 data]# docker-compose -f docker-compose.yml up -d
[+] Running 3/3
⠿ Network data_default Created 0.2s
⠿ Container app-mysql Started 1.1s
⠿ Container app-tomcat Started
4.检查Mysql;
[root@es1 data]# ps -ef |grep mysql
ods 48103 48058 0 14:14 ? 00:00:00 mysqld
root 48271 27640 0 14:14 pts/1 00:00:00 grep --color=auto mysql
[root@es1 data]#
[root@es1 data]#
[root@es1 data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6bf63a191f1a 192.168.1.11:443/myharbor/mysql:5.7.21 "docker-entrypoint.s…" 58 seconds ago Up 57 seconds 0.0.0.0:33306->3306/tcp, :::33306->3306/tcp app-mysql
34a9d4e23e40 192.168.1.11:443/myharbor/tomcat:10.0.0-jdk8 "catalina.sh run" 58 seconds ago Up 57 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp app-tomcat
4e7efe886d08 192.168.1.11:443/myharbor/centos:7.9.2009 "/bin/bash" 5 hours ago Up 5 hours mylinux4
9ccb16edd9cd 192.168.1.11:443/myharbor/centos:7.9.2009 "/bin/bash" 5 hours ago Up 5 hours mylinux3
c1ab2e3fa765 192.168.1.11:443/myharbor/centos:7.9.2009 "/bin/bash" 17 hours ago Up 17 hours mylinux2
mysql -h 192.168.1.7 -P33306 -uroot -prootroot --ssl-mode=DISABLED
[root@es1 data]# mysql -h 192.168.1.7 -P33306 -uroot -prootroot --ssl-mode=DISABLED
mysql: Can't create/write to file '/data/mysql/logs/query.log' (Errcode: 2 - No such file or directory)
Error logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 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.
"root@192.168.1.7 Thu Jul 4 14:15:37 2024 14:15:37 [(none)]">show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
5.停止服务:并删除。
[root@es1 data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6bf63a191f1a 192.168.1.11:443/myharbor/mysql:5.7.21 "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 0.0.0.0:33306->3306/tcp, :::33306->3306/tcp app-mysql
34a9d4e23e40 192.168.1.11:443/myharbor/tomcat:10.0.0-jdk8 "catalina.sh run" 12 minutes ago Up 12 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp app-tomcat
4e7efe886d08 192.168.1.11:443/myharbor/centos:7.9.2009 "/bin/bash" 5 hours ago Up 5 hours mylinux4
9ccb16edd9cd 192.168.1.11:443/myharbor/centos:7.9.2009 "/bin/bash" 5 hours ago Up 5 hours mylinux3
c1ab2e3fa765 192.168.1.11:443/myharbor/centos:7.9.2009 "/bin/bash" 18 hours ago Up 18 hours mylinux2
docker-compose -f docker-compose.yml down
[root@es1 data]# docker-compose -f docker-compose.yml down
[+] Running 3/3
⠿ Container app-mysql Removed 3.5s
⠿ Container app-tomcat Removed 0.5s
⠿ Network data_default Removed
[root@es1 data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e7efe886d08 192.168.1.11:443/myharbor/centos:7.9.2009 "/bin/bash" 5 hours ago Up 5 hours mylinux4
9ccb16edd9cd 192.168.1.11:443/myharbor/centos:7.9.2009 "/bin/bash" 5 hours ago Up 5 hours mylinux3
c1ab2e3fa765 192.168.1.11:443/myharbor/centos:7.9.2009 "/bin/bash" 18 hours ago Up 18 hours mylinux2
--由此可见,停止时已经被删除。
6.启动mysql前设置合适的参数:
[root@es1 data]# cat docker-compose.yml
[root@es1 data]# docker-compose -f docker-compose.yml up -d
[+] Running 3/3
⠿ Network data_default Created 0.2s
⠿ Container app-tomcat Started 1.3s
⠿ Container app-mysql Started 1.3s
[root@es1 data]#
[root@es1 data]#
[root@es1 data]# mysql -uroot -prootroot -h192.168.1.7 -P33306
mysql: Can''t create/write to file '/data/mysql/logs/query.log' (Errcode: 2 - No such file or directory)
Error logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 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.
"root@192.168.1.7 Thu Jul 4 14:30:31 2024 14:30:31 [(none)]">show variables like '%sql_mode%';
+---------------+------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------+------------------------------------------------------------------------------------------------------------------------+
| sql_mode | STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
"root@192.168.1.7 Thu Jul 4 14:30:43 2024 14:30:43 [(none)]">show variables like '%server%';
+---------------------------------+--------------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------------+
| character_set_server | utf8mb4 |
| collation_server | utf8mb4_general_ci |
| innodb_ft_server_stopword_table | |
| server_id | 0 |
| server_id_bits | 32 |
| server_uuid | 3d2e5484-39cc-11ef-aff1-0242ac130002 |
+---------------------------------+--------------------------------------+
6 rows in set (0.00 sec)