因项目需求需要部署主从MySQL集群,继续采用上次的部署的MySQL镜像arm64v8/mysql:latest,版本信息为v8.1.0。计划部署服务器192.168.31.100和192.168.31.101
部署MySQL主节点
在192.168.31.100上先创建好/data/docker/mysql/data和/data/docker/mysql/logs目录,创建好/data/docker/mysql/my.cnf文件,内容如下:
[mysqld]
server-id=100
log-bin=mysql-bin
port=3306
max_connections=200
max_connect_errors=10
character-set-server=utf8
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
performance_schema_max_table_instances=600[mysql]
default-character-set=utf8[client]
port=3306
default-character-set=utf8
然后创建MySQL镜像,执行
docker run --network=host -d -p 3306:3306 \
--name mysql --privileged=true \
-e MYSQL_ROOT_PASSWORD="888888" -e MYSQL_USER="admin" -e MYSQL_PASSWORD="888888" \
-v=/data/docker/mysql/my.cnf:/etc/my.cnf \
-v=/data/docker/mysql/data:/var/lib/mysql \
-v=/data/docker/mysql/logs:/var/log \
-v=/etc/localtime:/etc/localtime \
arm64v8/mysql:latest --server-id=100
执行成功后,检查MySQL是否启动成功,执行docker ps,出现如下内容表示启动成功
[root@server01 mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
50214a2de4f8 arm64v8/mysql:latest "docker-entrypoint.s…" 17 minutes ago Up 17 minutes mysql
进入镜像:docker exec -it mysql /bin/bash
登录MySQL:mysql -uroot -p
依次执行如下命令:
ALTER USER 'root'@'%' IDENTIFIED WITH 'mysql_native_password' BY '888888';
CREATE USER 'slave'@'%' IDENTIFIED BY '888888';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
ALTER USER 'slave'@'%' IDENTIFIED WITH 'mysql_native_password' BY '888888';
FLUSH PRIVILEGES;
然后执行show master status命令:
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 | 2925 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
注意这里的File和Position,很重要,后面会用到。
部署MySQL从节点
在192.168.31.101上先创建好/data/docker/mysql/data和/data/docker/mysql/logs目录,创建好/data/docker/mysql/my.cnf文件,内容如下:
[mysqld]
server-id=101
log-bin=mysql-bin
port=3306
max_connections=200
max_connect_errors=10
character-set-server=utf8
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
performance_schema_max_table_instances=600[mysql]
default-character-set=utf8[client]
port=3306
default-character-set=utf8
然后创建MySQL镜像,执行
docker run --network=host -d -p 3306:3306 \
--name mysql --privileged=true \
-e MYSQL_ROOT_PASSWORD="888888" -e MYSQL_USER="admin" -e MYSQL_PASSWORD="888888" \
-v=/data/docker/mysql/my.cnf:/etc/my.cnf \
-v=/data/docker/mysql/data:/var/lib/mysql \
-v=/data/docker/mysql/logs:/var/log \
-v=/etc/localtime:/etc/localtime \
arm64v8/mysql:latest --server-id=101
执行成功后,检查MySQL是否启动成功,执行docker ps,出现如下内容表示启动成功
[root@server02 mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6793b5e6ded arm64v8/mysql:latest "docker-entrypoint.s…" 21 minutes ago Up 21 minutes mysql
进入镜像:docker exec -it mysql /bin/bash
登录MySQL:mysql -uroot -p
依次执行如下命令:
ALTER USER 'root'@'%' IDENTIFIED WITH 'mysql_native_password' BY '888888';
FLUSH PRIVILEGES;
配置从节点
change master to master_host='192.168.31.100', master_user='slave', master_password='888888', master_port=3306, master_log_file='binlog.000002', master_log_pos=2925, master_connect_retry=30;
注意这里的master_log_file和master_log_pos需要和前面主节点匹配起来,具体参数说明如下:
# master_host :Master库的地址
# master_port:Master的端口号,指的是容器的端口号
# master_user:用于数据同步的用户
# master_password:用于同步的用户的密码
# master_log_file:指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值
# master_log_pos:从哪个 Position 开始读,即上文中提到的 Position 字段的值
# master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒
然后开通同步执行:start slave;
如果需要关闭同步执行:stop slave;
然后执行show slave status命令:
+----------------------------------+-------------+-------------+-------------+---------------+-----------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version | Master_public_key_path | Get_master_public_key | Network_Namespace |
+----------------------------------+-------------+-------------+-------------+---------------+-----------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+
| Waiting for source to send event | 10.16.39.9 | slave | 3306 | 30 | binlog.000002 | 2925 | ecs02-relay-bin.000002 | 1804 | binlog.000002 | Yes | Yes | | | | | | | 0 | | 0 | 2925 | 2014 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 100 | 7ec2b8d1-ae21-11ef-9e2a-fa163e637a5c | mysql.slave_master_info | 0 | NULL | Replica has read all relay log; waiting for more updates | 10 | | | | | | | | 0 | | | | | 0 | |
+----------------------------------+-------------+-------------+-------------+---------------+-----------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+----------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+-------------------+
关键看Slave_SQL_Running_State列,没有报错即可。
至此MySQL主从配置已经全部部署好。
验证测试
在主节点上创建一个数据库test,并创建一个表,插入几条数据
从节点上截图如下: