centos7 installs docker, docker-compose, and mysql master-slave replication
1. Install docker
# Install dependencies yum install -y yum-utils #Set the docker warehouse image address yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # View all available docker-ce versions yum list docker-ce --showduplicates | sort -r # Select a docker-ce version to install yum -y install docker-ce-23.0.6-1.el7 # Set docker to start at boot systemctl enable docker & amp; & amp; systemctl start docker
Configure docker image accelerator
cat > /etc/docker/daemon.json << EOF { "exec-opts": ["native.cgroupdriver=systemd"], "registry-mirrors": ["https://kn0t2bca.mirror.aliyuncs.com"] } EOF
Restart docker service
systemctl daemon-reload & amp; systemctl restart docker
2. Install docker-compose
Install
#Download docker-compose file curl -L https://github.com/docker/compose/releases/download/1.18.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose #Copy the file to the /usr/local/bin environment variable mv docker-compose /usr/local/bin #Give him execution permissions chmod +x /usr/local/bin/docker-compose #Check whether the installation is successful docker-compose-version Docker Compose version v2.18.1
uninstall
rm /usr/local/bin/docker-compose
3. Dockercompose installs mysql master-slave configuration master-slave replication (single node multiple containers)
1. Create the mysql.cnf
file under /data/docker-compose-service/mysql/master/conf.d
with the following content:
[mysqld] log-bin = mysql-bin server-id=1
2. Create the mysql.cnf
file under /data/docker-compose-service/mysql/slave/conf.d
, with the following content:
[mysqld] log-bin = mysql-bin server-id=2
3. Create a storage directory for storing data:
mkdir /data/docker-compose-service/mysql/master/data mkdir /data/docker-compose-service/mysql/slave/data
5. Create the mysql master-slave resource file in the /data/docker-compose-service/mysql directory
version: '3' services: mysql_master: image:mysql restart: always container_name: mysql_master environment: MYSQL_ROOT_PASSWORD: 123456 MYSQL_USER: zhangsan MYSQL_PASSWORD: zhangsan command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci --explicit_defaults_for_timestamp=true --lower_case_table_names=1 --max_allowed_packet=128M; ports: - 13306:3306 volumes: - /data/docker-compose-service/mysql/master/data:/var/lib/mysql - /data/docker-compose-service/mysql/master/conf.d:/etc/mysql/conf.d - /etc/localtime:/etc/localtime:ro networks: -mysql_net mysql_slave: image:mysql restart: always container_name: mysql_slave environment: MYSQL_ROOT_PASSWORD: 123456 MYSQL_USER: zhangsan MYSQL_PASSWORD: zhangsan command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci --explicit_defaults_for_timestamp=true --lower_case_table_names=1 --max_allowed_packet=128M; ports: - 13307:3306 volumes: - /data/docker-compose-service/mysql/slave/data:/var/lib/mysql - /data/docker-compose-service/mysql/slave/conf.d:/etc/mysql/conf.d - /etc/localtime:/etc/localtime:ro networks: -mysql_net networks: mysql_net:
, 6. Check the configuration file
docker-compose -f docker-compose.yaml config -q
7. Start the container (if the network is not good, you can download the image in advance)
[root@VM-node1 mysql]# docker-compose -f docker-compose.yaml up -d [ + ] Building 0.0s (0/0) [ + ] Running 2/2 ? Container mysql_slave Started 1.2s ? Container mysql_master Started
8. Check the container status
[root@VM-node1 mysql]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 701db63c8488 mysql "docker-entrypoint.s…" 9 seconds ago Up 8 seconds 33060/tcp, 0.0.0.0:13307->3306/tcp, :::13307->3306/tcp mysql_slave 18b771b15c73 mysql "docker-entrypoint.s…" 9 seconds ago Up 8 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp, :::13306->3306/tcp mysql_master
9. Enter the container slave node and use the MySQL client to connect to the master server to view the master node status.
[root@VM-node1 mysql]# docker exec -it mysql_slave bash root@701db63c8488:/# mysql -uroot -h mysql_master -p123456 mysql> show master status -> ; + ------------------ + ---------- + --------------- + ---- --------------- + ------------------ + | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | + ------------------ + ---------- + --------------- + ---- --------------- + ------------------ + | mysql-bin.000003 | 156 | | | | + ------------------ + ---------- + --------------- + ---- --------------- + ------------------ + 1 row in set (0.00 sec)
10. Exit the connection between the client and the master node, connect to the local slave node, and configure master-slave synchronization.
mysql> exit root@701db63c8488:/# mysql -uroot -p123456 mysql> change master to master_host='mysql_master', -> master_user='root', -> master_log_file='mysql-bin.000003', -> master_log_pos=156, -> master_port=3306, -> master_password='123456'; Query OK, 0 rows affected, 9 warnings (0.00 sec)
11. Verify master-slave synchronization
mysql> show slave status \G; *************************** 1. row ********************* ******* Slave_IO_State: Master_Host: mysql_master Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 156 Relay_Log_File: 701db63c8488-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: No Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 156 Relay_Log_Space: 156 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 Master_UUID: Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0 Network_Namespace: 1 row in set, 1 warning (0.01 sec)
4. Common commands of docker-compoes and docker
Interpretation | Command | Remarks |
---|---|---|
View help | docker-compose -h | |
Start all services | docker-compose up | |
Start all services in the background | docker-compose up -d | |
Start the service in the background according to the configuration file | docker-compose -f docker-compoes.yaml up -d | |
Stop and delete containers, networks, volumes, and images | docker-compose down | |
Enter the container | docker-compose exec container -it /bin/bash | |
Query all containers currently arranged by docker-compose | docker-compose ps | Must be executed in the same directory as the resource manifest file |
Query the process and resource configuration | docker-compose top | Must be executed in the same directory as the resource manifest file |
View container logs | docker-compose logs container | Must be executed in the directory at the same level as the resource manifest file |
Check the configuration, and output only if there are errors | docker-compose config – q | |
Restart service | docker-compose restart container | |
Start service | docker-compose start container | |
Stop service | docker-compose stop container | |
View specified container (service) resource indicators | docker stats container |
Attached is a container monitoring script (must be executed in the same directory as the resource manifest file)
#!/bin/bash containers=$(docker-compose ps --services) for container in $containers do echo "Resource usage for container: $container" docker stats --no-stream $container echo "--------------------------" done