婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Centos7實現(xiàn)MySQL基于日志還原數(shù)據(jù)的示例代碼

Centos7實現(xiàn)MySQL基于日志還原數(shù)據(jù)的示例代碼

熱門標簽:鶴壁電銷外呼系統(tǒng)怎么安裝 400電話辦理哪家好廠商 地圖標注需要現(xiàn)場嗎 企業(yè)400電話辦理哪正規(guī) 網(wǎng)站上插入地圖標注內(nèi)容 繽客網(wǎng)注冊時地圖標注出不來 工廠位置地圖標注 地圖標注企業(yè)名稱侵權(quán)案件 重慶營銷外呼系統(tǒng)排名

簡介

Binlog日志,即二進制日志文件,用于記錄用戶對數(shù)據(jù)庫操作的SQL語句信息,當發(fā)生數(shù)據(jù)誤刪除的時候我們可以通過binlog日志來還原已經(jīng)刪除的數(shù)據(jù),還原數(shù)據(jù)的方法分為傳統(tǒng)二進制文件還原數(shù)據(jù)和基于GTID的二進制文件還原數(shù)據(jù)

前期準備

準備一臺Centos7虛擬機,關閉防火墻和selinux,配置IP地址,同步系統(tǒng)時間,安裝MySQL數(shù)據(jù)庫

傳統(tǒng)二進制日志還原數(shù)據(jù)

修改配置文件

[root@localhost ~]# vi /etc/my.cnf
server-id=1
log-bin=binlog

#重啟數(shù)據(jù)庫服務
[root@localhost ~]# systemctl restart mysqld

操作數(shù)據(jù)庫

mysql> create database mydb charset utf8mb4;
mysql> use mydb;
mysql> create table test(id int)engine=innodb charset=utf8mb4;
mysql> insert into test values(1);
mysql> insert into test values(2);
mysql> insert into test values(3);
mysql> insert into test values(4);
mysql> commit;
mysql> update test set id=10 where id=4;
mysql> commit;
mysql> select * from test;
+------+
| id  |
+------+
|  1 |
|  2 |
|  3 |
|  10 |
+------+
4 rows in set (0.00 sec)
mysql> drop database mydb;

查看二進制日志信息

mysql> show master status\G;
*************************** 1. row ***************************
       File: binlog.000001
     Position: 1960
   Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

 
#查找創(chuàng)庫和刪庫的點,為219和1868
mysql> show binlog events in 'binlog.000001';
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name   | Pos | Event_type   | Server_id | End_log_pos | Info                                |
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+
| binlog.000001 | 219 | Query     |     1 |     329 | create database mydb charset utf8mb4                |
| binlog.000001 | 1868 | Query     |     1 |    1960 | drop database mydb                         |
+---------------+------+----------------+-----------+-------------+--------------------------------------------------------------------+

另存為二進制日志信息

[root@localhost ~]# mysqlbinlog --start-position=219 --stop-position=1868 /var/lib/mysql/binlog.000001 > /tmp/binlog.sql

恢復數(shù)據(jù)

#臨時關閉二進制日志記錄以免重復記錄
mysql> set sql_log_bin=0;
#恢復數(shù)據(jù)
mysql> source /tmp/binlog.sql
#重啟二進制日志記錄
mysql> set sql_log_bin=1;

查看數(shù)據(jù)恢復情況

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| mydb        |
| mysql       |
| performance_schema |
| sys        |
+--------------------+
5 rows in set (0.00 sec)

mysql> use mydb;
Database changed
mysql> select * from test;
+------+
| id  |
+------+
|  1 |
|  2 |
|  3 |
|  10 |
+------+
4 rows in set (0.00 sec)、

基于GTID二進制日志還原數(shù)據(jù)

修改配置文件

[root@localhost ~]# vi /etc/my.cnf
server-id=1
log-bin=binlog
gtid_mode=ON
enforce_gtid_consistency=true
log_slave_updates=1

#重啟數(shù)據(jù)庫服務
[root@localhost ~]# systemctl restart mysqld

操作數(shù)據(jù)庫

mysql> create database mydb1;
mysql> use mydb1;
Database changed
mysql> create table t1(id int)engine=innodb charset=utf8mb4;
mysql> insert into t1 values(1);
mysql> insert into t1 values(2);
mysql> insert into t1 values(3);
mysql> insert into t1 values(11);
mysql> insert into t1 values(12);
mysql> commit;
mysql> select * from t1;
+------+
| id  |
+------+
|  1 |
|  2 |
|  3 |
|  11 |
|  12 |
+------+
5 rows in set (0.00 sec)
mysql> drop database mydb1;

查看二進制日志信息

mysql> show master status\G;
*************************** 1. row ***************************
       File: binlog.000003
     Position: 1944
   Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 51d3db57-bf69-11ea-976c-000c2911a022:1-8
1 row in set (0.00 sec)

mysql> show binlog events in 'binlog.000003';
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
| Log_name   | Pos | Event_type   | Server_id | End_log_pos | Info                               |
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
| binlog.000003 | 154 | Gtid      |     1 |     219 | SET @@SESSION.GTID_NEXT= '51d3db57-bf69-11ea-976c-000c2911a022:1' |
| binlog.000003 | 219 | Query     |     1 |     316 | create database mydb1                       |
| binlog.000003 | 1784 | Gtid      |     1 |    1849 | SET @@SESSION.GTID_NEXT= '51d3db57-bf69-11ea-976c-000c2911a022:8' |
| binlog.000003 | 1849 | Query     |     1 |    1944 | drop database mydb1                        |
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+

另存為二進制日志信息

#8號事務記錄為刪除數(shù)據(jù)庫,因此只需恢復1-7號事務記錄即可
[root@localhost ~]# mysqlbinlog --skip-gtids --include-gtids='51d3db57-bf69-11ea-976c-000c2911a022:1-7' /var/lib/mysql/binlog.000003 > /tmp/gtid.sql

參數(shù)說明:
--include-gtids:包含事務
--exclude-gtids:排除事務
--skip-gtids:跳過事務

恢復數(shù)據(jù)

mysql> set sql_log_bin=0;
mysql> source /tmp/gtid.sql
mysql> set sql_log_bin=1;

查看數(shù)據(jù)恢復情況

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| mydb        |
| mydb1       |
| mysql       |
| performance_schema |
| sys        |
+--------------------+
6 rows in set (0.00 sec)

mysql> use mydb1;
Database changed
mysql> select * from t1;
+------+
| id  |
+------+
|  1 |
|  2 |
|  3 |
|  11 |
|  12 |
+------+
5 rows in set (0.00 sec)

 到此這篇關于Centos7實現(xiàn)MySQL基于日志還原數(shù)據(jù)的示例代碼的文章就介紹到這了,更多相關Centos7 MySQL日志還原數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

標簽:棗莊 克拉瑪依 渭南 日照 鹽城 東莞 常州 96

巨人網(wǎng)絡通訊聲明:本文標題《Centos7實現(xiàn)MySQL基于日志還原數(shù)據(jù)的示例代碼》,本文關鍵詞  Centos7,實現(xiàn),MySQL,基于,日志,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。
  • 相關文章
  • 下面列出與本文章《Centos7實現(xiàn)MySQL基于日志還原數(shù)據(jù)的示例代碼》相關的同類信息!
  • 本頁收集關于Centos7實現(xiàn)MySQL基于日志還原數(shù)據(jù)的示例代碼的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 彝良县| 安图县| 瓮安县| 鲜城| 齐河县| 许昌市| 新疆| 辽宁省| 增城市| 荔波县| 鄯善县| 吉林省| 寿宁县| 嘉荫县| 广安市| 大同县| 松桃| 永泰县| 贡嘎县| 中阳县| 衡阳县| 新巴尔虎右旗| 万荣县| 托里县| 隆林| 大同市| 南皮县| 龙岩市| 扶沟县| 南陵县| 郑州市| 垦利县| 石台县| 太白县| 台湾省| 台北县| 锡林郭勒盟| 江源县| 郎溪县| 岱山县| 临朐县|