赞
踩
概念:
MYSQL 主从复制是 MYSQL 集群模式中应用最广泛的一种。
可解决的问题:
主从原理:
条件:
原理:
slave start
,Slave IO 线程会通过在 Master 上创建的授权用户连接至 Master,并请求 Master 从指定的文件和 position 发送 bin-log 日志内容。分析:网络不稳定,数据量过大等原因导致。
解决方案:
从库忽略错误,继续执行。
> stop slave;
> global sql_slave_skip_counter = [n]
> start slave;
重做主从,完全同步。
> flush tables with read lock;
> unlock tables;
方法 2 同样适用于新增从库。
方法:
两台 CentOS 7 虚拟机。
配置好实验环境。
克隆系统。
配置克隆系统。
配置 yum 源。
下载 rpm 包:$ wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
生成源:$ rpm -ivh mysql80-community-release-el7-7.noarch.rpm
更新 GPG-KEY:$ rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
关闭防火墙和 selinux。
关闭 selinux:先执行$ setenforce 0
,再修改配置文件/etc/selinux/config
# disabled 关闭
# enforcing 开启
SELINUX=disabled
关闭防火墙:
$ systemctl stop firewalld
$ systemctl disable firewalld
安装 mysql 8:$ yum -y install mysql-community-server
2~4 步可使用 xshell -> 工具 -> 发送键输入到 -> 所有会话,同步完成。
编辑配置文件:/etc/my.cnf
Master 编辑内容:
[mysqld]
# 数据库 GID 不可重复
server-id = 110
# 日志文件前缀
log-bin = mysql-bin
Slave 编辑内容:
[mysqld]
# 数据库 GID 不可重复
server-id = 111
重启数据库:$ systemctl restart mysqld
查看主库 Master 状态:> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 477 | | | |
+------------------+----------+--------------+------------------+-------------------+
查看主库日志文件:$ mysqlbinlog /var/lib/mysql/mysql-bin.000001 | more
at [n]
:起始点为 n。end_log_pos [n]
:结束点为 n。登录主库创建从库连接账号:> create user 'tongbu'@'%' identified with mysql_native_password by 'WahahaAyaya123!';
给账号授权:> grant replication slave on *.* to 'tongbu'@'%';
查看主库 Master 状态:
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1573 | | | |
+------------------+----------+--------------+------------------+-------------------+
登录从库设置 Master 信息:
> change master to master_host='192.168.0.110',
master_user='tongbu',
master_password='WahahaAyaya123!',
master_log_file='mysql-bin.000001',
master_log_pos=1573;
从库开启同步:> start slave;
查看 slave 状态信息:> show slave status\G
-- Slave_IO_Running 和 Slave_SQL_Running 值都为 Yes 表示搭建成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
测试:
> create database test_02;
> show databases;
创建测试数据库:> cerate database test_01;
删除测试数据库:> drop database test_01;
查看 binlog 文件,记录需要还原的起始点和结束点:at 554
,end_log_pos 671
将要恢复的内容导入到文件中:$ mysqlbinlog --start-position=554 --stop-position=671 /var/lib/mysql/mysql-bin.000001 > bak.sql
恢复数据:mysql -p'WahahaAyaya123!' < bak.sql
登录数据库查看内容是否恢复:
```sql {.line-numbers}
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test_01 |
+--------------------+
5 rows in set (0.00 sec)
```
删除 Master 库数据:> drop database test_02;
查看 Slave 状态:> show slave status;
-- 主从关系已被破坏
Slave_IO_Running: Yes
Slave_SQL_Running: No
Last_Error: Coordinator stopped because there were error(s) in the worker(s).
The most recent failure being: Worker 1 failed executing transaction 'ANONYMOUS' at
master log mysql-bin.000005, end_log_pos 1108. See error log and/or
performance_schema.replication_applier_status_by_worker table for
more details about this failure or others, if any.
关闭 Slave 同步:> stop slave;
忽略该错误:> set global sql_slave_skip_counter = 1;
启动 Slave 同步:> start slave;
再次查看 Slave 状态:
-- 主从关系已恢复
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Slave_IO_State: Waiting for source to send event -- I/O 线程与 Master 的连接状态 Master_Host: 192.168.0.110 -- Master IP Master_User: tongbu -- Master 用户 Master_Port: 3306 -- Master 端监听端口 Connect_Retry: 60 -- 超时重连间隔 Master_Log_File: mysql-bin.000005 -- I/O 线程读取的 Master 二进制日志名 Read_Master_Log_Pos: 157 -- I/O 线程读取的 Master 二进制日志点位 Relay_Log_File: centos7-111-relay-bin.000006 -- SQL 线程读取的中继日志名 Relay_Log_Pos: 326 -- SQL 线程读取的中继日志点位 Relay_Master_Log_File: mysql-bin.000005 -- SQL 线程执行的 Master 二进制日志名 Slave_IO_Running: Yes -- I/O 线程是否启动并成功连接到 Master Slave_SQL_Running: Yes -- SQL 线程是否启动 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 -- SQL_SLAVE_SKIP_COUNTER 设置的值 Exec_Master_Log_Pos: 157 Relay_Log_Space: 758 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 -- SQL 线程和 I/O 线程之间的时间差,单位为秒,常用于检查主从延迟 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: 110 Master_UUID: f846acf1-7321-11ed-9d3f-000c29ef3be5 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:
mysql -u[用户名] -p[密码] -e 'show slave status\G' | grep 'Seconds_Behind_Master'
mysql -u[用户名] -p[密码] -e 'show slave status\G' | grep -Ew 'Slave_IO_Running|Slave_SQL_Running'
表现:
Slave_IO_State: Connecting to source
Master_Host: 192.168.0.110
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 985
Relay_Log_File: centos7-111-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
排查:
解决方案:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。