Mysql日誌詳解

 

錯誤日誌

錯誤日誌會記錄以下信息java

  • mysql執行過程當中的錯誤信息
  • mysql執行過程當中的告警信息
  • event scheduler運行時所產生信息
  • mysq啓動和中止過程當中產生的信息
  • 主從複製結構中,重從服務器IO複雜線程的啓動信息

錯誤日誌相關參數

MariaDB [(none)]> show variables where
    -> variable_name="log_error" or
    -> variable_name="log_warnings";
+---------------+------------------------------------------------------+
| Variable_name | Value                                                |
+---------------+------------------------------------------------------+
| log_error     | D:\application\MariaDB 10.4\data\DESKTOP-A3DGD26.err |
| log_warnings  | 2                                                    |
+---------------+------------------------------------------------------+
2 rows in set (0.001 sec)

log_error:錯誤日誌的存放路徑python

log_warnings:表示是否記錄告警信息到錯誤日誌,0表示不記錄告警信息,1表示記錄告警信息,大於1表示各種告警信息,例若有關網絡故障的信息和從新鏈接信息寫入錯誤日誌mysql

FILE:sql

InnoDB: using atomic writes.
2019-09-05 19:32:27 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2019-09-05 19:32:27 0 [Note] InnoDB: Uses event mutexes
2019-09-05 19:32:27 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-09-05 19:32:27 0 [Note] InnoDB: Number of pools: 1
2019-09-05 19:32:27 0 [Note] InnoDB: Using SSE2 crc32 instructions
2019-09-05 19:32:27 0 [Note] InnoDB: Initializing buffer pool, total size = 1G, instances = 1, chunk size = 128M
2019-09-05 19:32:27 0 [Note] InnoDB: Completed initialization of buffer pool
2019-09-05 19:32:27 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2019-09-05 19:32:27 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2019-09-05 19:32:27 0 [Note] InnoDB: Setting file '.\ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2019-09-05 19:32:27 0 [Note] InnoDB: File '.\ibtmp1' size is now 12 MB.
2019-09-05 19:32:27 0 [Note] InnoDB: Waiting for purge to start
2019-09-05 19:32:27 0 [Note] InnoDB: 10.4.7 started; log sequence number 139854; transaction id 21
2019-09-05 19:32:27 0 [Note] InnoDB: Loading buffer pool(s) from D:\application\MariaDB 10.4\data\ib_buffer_pool
2019-09-05 19:32:27 0 [Note] Plugin 'FEEDBACK' is disabled.
2019-09-05 19:32:27 0 [Note] Server socket created on IP: '::'.
2019-09-05 19:32:28 0 [Note] InnoDB: Buffer pool(s) load completed at 190905 19:32:28
2019-09-05 19:32:28 0 [Note] Reading of all Master_info entries succeeded
2019-09-05 19:32:28 0 [Note] Added new Master_info '' to hash table
2019-09-05 19:32:28 0 [Note] D:\application\MariaDB 10.4\bin\mysqld.exe: ready for connections.
Version: '10.4.7-MariaDB'  socket: ''  port: 3306  mariadb.org binary distribution
2019-09-05 19:35:47 8 [Warning] Access denied for user '13098884691'@'localhost' (using password: NO)

  

 查詢日誌

查詢天然在mysql中被稱之爲general log,查詢日誌記錄了數據庫執行的命令,無論這些語法是否正確,都會被記錄。因爲數據庫操做命令很是多並且比較頻繁,數據庫

因此開啓了查詢日誌之後,數據庫可能須要不停的寫入查詢,這樣會增大服務器的IO壓力,增長不少的系統開銷,因此默認狀況下,mysql的查詢日誌是沒有開啓緩存

的,可是開啓查詢日誌也有助於咱們分析那些語句執行密集,執行密集的select語句對應的數據是否可以被緩存,查詢日誌也能夠幫助咱們分析問題,因此,我安全

們能夠更具實際狀況決定是否開啓查詢日誌,若是須要能夠手動開啓。服務器

查詢日誌相關參數

MariaDB [(none)]> show variables where variable_name like "%general_log%" or variable_name = "log_output";
+------------------+---------------------+
| Variable_name    | Value               |
+------------------+---------------------+
| general_log      | OFF                 |
| general_log_file | DESKTOP-A3DGD26.log |
| log_output       | FILE                |
+------------------+---------------------+
3 rows in set (0.001 sec)  

general_log:表示查詢日誌是否開啓,ON表示開啓,OFF表示未開啓,默認未OFF網絡

log_output:表示當查詢日誌開啓之後,以哪一種方式存放。FILE表示存放於文件中,TABLE表示存放於表mysql.general_log中(慢查詢存放於表mysql.slow_log),FILE,TABLE表示同時存放於文件和表中app

NONE表示不記錄日誌。log_output不只控制查詢日誌,還控制慢查詢日誌。

general_log_file:表示查詢日誌存放於文件的路徑

FILE:

D:\application\MariaDB 10.4\bin\mysqld.exe, Version: 10.4.7-MariaDB (mariadb.org binary distribution). started with:
TCP Port: 3306, Named Pipe: MySQL
Time		    Id Command	Argument
190905 21:51:24	    13 Connect	root@localhost as anonymous on 
		    13 Query	select @@version_comment limit 1
190905 21:53:09	    13 Query	create database testdata
190905 21:53:18	    13 Query	SELECT DATABASE()
		    13 Init DB	testdata
190905 21:53:47	    13 Query	create table book(namechar(20),author char(20))
190905 21:54:11	    13 Query	create table book(name char(20),author char(20))
190905 21:55:06	    13 Query	insert into book values('python','aaa')
190905 21:55:18	    13 Query	insert into book values('java','bbb')
190905 21:55:37	    13 Query	select * from book

TABLE:

MariaDB [mysql]> select * from general_log;
+----------------------------+------------------------------+-----------+-----------+--------------+-----------------------------------------------------------------------------------------+
| event_time                 | user_host                    | thread_id | server_id | command_type | argument                                                                                |
+----------------------------+------------------------------+-----------+-----------+--------------+-----------------------------------------------------------------------------------------+
| 2019-09-05 21:51:24.424262 | [root] @ localhost [::1]     |        13 |         1 | Connect      | root@localhost as anonymous on                                                          |
| 2019-09-05 21:51:24.425389 | root[root] @ localhost [::1] |        13 |         1 | Query        | select @@version_comment limit 1                                                        |
| 2019-09-05 21:53:09.520124 | root[root] @ localhost [::1] |        13 |         1 | Query        | create database testdata                                                                |
| 2019-09-05 21:53:18.755448 | root[root] @ localhost [::1] |        13 |         1 | Query        | SELECT DATABASE()                                                                       |
| 2019-09-05 21:53:18.755778 | root[root] @ localhost [::1] |        13 |         1 | Init DB      | testdata                                                                                |
| 2019-09-05 21:53:47.279001 | root[root] @ localhost [::1] |        13 |         1 | Query        | create table book(namechar(20),author char(20))                                         |
| 2019-09-05 21:54:11.155792 | root[root] @ localhost [::1] |        13 |         1 | Query        | create table book(name char(20),author char(20))                                        |
| 2019-09-05 21:55:06.938210 | root[root] @ localhost [::1] |        13 |         1 | Query        | insert into book values('python','aaa')                                                 |
| 2019-09-05 21:55:18.342203 | root[root] @ localhost [::1] |        13 |         1 | Query        | insert into book values('java','bbb')                                                   |
| 2019-09-05 21:55:37.666281 | root[root] @ localhost [::1] |        13 |         1 | Query        | select * from book                                                                      |
+----------------------------+------------------------------+-----------+-----------+--------------+-----------------------------------------------------------------------------------------+
21 rows in set (0.000 sec)

 

慢查詢日誌

某些sql語句執行完畢所花費的時間特別長,咱們將這種響應比較慢的語句記錄在慢查詢日誌中,只要超過指定時間的sql語句,都稱爲「慢查詢」,被記錄在慢查詢日誌中。

慢查詢日誌默認狀況下是關閉的,默認設置下,超過10s的語句纔會被記錄到慢查詢日誌中。

慢查詢日誌相關參數

MariaDB [(none)]> show  variables where variable_name like 'slow_query%' or variable_name='long_query_time' or variable_name='log_queries_not_using_indexes';
+-------------------------------+--------------------------+
| Variable_name                 | Value                    |
+-------------------------------+--------------------------+
| log_queries_not_using_indexes | OFF                      |
| long_query_time               | 10.000000                |
| slow_query_log                | ON                       |
| slow_query_log_file           | DESKTOP-A3DGD26-slow.log |
+-------------------------------+--------------------------+
4 rows in set (0.001 sec)

slow_query_log:表示查詢日誌是否開啓,ON表示開啓,OFF表示未開啓,默認OFF

slow_query_log_file:表示查詢日誌存放於文件的路徑

long_query_time:表示多長時間的查詢被認爲"慢查詢",默認10s

log_queries_not_using_indexes:表示若是運行的sql語句沒有使用到索引,也被記錄到慢查詢日誌,OFF表示不記錄,ON表示記錄,默認OFF

FILE:

D:\application\MariaDB 10.4\bin\mysqld.exe, Version: 10.4.7-MariaDB (mariadb.org binary distribution). started with:
TCP Port: 3306, Named Pipe: MySQL
Time		    Id Command	Argument
# Time: 190906  3:26:04
# User@Host: root[root] @ localhost [::1]
# Thread_id: 18  Schema:   QC_hit: No
# Query_time: 11.001576  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0
# Rows_affected: 0  Bytes_sent: 64
SET timestamp=1567711564;
select sleep(11);
# Time: 190906  3:26:58
# User@Host: root[root] @ localhost [::1]
# Thread_id: 18  Schema:   QC_hit: No
# Query_time: 12.000767  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0
# Rows_affected: 0  Bytes_sent: 64
SET timestamp=1567711618;
select sleep(12);  

TABLE:

MariaDB [(none)]> select * from mysql.slow_log;
+----------------------------+------------------------------+-----------------+-----------------+-----------+---------------+----+----------------+-----------+-----------+------------------+-----------+
| start_time                 | user_host                    | query_time      | lock_time       | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text         | thread_id |
+----------------------------+------------------------------+-----------------+-----------------+-----------+---------------+----+----------------+-----------+-----------+------------------+-----------+
| 2019-09-06 03:26:04.673659 | root[root] @ localhost [::1] | 00:00:11.001576 | 00:00:00.000000 |         1 |             0 |    |              0 |         0 |         1 | select sleep(11) |        18 |
| 2019-09-06 03:26:58.246917 | root[root] @ localhost [::1] | 00:00:12.000767 | 00:00:00.000000 |         1 |             0 |    |              0 |         0 |         1 | select sleep(12) |        18 |
+----------------------------+------------------------------+-----------------+-----------------+-----------+---------------+----+----------------+-----------+-----------+------------------+-----------+
2 rows in set (0.000 sec)  

 

二進制日誌

二進制日誌是一個二進制文件,記錄了對MySQL數據庫執行更改的全部操做,而且記錄了語句發生時間、執行時長、

操做數據等其餘額外信息,可是他不記錄SELECT、SHOW等那些不改變數據庫的SQL語句。二進制日誌主要用於數據

庫恢復和主從複製,以及審計操做。

二進制日誌相關參數

MariaDB [(none)]> show variables where
variable_name="log_bin" or
variable_name="log_bin_basename" or
variable_name="max_binlog_size" or
variable_name="log_bin_index" or
variable_name="binlog_format" or
variable_name="sql_log_bin" or
variable_name="sync_binlog";
+------------------+------------+
| Variable_name    | Value      |
+------------------+------------+
| binlog_format    | MIXED      |
| log_bin          | OFF        |
| log_bin_basename |            |
| log_bin_index    |            |
| max_binlog_size  | 1073741824 |
| sql_log_bin      | ON         |
| sync_binlog      | 0          |
+------------------+------------+
7 rows in set (0.001 sec)

log_bin:表示二進制日誌是否開啓,ON表示開啓,OFF表示未開啓,默認OFF

log_bin_basename:二進制日誌文件前綴名,二進制日誌就記錄在該文件中

max_binlog_size:二進制日誌文件的最大大小,超過此大小,二進制文件會自動滾動

log_bin_index:二進制日誌文件索引文件名,用於記錄全部的二進制文件

binlog_format:決定了二進制日誌的記錄方式,STATEMENT以語句的形式記錄,ROW以數據修改的形式記錄,MIXED以語句和數據修改混合形式記錄

sql_log_bin:決定是否對二進制日誌進行日誌記錄,ON表示執行記錄,OFF表示不執行記錄,默認OFF,這個是會話級別變量能夠通SET sql_log_bin = {0|1}來改變該變量值

sync_binlog:決定二進制日誌寫入磁盤時機,若是sync_binlog爲0,操做系統來決定何時寫入磁盤,若是sync_binlog爲N(N=1,2,3..),則每N次事務提交後,都當即將內存中的二進制日誌寫入磁盤,如何選擇取決於安全性與性能的權衡

二進制日誌的三種記錄方式

STATEMENT:記錄對數據庫作出修改的語句,好比,update A set test='test',若是使用statement模式,那麼這條update語句將被記錄到二進制日誌中,使用statement模式時,優勢是binlog日誌量少,IO壓力小,性能高,缺點是爲了儘量一致的還原操做,除了記錄語句自己外,可能還須要記錄一些相關信息,並且,在使用一些特定函數時,並不能保證恢復操做與記錄徹底一致

ROW:記錄對數據庫作出的修改的語句所影響到的數據行以及這些行的修改,好比,update A set test='test',若是使用row模式,那麼這條update所影響到的行所對應的修改,將會記錄在binlog中,使用row模式時,優勢是能完還原和複製被日誌記錄時的操做,缺點是日誌量較大,IO壓力比較大,性能消耗比較大

MIXED:混合上述兩種模式,通常使用statement方式進行記錄,若是遇到一些特殊函數使用row方式進行記錄,這種記錄方式稱爲mixed

 二進制日誌進行數據庫恢復

修改my.cnf 

[mysqld]
log_bin=bin_log_file
binlog_format=row
sync_binlog=1
server_id=7

重啓mysql服務後,查看是否生效

+------------------+-----------------------------------------------------+
| Variable_name    | Value                                               |
+------------------+-----------------------------------------------------+
| binlog_format    | ROW                                                 |
| log_bin          | ON                                                  |
| log_bin_basename | D:\application\MariaDB 10.4\data\bin_log_file       |
| log_bin_index    | D:\application\MariaDB 10.4\data\bin_log_file.index |
| max_binlog_size  | 1073741824                                          |
| sql_log_bin      | ON                                                  |
| sync_binlog      | 1                                                   |
+------------------+-----------------------------------------------------+
7 rows in set (0.001 sec)

生成的二進制日誌文件和二進制日誌索引文件

├─data
│  ├─bin_log_file.index
│  ├─bin_log_file.000001

二進制日誌相關命令

MariaDB [(none)]> show binary logs;
+---------------------+-----------+
| Log_name            | File_size |
+---------------------+-----------+
| bin_log_file.000001 |       331 |
+---------------------+-----------+
1 row in set (0.000 sec)

MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| bin_log_file.000001 |      331 |              |                  |
+---------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

MariaDB [(none)]> show binlog events in "bin_log_file.000001";
+---------------------+-----+-------------------+-----------+-------------+-----------------------------------------------+
| Log_name            | Pos | Event_type        | Server_id | End_log_pos | Info                                          |
+---------------------+-----+-------------------+-----------+-------------+-----------------------------------------------+
| bin_log_file.000001 |   4 | Format_desc       |         7 |         256 | Server ver: 10.4.7-MariaDB-log, Binlog ver: 4 |
| bin_log_file.000001 | 256 | Gtid_list         |         7 |         285 | []                                            |
| bin_log_file.000001 | 285 | Binlog_checkpoint |         7 |         331 | bin_log_file.000001                           |
+---------------------+-----+-------------------+-----------+-------------+-----------------------------------------------+
3 rows in set (0.000 sec)

新增數據,在模擬數據被誤刪除

建立數據庫->建立表->表插入數據->表刪除數據->刪除表->刪除數據庫

create database zz;
use zz;
create table zz_table(id int);
insert into zz_table values(1);
insert into zz_table values(2);
insert into zz_table values(3);
insert into zz_table values(4);
insert into zz_table values(5);
delete from zz_table where id=4;
drop table zz_table;
drop database zz;

我如今須要根據二進制日誌恢復到刪除表以前的狀態

MariaDB [(none)]> show binlog events in "bin_log_file.000001";
+---------------------+------+-------------------+-----------+-------------+-----------------------------------------------------------+
| Log_name            | Pos  | Event_type        | Server_id | End_log_pos | Info                                                      |
+---------------------+------+-------------------+-----------+-------------+-----------------------------------------------------------+
| bin_log_file.000001 |    4 | Format_desc       |         7 |         256 | Server ver: 10.4.7-MariaDB-log, Binlog ver: 4             |
| bin_log_file.000001 |  256 | Gtid_list         |         7 |         285 | []                                                        |
| bin_log_file.000001 |  285 | Binlog_checkpoint |         7 |         331 | bin_log_file.000001                                       |
| bin_log_file.000001 |  331 | Gtid              |         7 |         373 | GTID 0-7-1                                                |
| bin_log_file.000001 |  373 | Query             |         7 |         456 | create database zz                                        |
| bin_log_file.000001 |  456 | Gtid              |         7 |         498 | GTID 0-7-2                                                |
| bin_log_file.000001 |  498 | Query             |         7 |         592 | use `zz`; create table zz_table(id int)                   |
| bin_log_file.000001 |  592 | Gtid              |         7 |         634 | BEGIN GTID 0-7-3                                          |
| bin_log_file.000001 |  634 | Annotate_rows     |         7 |         687 | insert into zz_table values(1)                            |
| bin_log_file.000001 |  687 | Table_map         |         7 |         736 | table_id: 18 (zz.zz_table)                                |
| bin_log_file.000001 |  736 | Write_rows_v1     |         7 |         774 | table_id: 18 flags: STMT_END_F                            |
| bin_log_file.000001 |  774 | Xid               |         7 |         805 | COMMIT /* xid=13 */                                       |
| bin_log_file.000001 |  805 | Gtid              |         7 |         847 | BEGIN GTID 0-7-4                                          |
| bin_log_file.000001 |  847 | Annotate_rows     |         7 |         900 | insert into zz_table values(2)                            |
| bin_log_file.000001 |  900 | Table_map         |         7 |         949 | table_id: 18 (zz.zz_table)                                |
| bin_log_file.000001 |  949 | Write_rows_v1     |         7 |         987 | table_id: 18 flags: STMT_END_F                            |
| bin_log_file.000001 |  987 | Xid               |         7 |        1018 | COMMIT /* xid=15 */                                       |
| bin_log_file.000001 | 1018 | Gtid              |         7 |        1060 | BEGIN GTID 0-7-5                                          |
| bin_log_file.000001 | 1060 | Annotate_rows     |         7 |        1113 | insert into zz_table values(3)                            |
| bin_log_file.000001 | 1113 | Table_map         |         7 |        1162 | table_id: 18 (zz.zz_table)                                |
| bin_log_file.000001 | 1162 | Write_rows_v1     |         7 |        1200 | table_id: 18 flags: STMT_END_F                            |
| bin_log_file.000001 | 1200 | Xid               |         7 |        1231 | COMMIT /* xid=17 */                                       |
| bin_log_file.000001 | 1231 | Gtid              |         7 |        1273 | BEGIN GTID 0-7-6                                          |
| bin_log_file.000001 | 1273 | Annotate_rows     |         7 |        1326 | insert into zz_table values(4)                            |
| bin_log_file.000001 | 1326 | Table_map         |         7 |        1375 | table_id: 18 (zz.zz_table)                                |
| bin_log_file.000001 | 1375 | Write_rows_v1     |         7 |        1413 | table_id: 18 flags: STMT_END_F                            |
| bin_log_file.000001 | 1413 | Xid               |         7 |        1444 | COMMIT /* xid=19 */                                       |
| bin_log_file.000001 | 1444 | Gtid              |         7 |        1486 | BEGIN GTID 0-7-7                                          |
| bin_log_file.000001 | 1486 | Annotate_rows     |         7 |        1539 | insert into zz_table values(5)                            |
| bin_log_file.000001 | 1539 | Table_map         |         7 |        1588 | table_id: 18 (zz.zz_table)                                |
| bin_log_file.000001 | 1588 | Write_rows_v1     |         7 |        1626 | table_id: 18 flags: STMT_END_F                            |
| bin_log_file.000001 | 1626 | Xid               |         7 |        1657 | COMMIT /* xid=21 */                                       |
| bin_log_file.000001 | 1657 | Gtid              |         7 |        1699 | BEGIN GTID 0-7-8                                          |
| bin_log_file.000001 | 1699 | Annotate_rows     |         7 |        1753 | delete from zz_table where id=4                           |
| bin_log_file.000001 | 1753 | Table_map         |         7 |        1802 | table_id: 18 (zz.zz_table)                                |
| bin_log_file.000001 | 1802 | Delete_rows_v1    |         7 |        1840 | table_id: 18 flags: STMT_END_F                            |
| bin_log_file.000001 | 1840 | Xid               |         7 |        1871 | COMMIT /* xid=23 */                                       |
| bin_log_file.000001 | 1871 | Gtid              |         7 |        1913 | GTID 0-7-9                                                |
| bin_log_file.000001 | 1913 | Query             |         7 |        2025 | use `zz`; DROP TABLE `zz_table` /* generated by server */ |
| bin_log_file.000001 | 2025 | Gtid              |         7 |        2067 | GTID 0-7-10                                               |
| bin_log_file.000001 | 2067 | Query             |         7 |        2148 | drop database zz                                          |
+---------------------+------+-------------------+-----------+-------------+-----------------------------------------------------------+
41 rows in set (0.000 sec)

在文件系統中也能夠經過以下命令查看

D:\application\MariaDB 10.4\data>mysqlbinlog bin_log_file.000001 

根據上述日誌能夠發現,pos=373,end_log_pod'fdfs=1871即爲要恢復的類容,截取二進制日誌,放入自定義文件

mysqlbinlog --start-position=373 --stop-position=1871 bin_log_file.000001 > recover_file.sql

執行數據恢復

#臨時關閉binlog
set sql_log_bin=0;
#執行sql文件
source recover_file.sql
#開啓binlog
set sql_log_bin=1;

查看恢復結果

MariaDB [zz]> show tables;
+--------------+
| Tables_in_zz |
+--------------+
| zz_table     |
+--------------+
1 row in set (0.000 sec)

MariaDB [zz]> select * from zz_table;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    5 |
+------+
4 rows in set (0.000 sec)
相關文章
相關標籤/搜索