故障回顾
# 启动脚本,模拟数据库的用户写入
#!/bin/bash
mysql -uroot -p123 -e "drop database if exists prod;"
mysql -uroot -p123 -e "create database if not exists prod;"
mysql -uroot -p123 -e "create table if not exists prod.t1(id int);"
num=1
while true;do
mysql -uroot -p123 -e "insert into prod.t1 values($num);commit;"
((num++))
sleep 1
done
# 配置好延时后启动上面的脚本
# 创建库
mysql> create database time;
# 创建表
mysql> create table time.ttt(id int);
# 写入数据
mysql> insert into time.ttt values(1),(2),(3);
# 查看表内数据
mysql> select * from time.ttt;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
# 查看数据库的模拟写入数据
mysql> select * from prod.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
......
| 221 |
+------+
221 rows in set (0.00 sec)
# 模拟误操作删除库
mysql> drop database time;
步骤
# 1.停止延时从库的SQL线程(db02)
root@localhost[(none)]>stop slave sql_thread;
# 2.给延时从库做个全备
[root@db02 ~]# mysqldump -uroot -p123 -A | gzip > /tmp/lins.sql.gz
# 3.准备新环境(db04)
删除data目录
[root@db04 data]# mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/app/mysql/data
# 4.新环境创建用户
mysql> grant all on *.* to test@'%' identified by '123';
# 4.5临时关闭binlog
set sql_log_bin=0;
# 5.将旧库的全备恢复到新环境(如果全备文件很大,scp速度慢)
[root@db02 ~]# zcat /tmp/lins.sql.gz | mysql -utest -p123 -h10.0.0.54
# 6.截取relaylog drop time之前数据
[root@db02 data]# mysqlbinlog --base64-output=decode-rows -vvv /app/mysql/data/db02-relay-bin.000002 | grep -i 'drop' -C 5
# at 84691
#230807 16:37:31 server id 1 end_log_pos 84610 CRC32 0x08e2ec23 Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1691397451/*!*/;
SET @@session.pseudo_thread_id=5/*!*/;
drop database time
/*!*/;
# at 84783
# 6.2查找到全备到哪
[root@db02 data]# vim relay-log.info
./db02-relay-bin.000002
367
# 6.3截取binlog
[root@db02 data]# mysqlbinlog --start-position=367 --stop-position=84691 /app/mysql/data/db02-relay-bin.000002 > /tmp/inc11.sql
# 6.4导入数据
[root@db02 data]# mysql -utest -p123 -h10.0.0.54 < /tmp/inc11.sql
# 7.停止连接主库的程序
0)systemctl stop php-fpm
1) 挂维护页
写一个nginx配置文件,访问域名和正常网站一样
根据主配置文件内的include /etc/nginx/conf.d/*.conf;
正常访问配置文件和维护配置文件都不带.conf
使用ln -s blog blog.conf软连接的方式,配置nginx网站。
# 8.drop time之后到relaylog结束
[root@db02 data]# mysqlbinlog --base64-output=decode-rows -vvv /app/mysql/data/db02-relay-bin.000002 | tail
# at 326531
#230807 16:53:48 server id 1 end_log_pos 326389 CRC32 0x21ebe426 Xid = 5278
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
# 9.导出
[root@db02 data]# mysqlbinlog --start-position=84783 --stop-position=326531 /app/mysql/data/db02-relay-bin.000002 > /tmp/inc12.sql
# 10.导入数据
[root@db02 data]# mysql -utest -p123 -h10.0.0.54 < /tmp/inc12.sql
# 10.5开启binlog
set sql_log_bin=1;
# 11.应用割接
新环境dump出来,还原到旧环境
1) 主库导出04恢复好的数据
mysqldump -A -R --triggers|gzip > /tmp/delay_ful1.sql.gz
----恢复到主库时不要关闭binlog,从库里也有删库命令,
2) 就用新环境,其他从库change Master,改代码