mysql主库清理数据,从库保留
2023-09-14 08:59:49 时间
因为业务需要,想在mysql主库清理一些数据,但从库想要保留,根据网友介绍,可以根据binlog跳过清理的命令
1.确保主从同步的情况下,主库开始操作
mysql flush logs; --刷新日志,切换一个新的binlog日志,比较小,后面修改就会方便些
Query OK, 0 rows affected (0.21 sec)
mysql show master status \G
*************************** 1. row ***************************
File: mysql-bin.000039 --这里的binlog位置后面不会用到
Position: 33958
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.03 sec)
2.从库停止同步 ---第1.2步尽量要快速操作
mysql stop slave;
Query OK, 0 rows affected (0.05 sec)
mysql show slave status \G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.196
Master_User: repli
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000039
Read_Master_Log_Pos: 488906
Relay_Log_File: mysql-relay-bin.000016
Relay_Log_Pos: 489119
Relay_Master_Log_File: mysql-bin.000039
Slave_IO_Running: No
Slave_SQL_Running: No
3.主库清空数据
mysql truncate table t2; 步骤a
Query OK, 0 rows affected (0.46 sec)
mysql show master status \g 步骤b
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000039 | 1942762 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.06 sec)
/usr/local/mysql/bin/mysqlbinlog mysql-bin.000039 000039.sql 步骤c
4.编辑000039.sql,删掉truncate语句
truncate table t2 /*!*/; ---删掉的内容
查找1942762 找不到该位置的话,可能会丢失数据,因此前面的三步abc一定要注意顺序!最好sql里面是包含这个位置信息的,然后删掉后面的内容,避免后面日志应用的时候,重复操作。
end_log_pos 1942762 将这个位置后面的内容全部干掉!
5.从库恢复
/usr/local/mysql/bin/mysql -umydba -p
source /root/000039.sql
mysql change master to master_log_file=mysql-bin.000039,master_log_pos=1942762;
Query OK, 0 rows affected (0.19 sec)
mysql start slave;
Query OK, 0 rows affected (0.01 sec)
mysql show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.196
Master_User: repli
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000039
Read_Master_Log_Pos: 10009221
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 8066779
Relay_Master_Log_File: mysql-bin.000039
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: 10009221
Relay_Log_Space: 8066986
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: 701cbadc-ba33-11e5-9091-305a3a78baf2
Master_Info_File: /usr/local/mysql/data/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:
1 row in set (0.00 sec)
---主从同步,跳过了truncate这个命令
6.验证
主库:
mysql select * from t2;
Empty set (0.00 sec)
从库:
mysql select * from t2;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+----+
5 rows in set (0.00 sec)
MySQL从库维护经验分享 MySQL 主从架构应该是最常用的一组架构了。从库会实时同步主库传输来的数据,一般从库可以作为备用节点或作查询使用。其实不只是主库需要多关注,从库有时候也要经常维护,本篇文章将会分享几点从库维护经验,一起来学习吧。
1.确保主从同步的情况下,主库开始操作
mysql flush logs; --刷新日志,切换一个新的binlog日志,比较小,后面修改就会方便些
Query OK, 0 rows affected (0.21 sec)
mysql show master status \G
*************************** 1. row ***************************
File: mysql-bin.000039 --这里的binlog位置后面不会用到
Position: 33958
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.03 sec)
2.从库停止同步 ---第1.2步尽量要快速操作
mysql stop slave;
Query OK, 0 rows affected (0.05 sec)
mysql show slave status \G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.196
Master_User: repli
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000039
Read_Master_Log_Pos: 488906
Relay_Log_File: mysql-relay-bin.000016
Relay_Log_Pos: 489119
Relay_Master_Log_File: mysql-bin.000039
Slave_IO_Running: No
Slave_SQL_Running: No
3.主库清空数据
mysql truncate table t2; 步骤a
Query OK, 0 rows affected (0.46 sec)
mysql show master status \g 步骤b
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000039 | 1942762 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.06 sec)
/usr/local/mysql/bin/mysqlbinlog mysql-bin.000039 000039.sql 步骤c
4.编辑000039.sql,删掉truncate语句
truncate table t2 /*!*/; ---删掉的内容
查找1942762 找不到该位置的话,可能会丢失数据,因此前面的三步abc一定要注意顺序!最好sql里面是包含这个位置信息的,然后删掉后面的内容,避免后面日志应用的时候,重复操作。
end_log_pos 1942762 将这个位置后面的内容全部干掉!
5.从库恢复
/usr/local/mysql/bin/mysql -umydba -p
source /root/000039.sql
mysql change master to master_log_file=mysql-bin.000039,master_log_pos=1942762;
Query OK, 0 rows affected (0.19 sec)
mysql start slave;
Query OK, 0 rows affected (0.01 sec)
mysql show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.196
Master_User: repli
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000039
Read_Master_Log_Pos: 10009221
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 8066779
Relay_Master_Log_File: mysql-bin.000039
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: 10009221
Relay_Log_Space: 8066986
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: 701cbadc-ba33-11e5-9091-305a3a78baf2
Master_Info_File: /usr/local/mysql/data/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:
1 row in set (0.00 sec)
---主从同步,跳过了truncate这个命令
6.验证
主库:
mysql select * from t2;
Empty set (0.00 sec)
从库:
mysql select * from t2;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+----+
5 rows in set (0.00 sec)
MySQL从库维护经验分享 MySQL 主从架构应该是最常用的一组架构了。从库会实时同步主库传输来的数据,一般从库可以作为备用节点或作查询使用。其实不只是主库需要多关注,从库有时候也要经常维护,本篇文章将会分享几点从库维护经验,一起来学习吧。
相关文章
- Mysql远程连接数据库报错排查:pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '**.**.**.**' (timed out)")
- MySQL_(Java)使用JDBC向数据库中修改(update)数据
- 【异常】ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
- mysql 分组加序号
- 【整理】源码安装 mysql-5.6.10
- MySQL超时参数以及相关数据集成、DataX数据同步案例分享
- 【问题解决方案】MySQL安装后无法启动-net start mysql服务名无效
- mysql启动时报错:Starting MySQL... ERROR! The server quit without updating PID file (/opt/mysql/data/mysql.pid) 的解决方法
- MySQL 批量插入数据,单次插入多少条数据效率最高
- MySQL优化union查询
- Hadoop sqoop从MySQL导入数据到HDFS
- MySQL复习资料(出口考试)
- 云数据库RDS_MySQL购买流程_以及购买步骤的建议
- Python:PyMySQL模块读写MySQL数据
- mysql优化方法积累
- paip.将数据导入到在英语语音数据库mysql道路解决空原则问题
- 4.5万字手把手教你实现MySQL TB级数据存储!!
- 安全测试===Mysql 注入技巧学习 MySQL注入技巧(1)
- 如何实现MySQL表数据随机读取?从mysql表中读取随机数据
- Mysql求最小正整缺失值
- Mysql之安全清理mysql-slow.log
- 【MYSQL中级篇】数据库数据查询学习
- Mysql基于Mycat实现读写分离
- 【MySQL数据库原理】数据库批量导入美团NLP分类数据集Meituan-Dianping/asap
- 【Mysql异构实时同步Oracle】OGG12异构同步mysql到oracle(windows mysql实时同步数据到linux oracle)详细文档