MySQL的主从复制与主从切换详解
- 一、MySql主从节点的搭建
-
- [一、 master主节点的搭建](#一、 master主节点的搭建)
-
-
- [1. 查看是否开启bin_log日志](#1. 查看是否开启bin_log日志)
- [2. MySQL开启log_bin功能](#2. MySQL开启log_bin功能)
- [3. 创建主从复制用户并授权](#3. 创建主从复制用户并授权)
- [4. mysql数据库状态查看](#4. mysql数据库状态查看)
- [5. 主节点锁表并进行数据备份](#5. 主节点锁表并进行数据备份)
-
- 二、solve从节点的搭建
-
-
- [1. 配置文件添加内容](#1. 配置文件添加内容)
- [2. 同步主库数据并授权](#2. 同步主库数据并授权)
- [3. 主库解除锁定](#3. 主库解除锁定)
- [4. 开启从节点](#4. 开启从节点)
- [5. 检查从节点状态](#5. 检查从节点状态)
- [6. 确认主从复制开启成功](#6. 确认主从复制开启成功)
-
- [1. 查看主节点相关信息](#1. 查看主节点相关信息)
- [2. 查看从节点相关信息](#2. 查看从节点相关信息)
-
- [二、主从同步失败问题定位,Slave_IO_Running: No](#二、主从同步失败问题定位,Slave_IO_Running: No)
- 三、MSQL主从切换操作
-
- [1、 从库10.8.109.166上执行操作如下](#1、 从库10.8.109.166上执行操作如下)
- 2、主库10.8.109.165执行操作如下
一、MySql主从节点的搭建
一、 master主节点的搭建
1. 查看是否开启bin_log日志
注: 若log_bin为ON表示已启用bin_log日志,为OFF表示未开启,则需执行下一步开启log_bin功能
mysql> show global variables like 'log_bin%';
+---------------------------------+------------------------------+
| Variable_name | Value |
+---------------------------------+------------------------------+
| log_bin | ON |# ON为开启,OFF关闭
| log_bin_basename | /mysqld/data/mysql-bin |
| log_bin_index | /mysqld/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+------------------------------+
2. MySQL开启log_bin功能
- 修改mysql配置文件my.cnf 添加内容如下
bash
[root@localhost ~]# vi my.cnf
[mysqld]
log-bin=mysql-bin #指定并激活mysql二进制文件名称
server-id = 1 #设置server-id 为1 代表开启二进制,若为0代表禁止二进制,mysql8.0默认为1,8.0以前默认为0
-
此台服务器mysql完整配置在这里!
[mysqld]
skip-name-resolve
default_storage_engine = InnoDB
basedir = /opt/rdapp/mysqld
datadir = /opt/rdapp/mysqld/data
port=3306
#socket = /opt/rdapp/mysqld/pid/mysql.sock
pid-file = /opt/rdapp/mysqld/pid/mysql.pid
log-error = /opt/rdapp/mysqld/logs/err.log
log_error_verbosity = 3
default_authentication_plugin = mysql_native_password
skip-log-bin
back_log = 600 #mysql等待连接的堆栈大小
lower_case_table_names = 1 #不区分表的大小写
max_allowed_packet = 1024M #接收数据包的大小
binlog_cache_size = 8M
max_connections = 1000 #最大连接数
open_files_limit = 65535 #打开文件描述符限制
table_open_cache = 1024 #表缓存
read_buffer_size = 2M #读入缓冲区大小
read_rnd_buffer_size = 8M #随机读取缓冲区大小
sort_buffer_size = 8M #执行排序使用的缓冲区大小
join_buffer_size = 8M #联合查询操作使用的缓冲区大小
thread_cache_size = 64 #可以重新利用保存在缓存中线程的数量
key_buffer_size = 4M #指定���于索引的缓冲区大小
slow_query_log = 1 #慢日志
long_query_time = 3 #慢查询时间,超过3秒则为慢查询
slow_query_log_file= /opt/rdapp/mysqld/logs/slow.log #慢日志保存位置
default-storage-engine = InnoDB #默认存储引擎
innodb_buffer_pool_size = 512M #innodb缓冲池大小
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0 #默认为0,表示不限制并发数
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2 #日志刷新级别
innodb_log_buffer_size = 2M
innodb_log_file_size = 64M
innodb_log_files_in_group = 3 #以以循环方式将日志文件写到多个文件。推荐设置为3
innodb_max_dirty_pages_pct = 90 #innodb主线程刷新缓存池中的数据,使脏数据比例小于90%
innodb_lock_wait_timeout = 120 #InnoDB事务在被回滚之前可以等待一个锁定的超时秒数
log_bin = mysql-bin
#binlog_format = mixed
binlog_expire_logs_seconds = 2592000 #超过30天的binlog删除
log-bin-trust-function-creators = 1
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
character_set_server = utf8mb4
#collation_server = utf8mb4_general_ci
general_log = on
general_log_file = /opt/rdapp/mysqld/logs/general.log
plugin-load-add = connection_control.so
interactive_timeout = 900
wait_timeout = 900
connection_control_failed_connections_threshold = 0
connection_control_min_connection_delay = 15000
connection_control_max_connection_delay = 30000
#skip-name-resolve
#host_cache_size=10#MGR集群
disabled_storage_engines = "MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id = 2
gtid_mode = ON
enforce_gtid_consistency = ON
binlog_checksum = NONE
log_slave_updates = ON
binlog_format = ROW
#sql_require_primary_key = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
transaction_write_set_extraction = XXHASH64
plugin_load_add='group_replication.so'
group_replication_group_name = "f5b933ac-d49d-11eb-b9bf-005056863e69"
group_replication_start_on_boot = on
group_replication_local_address = "mysqlnode2.service.rdapp.internal:33061"
group_replication_group_seeds = "mysqlnode1.service.rdapp.internal:33061,mysqlnode2.service.rdapp.internal:33061"
group_replication_bootstrap_group = off
report_host = "mysqlnode2.service.rdapp.internal"
group_replication_transaction_size_limit = 150000000 #组复制最大事务大小大小限制
group_replication_consistency = before
group_replication_communication_max_message_size = 10M
group_replication_flow_control_certifier_threshold = 25000
group_replication_autorejoin_tries = 10
group_replication_member_expel_timeout = 60
binlog_transaction_dependency_tracking = writeset
slave_parallel_type = LOGICAL_CLOCK
slave_preserve_commit_order = ON
slave_checkpoint_period = 2
slave_checkpoint_group = 256[client]
port = 3306
socket = /opt/rdapp/mysqld/pid/mysql.sock[mysqldump]
quick
max_allowed_packet = 512M #服务器发送和接受的最大包长度
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
3. 创建主从复制用户并授权
bash
# 登录作为主节点的mysql
[root@localhost mysqld]# /opt/rdapp/mysqld/mysqld-os/mysqld/bin/mysql -h 10.8.109.165 -urdapp -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.25 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.
# mysql内输入下面的命令
mysql> create user 'repl'@'%' identified by 'Nucleus!';
Query OK, 0 rows affected (0.02 sec)
mysql> grant replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.41 sec)
mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| hl | % |
| rdapp | % |
| repl | % |
| auditor | 127.0.0.1 |
| reraat | 127.0.0.1 |
| security | 127.0.0.1 |
| systemadmin | 127.0.0.1 |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| rdapp | localhost |
+------------------+-----------+
11 rows in set (0.00 sec)
4. mysql数据库状态查看
bash
# 查看mysql的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
# 查看log_bin信息
mysql> show global variables like 'log_bin%';
+---------------------------------+------------------------------+
| Variable_name | Value |
+---------------------------------+------------------------------+
| log_bin | ON |
| log_bin_basename | /mysqld/data/mysql-bin |
| log_bin_index | /mysqld/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+------------------------------+
# 查看主节点的状态信息
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 156 | | | |
+------------------+----------+--------------+------------------+-------------------+
# mysql-bin.000004 指二进制的名称
# 数据库的变更标识
# 复制哪个库
# 不允许复制哪个库
# 是否激活gtid
5. 主节点锁表并进行数据备份
- 备份数据后面导入从节点,保证主从数据库起始数据一致
bash
[root@localhost ~]# /opt/rdapp/mysqld/mysqld-os/mysqld/bin/mysql -h 10.8.109.165 -urdapp -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.25 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.
#锁表
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
#数据备份
[root@localhost ~]# /opt/rdapp/mysqld/mysqld-os/mysqld/bin/mysqldump -h127.0.0.1 -ureraat -p --all-databases --triggers --routines --events > /root/data.sql
Enter password:
#解表
[root@localhost ~]# UNLOCK TABLES;
二、solve从节点的搭建
1. 配置文件添加内容
- 修改mysql配置文件my.cnf 添加内容如下
bash
[root@lopocalhost mysqld]# vi my.cnf nnnnnnnn
[mysqld]
server-id = 21 # server-id必须是唯一的,与master节点不同就可以
2. 同步主库数据并授权
bash
# 登录将作为从节点的mysql
[root@localhost mysqld]# /opt/rdapp/mysqld/mysqld-os/mysqld/bin/mysql -h 10.8.109.166 -urdapp -pxxx
# mysql内输入下面的命令
# 从库导入主库导出的数据data.sql,确保主从数据库内容一致
mysql> source /root/data.sql ;
mysql> CHANGE MASTER TO
-> master_host='10.8.109.165',
-> MASTER_USER='repl',
-> master_password='Nucleus!',
-> MASTER_LOG_FILE='mysql-bin.000005',
-> MASTER_LOG_POS=1;
Query OK, 0 rows affected, 7 warnings (0.00 sec)
3. 主库解除锁定
bash
[root@localhost ~]# /opt/rdapp/mysqld/mysqld-os/mysqld/bin/mysql -h 10.8.109.165 -urdapp -pxxx
#解表
[root@localhost ~]# UNLOCK TABLES;
4. 开启从节点
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
5. 检查从节点状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.8.109.165
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 156
Relay_Log_File: localhost-relay-bin.000005
Relay_Log_Pos: 371
Relay_Master_Log_File: mysql-bin.000010
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: 156
Relay_Log_Space: 752
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: 1
Master_UUID: 400f2c2b-e2bb-11ee-8f85-00505686c1d1
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave 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)
ERROR:
No query specified
mysql> show variables like 'server_uuid';
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | 05e7063e-908d-11ec-9916-00505686574e |
+---------------+--------------------------------------+
1 row in set (0.00 sec)
6. 确认主从复制开启成功
1. 查看主节点相关信息
bash
mysql> show processlist;
+----+-----------------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| 5 | event_scheduler | localhost | NULL | Daemon | 316 | Waiting on empty queue | NULL |
| 14 | rdapp | 10.8.109.165:44440 | NULL | Query | 0 | init | show processlist |
| 19 | repl | 10.8.109.166:48682 | NULL | Binlog Dump | 151 | Master has sent all binlog to slave; waitingfor more updates | NULL |
+----+-----------------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)
mysql> show databases;
2. 查看从节点相关信息
bash
mysql> show processlist;
+----+-----------------+--------------------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+--------------------+------+---------+------+--------------------------------------------------------+------------------+
| 5 | system user | connecting host | NULL | Connect | 132 | Waiting for master to send event | NULL |
| 6 | system user | | NULL | Query | 132 | Slave has read all relay log; waiting for more updates | NULL |
| 7 | event_scheduler | localhost | NULL | Daemon | 132 | Waiting on empty queue | NULL |
| 11 | rdapp | 10.8.109.165:38224 | orgs | Sleep | 5 | | NULL |
| 15 | rdapp | 10.8.109.166:40990 | NULL | Query | 0 | init | show processlist |
+----+-----------------+--------------------+------+---------+------+--------------------------------------------------------+------------------+
5 rows in set (0.00 sec)
二、主从同步失败问题定位,Slave_IO_Running: No
# "Slave_IO_Running: Yes" 和 "Slave_SQL_Running: Yes" 都是Yes才是正确的,否则都主从都有问题
# 查看从节点状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 10.8.109.165
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 4
Relay_Log_File: server2-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: No
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: 4
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: 13117
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. ## 停掉备库实例,删除备库 data 文件夹下的的 auto.cnf
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID:
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 240315 10:44:01
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
问题:
* Last_Error: Error 'Operation CREATE USER failed for 'repl'@'%'' on query. Default database: ''. Query: 'CREATE USER 'repl'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*1E63BECE0EBF96B7F6F0167BB416443EF9079EB8''
1. 查看server_id是否不同
mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 2 |
+---------------+-------+
1 row in set (0.00 sec)
2. 用户授权是否正确
3. uuid也需要不同
解决方法:停掉备库实例,删除备库 data 文件夹下的的 auto.cnf
具体操作如下:
[root@localhost data]# ls
auto.cnf client-cert.pem #ib_16384_0.dblwr ibdata1 ib_logfile2 master1.pid mysql-bin.000006 performance_schema server-cert.pem undo_001
ca-key.pem client-key.pem #ib_16384_1.dblwr ib_logfile0 ibtmp1 mysql mysql-bin.index private_key.pem server-key.pem undo_002
ca.pem cms ib_buffer_pool ib_logfile1 #innodb_temp mysql-bin.000005 mysql.ibd public_key.pem sys
[root@localhost data]# rm -rf auto.cnf
[root@localhost data]# systemctl restart mysqld
[root@localhost data]# /opt/rdapp/mysqld/mysqld-os/mysqld/bin/mysql -h 10.8.109.165 -urdapp -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.25 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.
#查看uuid
mysql> show variables like 'server_uuid';
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | 400f2c2b-e2bb-11ee-8f85-00505686c1d1 |
+---------------+--------------------------------------+
1 row in set (0.01 sec)
三、MSQL主从切换操作
1、 从库10.8.109.166上执行操作如下
# 停止从库的运行
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 清空主从信息
mysql> reset slave all;
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 关闭只读开启读写,转为新主库
mysql> set global read_only=off;
Query OK, 0 rows affected (0.00 sec)
mysql> set global super_read_only=off;
Query OK, 0 rows affected (0.00 sec)
# 查看二进制binlog文件的名称,此处是 mysql-bin.000006 (后面主节点切换到从要用)
mysql> show master status ;
+------------------+----------+--------------+------------------+--------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+--------------------------------------------+
| mysql-bin.000006 | 87795 | | | 400f2c2b-e2bb-11ee-8f85-00505686c1d1:1-136 |
+------------------+----------+--------------+------------------+--------------------------------------------+
2、主库10.8.109.165执行操作如下
# 主库设置执行新主库复制链路,转为新从库,完成主从切换(注意:此处的MASTER_LOG_FILE='mysql-bin.000006'中mysql-bin.000006为上一步骤在原来的从节点查询到的结果)
mysql> CHANGE MASTER TO
-> master_host='10.8.109.165',
-> MASTER_USER='repl',
-> master_password='Nucleus!',
-> MASTER_LOG_FILE='mysql-bin.000006',
-> MASTER_LOG_POS=1;
Query OK, 0 rows affected, 7 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: Waiting for master to send event
Master_Host: 100.66.101.5
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 87795
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 371
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes #此处必须为yes代表成功
Slave_SQL_Running: Yes #此处必须为yes代表成功
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
............................................................省略