Ubuntu docker mysql 集群 3
设置多主多从
主/从 | 服务名 | IP | PORT | docker虚拟网络 |
---|---|---|---|---|
主1 | mysql8_master1000 | 192.168.79.177 | 13306 | mysql8_master1000 |
从1 | mysql8_worker1001 | 192.168.79.177 | 13316 | mysql8_worker1001 |
从2 | mysql8_worker1002 | 192.168.79.177 | 13326 | mysql8_worker1002 |
主2 | mysql8_master2000 | 192.168.79.177 | 13336 | mysql8_master2000 |
从3 | mysql8_worker2001 | 192.168.79.177 | 13346 | mysql8_worker2001 |
从4 | mysql8_worker2002 | 192.168.79.177 | 13356 | mysql8_worker2002 |
tester@server01:~$ docker network ls;
NETWORK ID NAME DRIVER SCOPE
0a32f533d846 bridge bridge local
e6c2306a58dd host host local
ec5d0cad9230 jenkins-data_net bridge local
0acc0da3eec1 mysql8_network bridge local
ed4189398aaa none null local
# 创建主库相关目录
tester@server01:~$ mkdir -p /home/tester/data/docker/mysql8_master1000
tester@server01:~$ mkdir -p /home/tester/data/docker/mysql8_master2000
# 创建从库相关目录
tester@server01:~$ mkdir -p /home/tester/data/docker/mysql8_worker1001
tester@server01:~$ mkdir -p /home/tester/data/docker/mysql8_worker1002
tester@server01:~$ mkdir -p /home/tester/data/docker/mysql8_worker2001
tester@server01:~$ mkdir -p /home/tester/data/docker/mysql8_worker2002
主库配置
mysql8_master1000 主库配置
# my.cnf
tester@server01:~$ cat > /home/tester/data/docker/mysql8_master1000/my.cnf <<EOF
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
!includedir /etc/mysql/conf.d/
default_authentication_plugin= mysql_native_password
# must unique value
server-id=1000
log-bin=mysql-bin
# 日志文件自动删除/过期的天数,避免占用磁盘空间。默认值为0-不自动删除。
expire_logs_days=14
# binlog日志写入后与硬盘同步,备注有说明具体配置含义
# sync_binlog=1
binlog_cache_size=4M
# mixed/statement/row format, default is statement, row is better for future operation
# STATEMENT模式(基于SQL语句的复制(statement-based replication, SBR))
# ROW模式(基于行的复制(row-based replication, RBR))
# MIXED模式(混合模式复制(mixed-based replication, MBR))
binlog_format=row
##################################################
# https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#option_mysqld_binlog-do-db
# 配置需要复制的数据库,默认复制全部数据库
# binlog-do-db=test
# 设置不需要复制的数据库
binlog-ignore-db=mysql
binlog-ignore-db=infomation_schema
##################################################
##################################################
# 1000 也会作为 2000 的从机
log-slave-updates
# 自增长的幅度,偏移量
auto-increment-increment=2
# 自增长的开始位置
auto-increment-offset=1
##################################################
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
EOF
mysql8_master2000 主库配置
# my.cnf
tester@server01:~$ cat > /home/tester/data/docker/mysql8_master2000/my.cnf <<EOF
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
!includedir /etc/mysql/conf.d/
default_authentication_plugin= mysql_native_password
# must unique value
server-id=2000
log-bin=mysql-bin
# 日志文件自动删除/过期的天数,避免占用磁盘空间。默认值为0-不自动删除。
expire_logs_days=14
# binlog日志写入后与硬盘同步,备注有说明具体配置含义
# sync_binlog=1
binlog_cache_size=4M
# mixed/statement/row format, default is statement, row is better for future operation
# STATEMENT模式(基于SQL语句的复制(statement-based replication, SBR))
# ROW模式(基于行的复制(row-based replication, RBR))
# MIXED模式(混合模式复制(mixed-based replication, MBR))
binlog_format=row
##################################################
# https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#option_mysqld_binlog-do-db
# 配置需要复制的数据库,默认复制全部数据库
# binlog-do-db=test
# 设置不需要复制的数据库
binlog-ignore-db=mysql
binlog-ignore-db=infomation_schema
##################################################
##################################################
# 2000 也会作为 1000 的从机
log-slave-updates
# 自增长的幅度,偏移量
auto-increment-increment=2
# 自增长的开始位置
auto-increment-offset=2
##################################################
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
EOF
启动 mysql 主库
# 主库01
tester@server01:~$ docker run -d --name mysql8_master1000 \
--privileged=true \
--network mysql8_network \
-v /home/tester/data/docker/mysql8_master1000/my.cnf:/etc/mysql/my.cnf:rw \
-v /home/tester/data/docker/mysql8_master1000/log:/var/log/mysql \
-v /home/tester/data/docker/mysql8_master1000/data:/var/lib/mysql \
-p 13306:3306 -e MYSQL_ROOT_PASSWORD=123456 \
mysql
# 主库02
tester@server01:~$ docker run -d --name mysql8_master2000 \
--privileged=true \
--network mysql8_network \
-v /home/tester/data/docker/mysql8_master2000/my.cnf:/etc/mysql/my.cnf:rw \
-v /home/tester/data/docker/mysql8_master2000/log:/var/log/mysql \
-v /home/tester/data/docker/mysql8_master2000/data:/var/lib/mysql \
-p 13336:3306 -e MYSQL_ROOT_PASSWORD=123456 \
mysql
查看已运行的实例
tester@server01:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ab99b0ba64c7 mysql "docker-entrypoint.s…" 4 seconds ago Up 2 seconds 33060/tcp, 0.0.0.0:13336->3306/tcp, :::13336->3306/tcp mysql8_master2000
5de734a1e3c4 mysql "docker-entrypoint.s…" 12 seconds ago Up 10 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp, :::13306->3306/tcp mysql8_master1000
# 此处不会再自动分配IP,与 Ubuntu docker mysql 集群 1 不一样
tester@server01:~$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql8_master1000
tester@server01:~$
tester@server01:~$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql8_master2000
tester@server01:~$
登入主库且分别设置同步用户
# mysql8_master1000
tester@server01:~$ mysql -uroot -p123456 -h192.168.79.177 -P13306
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 8
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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> CREATE user 'binlog_worker'@'%';
Query OK, 0 rows affected (0.02 sec)
mysql> ALTER user 'binlog_worker'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'binlog_worker'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
# 记住这个Position value `1087`, 后续会用到
mysql> show master status;
+------------------+----------+--------------+-------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------+-------------------+
| mysql-bin.000003 | 1087 | | mysql,infomation_schema | |
+------------------+----------+--------------+-------------------------+-------------------+
1 row in set (0.00 sec)
mysql> show slave status;
Empty set, 1 warning (0.00 sec)
mysql> show slave hosts;
Empty set, 1 warning (0.00 sec)
# mysql8_master2000
tester@server01:~$ mysql -uroot -p123456 -h192.168.79.177 -P13336
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 8
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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> CREATE user 'binlog_worker'@'%';
Query OK, 0 rows affected (0.02 sec)
mysql> ALTER user 'binlog_worker'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'binlog_worker'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
# 记住这个Position value `1087`, 后续会用到
mysql> show master status;
+------------------+----------+--------------+-------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------+-------------------+
| mysql-bin.000003 | 1087 | | mysql,infomation_schema | |
+------------------+----------+--------------+-------------------------+-------------------+
1 row in set (0.00 sec)
mysql> show slave status;
Empty set, 1 warning (0.00 sec)
mysql> show slave hosts;
Empty set, 1 warning (0.00 sec)
主库01 设置为主库02的从库
# mysql8_master1000
mysql> change master to master_host='mysql8_master2000',master_user='binlog_worker',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1087;
Query OK, 0 rows affected, 7 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
主库02 设置为主库01的从库
# mysql8_master2000
mysql> change master to master_host='mysql8_master1000',master_user='binlog_worker',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1087;
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
分别查看01主库与02主库的从库
# mysql8_master1000
mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 2000 | | 3306 | 1000 | 7ff5c2ca-6b3c-11ed-9dd5-0242ac140003 |
+-----------+------+------+-----------+--------------------------------------+
1 row in set, 1 warning (0.00 sec)
# mysql8_master2000
mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 1000 | | 3306 | 2000 | 021d571c-6b3a-11ed-9d45-0242ac140002 |
+-----------+------+------+-----------+--------------------------------------+
1 row in set, 1 warning (0.00 sec)
创建数据库,自动同步到对应的服务商
# mysql8_master1000
mysql> create database bbb;
# mysql8_master2000
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| bbb |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database ccc;
# mysql8_master1000
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| bbb |
| ccc |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
由此互相主从配置完成。
从库1配置
my.cnf 配置,后续映射到docker容器中
tester@server01:~$ cat > /home/tester/data/docker/mysql8_worker1001/my.cnf <<EOF
[mysqld]
server-id=1001
log-bin=mysql-worker-bin
relay_log=mysql-worker-relay-bin
log_bin_trust_function_creators=true
binlog_cache_size=4M
# mixed/statement/row format, default is statement, row is better for future operation
binlog_format=row
##################################################
# https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#option_mysqld_binlog-do-db
# 配置需要复制的数据库,默认复制全部数据库
# replicate_do_db=test
# 设置不需要复制的数据库
replicate-ignore-db=mysql
replicate-ignore-db=infomation_schema
##################################################
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
slave_skip_errors=1062
default_authentication_plugin= mysql_native_password
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
EOF
从库2配置
my.cnf 配置,后续映射到docker容器中
tester@server01:~$ cat > /home/tester/data/docker/mysql8_worker1002/my.cnf <<EOF
[mysqld]
server-id=1002
log-bin=mysql-worker-bin
relay_log=mysql-worker-relay-bin
log_bin_trust_function_creators=true
binlog_cache_size=4M
# mixed/statement/row format, default is statement, row is better for future operation
binlog_format=row
##################################################
# https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#option_mysqld_binlog-do-db
# 配置需要复制的数据库,默认复制全部数据库
# replicate_do_db=test
# 设置不需要复制的数据库
# replicate-ignore-db=mysql
# replicate-ignore-db=infomation_schema
##################################################
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
slave_skip_errors=1062
default_authentication_plugin= mysql_native_password
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
EOF
从库3配置
my.cnf 配置,后续映射到docker容器中
tester@server01:~$ cat > /home/tester/data/docker/mysql8_worker2001/my.cnf <<EOF
[mysqld]
server-id=2001
log-bin=mysql-worker-bin
relay_log=mysql-worker-relay-bin
log_bin_trust_function_creators=true
binlog_cache_size=4M
# mixed/statement/row format, default is statement, row is better for future operation
binlog_format=row
##################################################
# https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#option_mysqld_binlog-do-db
# 配置需要复制的数据库,默认复制全部数据库
# replicate_do_db=test
# 设置不需要复制的数据库
replicate-ignore-db=mysql
replicate-ignore-db=infomation_schema
##################################################
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
slave_skip_errors=1062
default_authentication_plugin= mysql_native_password
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
EOF
从库4配置
my.cnf 配置,后续映射到docker容器中
tester@server01:~$ cat > /home/tester/data/docker/mysql8_worker2002/my.cnf <<EOF
[mysqld]
server-id=2002
log-bin=mysql-worker-bin
relay_log=mysql-worker-relay-bin
log_bin_trust_function_creators=true
binlog_cache_size=4M
# mixed/statement/row format, default is statement, row is better for future operation
binlog_format=row
##################################################
# https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html#option_mysqld_binlog-do-db
# 配置需要复制的数据库,默认复制全部数据库
# replicate_do_db=test
# 设置不需要复制的数据库
# replicate-ignore-db=mysql
# replicate-ignore-db=infomation_schema
##################################################
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
slave_skip_errors=1062
default_authentication_plugin= mysql_native_password
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
EOF
启动从库1,2,3,4
tester@server01:~$ docker run -d --name mysql8_worker1001 \
--privileged=true \
--network mysql8_network \
-v /home/tester/data/docker/mysql8_worker1001/my.cnf:/etc/mysql/my.cnf:rw \
-v /home/tester/data/docker/mysql8_worker1001/log:/var/log/mysql \
-v /home/tester/data/docker/mysql8_worker1001/data:/var/lib/mysql \
-p 13316:3306 -e MYSQL_ROOT_PASSWORD=123456 \
mysql
tester@server01:~$ docker run -d --name mysql8_worker1002 \
--privileged=true \
--network mysql8_network \
-v /home/tester/data/docker/mysql8_worker1002/my.cnf:/etc/mysql/my.cnf:rw \
-v /home/tester/data/docker/mysql8_worker1002/log:/var/log/mysql \
-v /home/tester/data/docker/mysql8_worker1002/data:/var/lib/mysql \
-p 13326:3306 -e MYSQL_ROOT_PASSWORD=123456 \
mysql
tester@server01:~$ docker run -d --name mysql8_worker2001 \
--privileged=true \
--network mysql8_network \
-v /home/tester/data/docker/mysql8_worker2001/my.cnf:/etc/mysql/my.cnf:rw \
-v /home/tester/data/docker/mysql8_worker2001/log:/var/log/mysql \
-v /home/tester/data/docker/mysql8_worker2001/data:/var/lib/mysql \
-p 13346:3306 -e MYSQL_ROOT_PASSWORD=123456 \
mysql
tester@server01:~$ docker run -d --name mysql8_worker2002 \
--privileged=true \
--network mysql8_network \
-v /home/tester/data/docker/mysql8_worker2002/my.cnf:/etc/mysql/my.cnf:rw \
-v /home/tester/data/docker/mysql8_worker2002/log:/var/log/mysql \
-v /home/tester/data/docker/mysql8_worker2002/data:/var/lib/mysql \
-p 13356:3306 -e MYSQL_ROOT_PASSWORD=123456 \
mysql
# 此处不会再自动分配IP,与 Ubuntu docker mysql 集群 1 不一样
tester@server01:~$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql8_worker1001
tester@server01:~$
tester@server01:~$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql8_worker1002
tester@server01:~$
tester@server01:~$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql8_worker2001
tester@server01:~$
tester@server01:~$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql8_worker2002
tester@server01:~$
查看已运行的mysql镜像实例
tester@server01:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc35f6f683ac mysql "docker-entrypoint.s…" 10 seconds ago Up 9 seconds 33060/tcp, 0.0.0.0:13356->3306/tcp, :::13356->3306/tcp mysql8_worker2002
ba4a0cfbc8d6 mysql "docker-entrypoint.s…" 17 seconds ago Up 16 seconds 33060/tcp, 0.0.0.0:13346->3306/tcp, :::13346->3306/tcp mysql8_worker2001
566ec11f7fee mysql "docker-entrypoint.s…" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:13326->3306/tcp, :::13326->3306/tcp mysql8_worker1002
b51a14fa191b mysql "docker-entrypoint.s…" About a minute ago Up About a minute 33060/tcp, 0.0.0.0:13316->3306/tcp, :::13316->3306/tcp mysql8_worker1001
ab99b0ba64c7 mysql "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 33060/tcp, 0.0.0.0:13336->3306/tcp, :::13336->3306/tcp mysql8_master2000
5de734a1e3c4 mysql "docker-entrypoint.s…" 32 minutes ago Up 32 minutes 33060/tcp, 0.0.0.0:13306->3306/tcp, :::13306->3306/tcp mysql8_master1000
通过客户端查看是否连接成功
从库1修改master
### 从库01
tester@server01:~$ mysql -uroot -p123456 -h192.168.79.177 -P13316
# 修改 master
mysql> change master to master_host='mysql8_master1000',master_user='binlog_worker',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1087;
Query OK, 0 rows affected, 7 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)
# 如果Slave_IO_Running: No Slave_SQL_Running: Yes 则查看server_id 是否唯一
# 则执行 stop slave, 重新 change master to ... 再次 start slave;
# mysql> stop slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: mysql8_master1000
Master_User: binlog_worker
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1468
Relay_Log_File: mysql-worker-relay-bin.000002
Relay_Log_Pos: 707
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,infomation_schema
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: 1468
Relay_Log_Space: 924
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: 0
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: 1000
Master_UUID: 021d571c-6b3a-11ed-9d45-0242ac140002
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:
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)
从库2修改master
### 从库02
tester@server01:~$ mysql -uroot -p123456 -h192.168.79.177 -P13326
# 修改 master
mysql> change master to master_host='mysql8_master1000',master_user='binlog_worker',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1087;
Query OK, 0 rows affected, 7 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)
# 如果Slave_IO_Running: No Slave_SQL_Running: Yes 则查看server_id 是否唯一
# 则执行 stop slave, 重新 change master to ... 再次 start slave;
# mysql> stop slave;
# 此处不带 ';'
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: mysql8_master1000
Master_User: binlog_worker
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1468
Relay_Log_File: mysql-worker-relay-bin.000002
Relay_Log_Pos: 707
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
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: 1468
Relay_Log_Space: 924
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: 0
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: 1000
Master_UUID: 021d571c-6b3a-11ed-9d45-0242ac140002
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:
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)
从库3修改master
### 从库01
tester@server01:~$ mysql -uroot -p123456 -h192.168.79.177 -P13446
# 修改 master
mysql> change master to master_host='mysql8_master2000',master_user='binlog_worker',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1087;
Query OK, 0 rows affected, 7 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)
# 如果Slave_IO_Running: No Slave_SQL_Running: Yes 则查看server_id 是否唯一
# 则执行 stop slave, 重新 change master to ... 再次 start slave;
# mysql> stop slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: mysql8_master2000
Master_User: binlog_worker
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1468
Relay_Log_File: mysql-worker-relay-bin.000002
Relay_Log_Pos: 707
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,infomation_schema
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: 1468
Relay_Log_Space: 924
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: 0
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: 2000
Master_UUID: 7ff5c2ca-6b3c-11ed-9dd5-0242ac140003
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:
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)
从库4修改master
### 从库02
tester@server01:~$ mysql -uroot -p123456 -h192.168.79.177 -P13356
# 修改 master
mysql> change master to master_host='mysql8_master2000',master_user='binlog_worker',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=1087;
Query OK, 0 rows affected, 7 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)
# 如果Slave_IO_Running: No Slave_SQL_Running: Yes 则查看server_id 是否唯一
# 则执行 stop slave, 重新 change master to ... 再次 start slave;
# mysql> stop slave;
# 此处不带 ';'
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: mysql8_master2000
Master_User: binlog_worker
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1468
Relay_Log_File: mysql-worker-relay-bin.000002
Relay_Log_Pos: 707
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
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: 1468
Relay_Log_Space: 924
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: 0
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: 2000
Master_UUID: 7ff5c2ca-6b3c-11ed-9dd5-0242ac140003
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:
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)
查看主库1
mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 1002 | | 3306 | 1000 | 645187f2-6b3e-11ed-9e05-0242ac140005 |
| 1001 | | 3306 | 1000 | 525fc5ee-6b3e-11ed-9f03-0242ac140004 |
| 2000 | | 3306 | 1000 | 7ff5c2ca-6b3c-11ed-9dd5-0242ac140003 |
+-----------+------+------+-----------+--------------------------------------+
3 rows in set, 1 warning (0.00 sec)
查看主库2
mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 2002 | | 3306 | 2000 | 93c250da-6b3e-11ed-9d68-0242ac140007 |
| 2001 | | 3306 | 2000 | 8f88f87d-6b3e-11ed-9deb-0242ac140006 |
| 1000 | | 3306 | 2000 | 021d571c-6b3a-11ed-9d45-0242ac140002 |
+-----------+------+------+-----------+--------------------------------------+
3 rows in set, 1 warning (0.00 sec)
主库1创建新库,从库1从库2,主库2都会同步新库
备注
在主服务器上最重要的二进制日志设置是sync_binlog,这使得mysql在每次提交事务的时候把二进制日志的内容同步到磁盘上,即使服务器崩溃也会把事件写入日志中。
sync_binlog
不仅影响到 binlog 对MySQL所带来的性能损耗,同时也影响到MySQL中数据的完整性。
sync_binlog=0
,当事务提交之后,MySQL不做fsync之类的磁盘同步指令刷新binlog_cache中的信息到磁盘,而让Filesystem自行决定什么时候来做同步,或者cache满了之后才同步到磁盘。sync_binlog=n
,当每进行n次事务提交之后,MySQL将进行一次fsync之类的磁盘同步指令来将binlog_cache中的数据强制写入磁盘。- 在MySQL中系统默认的设置是
sync_binlog=0
,也就是不做任何强制性的磁盘刷新指令,这时候的性能是最好的,但是风险也是最大的。因为一旦系统Crash,在binlog_cache中的所有binlog信息都会被丢失。而当设置为“1”的时候,是最安全但是性能损耗最大的设置。因为当设置为1的时候,即使系统Crash,也最多丢失binlog_cache中未完成的一个事务,对实际数据没有任何实质性影响。 sync_binlog
值为 0/1 系统写入性能差距可能高达5倍甚至更多。
通常不会使用同一个数据库操作账号如以上root
账号处理同步日志,会在从库1从库2分别新建一个账号用于同步主库数据。
相关配置参数 如XXX 使用 show variables like '%XXX%'
查看;
mysql> show variables like '%binlog%';
+------------------------------------------------+----------------------+
| Variable_name | Value |
+------------------------------------------------+----------------------+
| binlog_cache_size | 4194304 |
| binlog_checksum | CRC32 |
| binlog_direct_non_transactional_updates | OFF |
| binlog_encryption | OFF |
| binlog_error_action | ABORT_SERVER |
| binlog_expire_logs_auto_purge | ON |
| binlog_expire_logs_seconds | 0 |
| binlog_format | ROW |
| binlog_group_commit_sync_delay | 0 |
| binlog_group_commit_sync_no_delay_count | 0 |
| binlog_gtid_simple_recovery | ON |
| binlog_max_flush_queue_time | 0 |
| binlog_order_commits | ON |
| binlog_rotate_encryption_master_key_at_startup | OFF |
| binlog_row_event_max_size | 8192 |
| binlog_row_image | FULL |
| binlog_row_metadata | MINIMAL |
| binlog_row_value_options | |
| binlog_rows_query_log_events | OFF |
| binlog_stmt_cache_size | 32768 |
| binlog_transaction_compression | OFF |
| binlog_transaction_compression_level_zstd | 3 |
| binlog_transaction_dependency_history_size | 25000 |
| binlog_transaction_dependency_tracking | COMMIT_ORDER |
| innodb_api_enable_binlog | OFF |
| log_statements_unsafe_for_binlog | ON |
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| sync_binlog | 1 |
+------------------------------------------------+----------------------+
30 rows in set (0.01 sec)
常见错误码
- 1007:数据库已存在,创建数据库失败
- 1008:数据库不存在,删除数据库失败
- 1050:数据表已存在,创建数据表失败
- 1051:数据表不存在,删除数据表失败
- 1054:字段不存在,或程序文件跟数据库有冲突
- 1060:字段重复,导致无法插入
- 1061:重复键名
- 1062 Error ‘Duplicate entry ‘X’ for key ‘PRIMARY’ on query
- 1068:定义了多个主键
- 1094:位置线程ID
- 1146:数据表缺失,请恢复数据库
- 1053:复制过程中主服务器宕机
- 1062:主键冲突 Duplicate entry ‘%s’ for key %d