当前位置: 首页 > news >正文

主从复制mysql-replication | Replication故障排除

主从复制mysql-replication

准备环境

#防火墙 selinux
systemctl stop firewalld --now &&setenforce 0
#修改主机名:hostnamectl set-hostname 名字
tip:vim /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPRTOT=static
IPADDR=192.168.100.175
PREFIX=24
GATEWAY=192.168.100.2
DNS1=114.114.114.114
DNS2=8.8.8.8
### 添加|删除一个临时ip
#主机名解析 vim /etc/hosts     两台机器
ip地址   主机名
ip地址 	主机名

清理环境

yum erase -y mariadb mysql
rm -rf /etc/my* && rm -rf /var/lib/mysql && rm -rf /usr/bin/mysql
#检查一下
[[ ! -f /etc/my.cnf ]] && [[ ! -d /var/lib/mysql ]] && [[ ! -f /usr/bin/mysql ]] && echo "环境已经清理完成" || echo "环境未清理"

安装数据库

#1yum安装
#2启动
#3修改密码
mysqladmin -uroot -p'`awk '/temporary password/{p=$NF}END{print p}' /var/log/mysqld.log`' password 'Qq111111.'

无数据[master]

#开启binlog
[root@master ~]# mkidr -pv /data/
[root@master ~]# chown mysql.mysql /data
[root@master ~]# vim /etc/my.cnf
server-id=203
log-bin=/data/mysql-bin
# 重启数据库
# 创建一个账号
mysql> grant replication slave on *.* to 'relication'@'%' identified by 'Qianfeng@123';
mysql> flush privileges;
# 查看当前binlog日志文件以及pos位置点
mysql> show master status\G
*************************** 1. row ***************************File: mysql-bin.000001Position: 154Binlog_Do_DB: Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

从库

#配置文件中增加server-id
[root@slave ~]# vim /etc/my.cnf
server-id=204
[root@slave ~]# systemctl restart mysqld
mysql> ? change master to
mysql> CHANGE MASTER TOMASTER_HOST='master',MASTER_USER='relication',MASTER_PASSWORD='Qianfeng@123',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154,MASTER_CONNECT_RETRY=10;
# 启动slave
mysql>start slave;
# 查看主从是否成功
mysql> show slave status\G
## 14 15行。两个YES表示主从成功
## 关注39-42的信息
## 38行表示主从复制延迟时间
## 46 行UUID要不一致
mysql> show slave status\G 
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: masterMaster_User: relicationMaster_Port: 3306Connect_Retry: 10Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 154Relay_Log_File: slave-relay-bin.000002Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 527Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 203Master_UUID: 6f6d0551-a073-11ee-9f28-000c298a6e96Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 
1 row in set (0.00 sec)

重新配置主从

mysql>stop slave;
mysql> reset slave;
mysql>CHANGE MASTER TOMASTER_HOST='master',MASTER_USER='relication',MASTER_PASSWORD='Qianfeng@123',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154,MASTER_CONNECT_RETRY=10;
## 启动slave
mysql>start slave;

有数据

主库

#锁表备份
mysql> grant replication slave on *.* to 'repl'@'%' identified by 'Qq12345.';
mysql>flush privileges;
# 创建必要目录
[root@master ~]# mkdir /data
[root@master ~]# chown mysql.mysql /data
# 修改配置文件
[root@master ~]# vim /etc/my.cnf
server-id = 203
log-bin = /data/mysql-bin
# 重启数据库
[root@master ~]# systemctl restart mysqld
#锁表备份
mysql>flush tables with read lock;
# 新开一会终端窗口
[root@master ~]# mysqldump -uroot -pQq111111. -A > all.sql
[root@master ~]# 远程拷贝至从库
#查看二进制日志文件以及位置点信息
mysql>show master status\G
*************************** 1. row ***************************File: mysql-bin.000001Position: 154Binlog_Do_DB: Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

从库

#导入数据
[root@slave ~]# mysql -uroot -pQq111111. < all.sql
#配置主从
[root@slave ~]# vim /etc/my.cnf
server-id = 204
# 重启数据库
[root@slave ~]# systemctl restart mysqld
mysql>change master to
master_host='master',
master_user='repl',
master_password='Qq12345.',
master_port=3306,
master_log_file='mysql-bin.000001',
master_log_pos=154;
#启动slave
mysql>start slave;
# 查看主从状态
mysql> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: masterMaster_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 154Relay_Log_File: slave-relay-bin.000002Relay_Log_Pos: 320Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 527Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 203Master_UUID: 5aad69d6-a09e-11ee-b909-000c298a6e96Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 

解锁

unlock tables; 
或者直接退出

跳过错误码

vim /etc/my.cnf
slave-skip-errors=1062

Replication故障排除

开启 GTID 后的导出导入数据的注意点

mysqldump -uroot  -p  --set-gtid-purged=OFF   --all-databases > alldb.db

UUID一致,导致主从复制I/O线程不是yes

主库:

mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |
+---------------+-------+

从库:

mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+
1 row in set (0.00 sec)

server_id不一样,排除。

检查主从状态:

主库:

mysql> show master status\G
*************************** 1. row ***************************File: mysql-bin.000002Position: 849Binlog_Do_DB: Binlog_Ignore_DB: 
Executed_Gtid_Set: 21c27a81-633b-11ea-8d7d-00163e064efa:1-3
1 row in set (0.00 sec)

从库:

mysql> show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.31.47.161Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000002Read_Master_Log_Pos: 849

File一样,排除。

最后检查发现他们的auto.cnf中的server-uuid是一样的。

[root@localhost ~]# vim /var/lib/mysql/auto.cnf
[auto]
server-uuid=4f37a731-9b79-11e8-8013-000c29f0700f

修改uuid并重启服务

https://blog.csdn.net/NewRain_wang/article/details/104790631

http://www.lryc.cn/news/266209.html

相关文章:

  • 基于Java SSM框架实现教学质量评价评教系统项目【项目源码+论文说明】计算机毕业设计
  • 03|模型I/O:输入提示、调用模型、解析输出
  • springcloud-gateway-2-鉴权
  • 实现一个最简单的内核
  • 2024华为OD机试真题指南宝典—持续更新(JAVAPythonC++JS)【彻底搞懂算法和数据结构—算法之翼】
  • 【12.23】转行小白历险记-算法02
  • k8s部署nginx-ingress服务
  • SpringBoot Elasticsearch全文搜索
  • Python 常用模块re
  • 【华为OD题库-106】全排列-java
  • Three.js 详细解析(持续更新)
  • Unity中Shader平移矩阵
  • python dash 的学习笔记1
  • SQLITE如何同时查询出第一条和最后一条两条记录
  • 四、ensp配置ftp服务器实验
  • VS2020使用MFC开发一个贪吃蛇游戏
  • 【经典LeetCode算法题目专栏分类】【第9期】深度优先搜索DFS与并查集:括号生成、岛屿问题、扫雷游戏
  • 字符设备驱动开发-注册-设备文件创建
  • TrustZone之可信操作系统
  • java定义三套场景接口方案
  • idea连接数据库,idea连接MySQL,数据库驱动下载与安装
  • Redis-实践知识
  • 多维时序 | MATLAB实现SSA-CNN-SVM麻雀算法优化卷积神经网络-支持向量机多变量时间序列预测
  • leetcode160相交链表思路解析
  • 在线分析工具-日志优化
  • 硬核实战!mysql 错误操作整个表全部数据后如何恢复?附解决过程、思路(百万行SQL,通过binlog日志恢复)
  • 【什么是反射机制?为什么反射慢?】
  • PostGreSQL:货币类型
  • ESP8266网络相框采用TFT_eSPI库TJpg_Decoder库mixly库UDP库实现图片传送
  • Go 泛型发展史与基本介绍