顯示具有 binlog 標籤的文章。 顯示所有文章
顯示具有 binlog 標籤的文章。 顯示所有文章

2019年5月10日 星期五

【MYSQL】測試一次binlog 刪除或異動資料復原

測試一次binlog 刪除或異動資料復原

#建立數據庫binlogtest
mysql> create database binlogtest;
Query OK, 1 row affected (0.02 sec)

#使用數據庫binlogtest
mysql> use binlogtest;
Database changed

#顯示目前binlog最新的位置,也是目前創建DB的位置
mysql> SHOW BINARY LOGS;
+------------------+------------+
| Log_name         | File_size  |
+------------------+------------+
| mysql-bin.000020 |  344636396 |
+------------------+------------+
6 rows in set (0.03 sec)

#新增test1表
mysql> CREATE TABLE test1(id INT(10) NOT NULL,name varchar(20),age INT(10));
Query OK, 0 rows affected (0.15 sec)

#插入幾筆數據
mysql> insert into test1 (id,name,age) values (1,'user1',18),(2,'user2',19),(3,'user3',20);
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

#檢查一下數據
mysql> select * from test1;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | user1 |   18 |
|  2 | user2 |   19 |
|  3 | user3 |   20 |
+----+-------+------+
3 rows in set (0.00 sec)

#準備測試----誤下錯指令
mysql> update test1 set age = 20;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

#數據已變動
mysql> select * from test1;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | user1 |   20 |
|  2 | user2 |   20 |
|  3 | user3 |   20 |
+----+-------+------+
3 rows in set (0.00 sec)

#若當下又有使用者插入資料
mysql> insert into test1 values(4,'user4',21),(5,'user5',22),(6,'user6',23);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0


mysql> select * from test1;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | user1 |   20 |
|  2 | user2 |   20 |
|  3 | user3 |   20 |
|  4 | user4 |   21 |
|  5 | user5 |   22 |
|  6 | user6 |   23 |
+----+-------+------+
6 rows in set (0.00 sec)

復原方法:
#再開一個視窗在LINUX下先把binlog文件導出,只有binlogtest這個DB
mysqlbinlog mysql-bin.000020 --database=binlogtest > binlogtest.sql

#接著編輯把錯誤的指令刪除update test1 set age = 20(用FIND搜尋字串)
vi binlogtest.sql

如下字串把下面字段全部刪除後存檔
# at 344637142
#190510 10:40:49 server id 1  end_log_pos 344637253 CRC32 0xad712e55    Query   thread_id=1590  exec_time=0     error_code=0
SET TIMESTAMP=1557456049/*!*/;
update test1 set age = 20
/*!*/;

#接著切換到剛MYSQL視窗把DB內把binlogtest的DB刪除
mysql> drop database binlogtest;
Query OK, 1 row affected (0.06 sec)

#然後在LINUX下把binlogtest.sql就剛匯出的binlog 導入
因為剛在使用binlog匯出時,只有針對binlogtest這個DB匯出,所以匯入時不用再指定DB
mysql -uroot -p  < binlogtest.sql

#接著進入查看,數據都回來了
mysql> use binlogtest
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------------+
| Tables_in_binlogtest |
+----------------------+
| test1                |
+----------------------+
1 row in set (0.00 sec)

mysql> select * from test1;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | user1 |   18 |
|  2 | user2 |   19 |
|  3 | user3 |   20 |
|  4 | user4 |   21 |
|  5 | user5 |   22 |
|  6 | user6 |   23 |
+----+-------+------+
6 rows in set (0.00 sec)


同樣的,如果特定某個語句,應該都可以用這種方式復原,例如讓DROP database ...等

2019年5月8日 星期三

【MYSQL】查看binlog內容的方法


一般如果直接cat 或tail 會出現亂碼
[root@rosalie-mysql01 mysql]# cat mysql-bin.000015 |more
þbinSf¹\
--More--
test_table24iem8rUUx0aVBOYz0iLCJtYWMiOiIyM2Y3ODQwNzEzZGYwMjU5OTkwMTlmOGJiYjBlNjdkZmE1NWQ3YzVjYmZjYjk2ODliMWJhYmZkMTI0ZTkzODE5In0=GN6TmpVek5qTTBNMkUxTkE9PSIsInZhbHVlIjoiUUJQZmJocVwvMTB5VEl6YkVIcm9HcWVGb1wvM25IQnJiRmxnUGdFd1BQUUw1RmNqczVOZkhhV1RSR0F0eGpldnROIiwibWFj
IjoiN2FiODdmYWU1ZWQ1NmIxY2VjOTUwYjA4NmNkYjYyNTY5NmYzYzg4ZDI2YTY5NmQzN2YzNjRkMTM3YzViNjNkZSJ9
TmpVek5qTTBNMkUxTkE9PSIsInZhbHVlIjoiWjA1cjB3YVE4eXpndGdyODJhTjgxUzJqR01tcUZ4T1J3TWxMRytRZ1hsQT0iLCJtYWMiOiI1NDNhNTY3ODEzZjVmYzNiMzRiNTZ

方式一 先導出成txt檔,然後即可直接查看
mysqlbinlog mysql-bin.000015 > /tmp/binlog-dump.txt

方式二:選取要查詢的某時段(如果不確定是哪一個檔案用*)
mysqlbinlog --start-datetime="2006-01-01 00:00:00" --stop-datetime="2019-05-08 00:00:00" /var/lib/mysql/mysql-bin.* > /tmp/mysql_restore.txt

方式三:直接查看不再導出
mysqlbinlog mysql-bin.000015 -vv |more

案例一:如果需要復原到某一個時間點(例如刪除了某筆資料,還原到刪除前 ),針對單一DB
查到刪除前位置是808,所以要復原 rookery這個DB,需先把BINLOG單一匯出,再將SQL導入到刪除前
 ,因為BINLOG是記錄所有DB,若不單一導出,這樣會連同其他的DB也一起導出 ,再匯入時就會造成資料重覆

mysqlbinlog –stop-position=808 /usr/local/mysql/data/mysql-bin.000015 --database=rookery > /tmp/4abc3.txt

案例二:取開啟到結束的時間 ,指定DB 
mysqlbinlog --start-datetime="2018-11-02 12:00:00" --stop-datetime="2018-11-02 12:10:00" --database=goods /usr/local/mysql/data/mysql-bin.000015 > /tmp/aaa.txt

案例三:查到DROP字段往後取200行
mysqlbinlog -vv mysql-bin.000015 | grep -A 200 "drop"

2019年3月11日 星期一

【MYSQL】刪除binlog的方法





binlog會因為數據庫的不斷運行而越來越大 ,當硬碟空間被占據越大時,可能會導致訪問速度變慢,若是完全被占滿時,就會導致服務卡死,就遇過服務狀態正常,但確無法登入,連重啟也無法!因為磁碟撐爆了~
binlog文件不是直接rm刪除就解決了 !也千萬不要用rm的方式刪除 ,因為刪除後,即使空間釋放了,但 mysql-bin.index 檔案中,仍有記錄(如下)

錯誤方法:查詢mysql data下的binlog檔案記錄有哪些
ll mysql-bin.*

【MYSQL】MYSQL的SYS表說明(版本8.0)

mysql> use sys Reading table information for completion of table and column names You can turn off this feature to get a quicker s...