安装Docker和Docker Compose:
安装预置:
sudo yum update
yum install python3-pip
sudo yum group install "Development Tools"
sudo yum install epel-release
sudo yum install openssl-devel libffi-devel python3-devel
安装docker:
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
ls /var/lib/docker
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl start docker
安装docker-compose :
sudo rm /usr/local/bin/docker-compose (删除旧的)
sudo curl -L "https://developer.aliyun.com/compose/docker-compose/$(uname -s)/$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker compose version (验证版本,docker-compose 这个旧格式不能用了)
[root@localhost MYSQL]# docker compose version
Docker Compose version v2.19.1
创建容器:
docker compose up -d (docker-compose 这个旧格式不能用了)
配置主库(Mysql):
docker exec -it [容器ID或名称] bash
mysql -uroot -p
mysql> CREATE USER 'replication_user'@'%' IDENTIFIED BY 'replication_password';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW MASTER STATUS; # 这里注意MASTER_LOG_POS = 881,要在从节点配置。
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 | 881 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置从库(Slave):
docker exec -it [容器ID或名称] bash
mysql -uroot -p
mysql> STOP SLAVE;
mysql> CHANGE MASTER TO
-> MASTER_HOST = 'mysql-master',
-> MASTER_PORT = 3306,
-> MASTER_USER = 'replication_user',
-> MASTER_PASSWORD = 'replication_password',
-> MASTER_LOG_FILE = 'binlog',
-> MASTER_LOG_POS = 881; #刚才在主节点【SHOW MASTER STATUS;】获取的值。
Query OK, 0 rows affected, 9 warnings (0.01 sec)
mysql> START SLAVE; # 启动从库的复制进程:
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> SHOW SLAVE STATUS \G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to source
Master_Host: mysql-master
Master_User: replication_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog
Read_Master_Log_Pos: 881
Relay_Log_File: d7adf7f88efa-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: binlog
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
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: 881
Relay_Log_Space: 157
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: 2061
Last_IO_Error: Error connecting to source 'replication_user@mysql-master:3306'. This was attempt 2/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
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: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 230711 07:37:56
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.00 sec)
ERROR:
No query specified
确保Slave_IO_Running和Slave_SQL_Running都显示为Connecting to source 和 Yes。如果是No,则可能有配置问题。
根据 MySQL SHOW SLAVE STATUS \G 的输出,可以看到以下相关信息:
Read_Master_Log_Pos: 881
Exec_Master_Log_Pos: 881
这表示当前从服务器(Slave)正在读取主服务器(Master)的二进制日志文件,并且已经读取到位置为 881 的位置。