標(biāo)題索引
創(chuàng)新互聯(lián)建站是一家專業(yè)提供旬陽(yáng)企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、做網(wǎng)站、H5頁(yè)面制作、小程序制作等業(yè)務(wù)。10年已為旬陽(yáng)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
日志分類
日志操作
日志分解
日志分類
在數(shù)據(jù)庫(kù)系統(tǒng)中日志主要分為6類,具體如下:
1、查詢?nèi)罩荆褐饕涗浫粘2樵兊挠涗洠?br/>
2、慢查詢?nèi)罩荆褐饕涗洸樵儠r(shí)長(zhǎng)超過(guò)設(shè)置的數(shù)值時(shí)間的語(yǔ)句,方便DBA運(yùn)維人員進(jìn)行查詢;
3、錯(cuò)誤日志:主要用于記錄錯(cuò)誤的事件,方便DBA運(yùn)維人員對(duì)數(shù)據(jù)庫(kù)的操作和日常維護(hù);
4、二進(jìn)制日志:主要用于記錄sql語(yǔ)句對(duì)數(shù)據(jù)庫(kù)的操作,方便DBA運(yùn)維人員審計(jì)和備份恢復(fù);
5、中繼日志:主要用于數(shù)據(jù)庫(kù)集群架構(gòu)設(shè)計(jì)中數(shù)據(jù)庫(kù)的主備;
6、事務(wù)日志:主要用于防止數(shù)據(jù)庫(kù)服務(wù)器異常中斷時(shí),事物的恢復(fù)。
日志操作
1.查詢?nèi)罩荆樵內(nèi)罩究梢杂涗浽谖募到y(tǒng)的文件中,也可以記錄在自身數(shù)據(jù)庫(kù)的表中,具體的記錄方式可通過(guò)全部變量進(jìn)行查看或更改,默認(rèn)只記錄在文件之中,建議關(guān)閉查詢?nèi)罩?br/>
MariaDB [(none)]> show global variables like 'general_log%'; +------------------+----------------+ | Variable_name | Value | +------------------+----------------+ | general_log | ON | | general_log_file | nginxser02.log | #相對(duì)目錄,相對(duì)數(shù)據(jù)文件安裝的目錄 +------------------+----------------+ 2 rows in set (0.00 sec) MariaDB [(none)]> show global variables like 'log_output'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | FILE | #值可以為file|table|none +---------------+-------+ 1 row in set (0.00 sec)
2.慢查詢?nèi)罩?,?zhí)行時(shí)間超出指定查詢的日志,慢查詢有可能數(shù)據(jù)庫(kù)設(shè)計(jì)的不合理或表被鎖定非設(shè)計(jì)原因,具體驗(yàn)證或修改可根據(jù)如下示例
#查詢慢查詢?cè)O(shè)定時(shí)長(zhǎng),想修改時(shí)長(zhǎng)并永久生效,寫配置文件 MariaDB [(none)]> show global variables like 'long_query_time'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+ 1 row in set (0.00 sec) MariaDB [(none)]> show global variables like 'slow_query%'; +---------------------+---------------------+ | Variable_name | Value | +---------------------+---------------------+ | slow_query_log | OFF | | slow_query_log_file | nginxser02-slow.log | +---------------------+---------------------+ 2 rows in set (0.00 sec) #慢查詢?nèi)罩居涗浀牟呗栽O(shè)定 MariaDB [(none)]> show global variables like 'log_slow%'; +---------------------+--------------------------------------------------------------------------------------------------------------+ | Variable_name | Value | +---------------------+--------------------------------------------------------------------------------------------------------------+ | log_slow_filter | admin,filesort,filesort_on_disk,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk | | log_slow_queries | OFF | | log_slow_rate_limit | 1 | | log_slow_verbosity | | +---------------------+--------------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec)
3.錯(cuò)誤日志,錯(cuò)誤日志記錄所有數(shù)據(jù)庫(kù)生命周期中所有的錯(cuò)誤信息,具體如下操作
MariaDB [(none)]> show global variables like 'log_error%'; +---------------+------------------------------+ | Variable_name | Value | +---------------+------------------------------+ | log_error | /var/log/mariadb/mariadb.log | +---------------+------------------------------+ 1 row in set (0.00 sec) #設(shè)置錯(cuò)誤警告信息,1表示記錄,0表示不記錄 MariaDB [(none)]> show global variables like 'log_war%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_warnings | 1 | +---------------+-------+ 1 row in set (0.00 sec)
4.二進(jìn)制日志,文件也為二進(jìn)制文件,可利用數(shù)據(jù)庫(kù)的恢復(fù)等,默認(rèn)情況下是關(guān)閉的,且只能在配置文件中進(jìn)行修改,其中變量sql_log_bin是否記錄二進(jìn)制日志的開(kāi)關(guān),而log-bin是記錄二進(jìn)制文件的路徑,可以使用絕對(duì)路徑,也可以使用基于數(shù)據(jù)庫(kù)安裝路徑的相對(duì)路徑,max_binlog_size是默認(rèn)單個(gè)二進(jìn)制文件最大大小,默認(rèn)為1G,超出此大小則進(jìn)行日志滾動(dòng),當(dāng)然重啟服務(wù)時(shí)也會(huì)進(jìn)行滾動(dòng),sync_bin_log默認(rèn)情況值為0,不會(huì)將內(nèi)存中的日志實(shí)時(shí)同步進(jìn)磁盤,若服務(wù)器異常斷電則日志丟失,數(shù)據(jù)丟失,是否開(kāi)啟此功能根據(jù)架構(gòu)設(shè)定自行設(shè)置,具體如下
#默認(rèn)情況下是開(kāi)啟了二進(jìn)制文件記錄,但是未指定二進(jìn)制文件的記錄位置 MariaDB [(none)]> show global variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | OFF | +---------------+-------+ 1 row in set (0.00 sec) MariaDB [(none)]> show global variables like 'sql_log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | sql_log_bin | ON | +---------------+-------+ 1 row in set (0.00 sec) MariaDB [(none)]> show global variables like 'sync_binlog'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | sync_binlog | 0 | +---------------+-------+ 1 row in set (0.00 sec)
開(kāi)啟二進(jìn)制文件所指定的路徑路徑(相對(duì)數(shù)據(jù)庫(kù)安裝的路徑),并設(shè)定記錄在文件MySQL-bin文件中,具體配置如下
[ root@centos7 ~ ]#vim /etc/my.cnf [mysqld]z log-bin=mysql-bin #記錄日志時(shí)的格式,格式有基于statement、raw或者二者的混雜格式 binlog_format=mixed
再次登陸數(shù)據(jù)庫(kù)進(jìn)行驗(yàn)證
MariaDB [(none)]> show global variables like 'log_bin'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_bin | ON | +---------------+-------+ 1 row in set (0.00 sec) MariaDB [(none)]> help show ; Name: 'SHOW' Description: SHOW has many forms that provide information about databases, tables, columns, or status information about the server. This section describes those following: SHOW AUTHORS SHOW {BINARY | MASTER} LOGS SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count] MariaDB [(none)]> show binary logs; #查看正在使用中的二進(jìn)制日志 +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 264 | | mysql-bin.000002 | 245 | +------------------+-----------+ 2 rows in set (0.00 sec) MariaDB [(none)]> show binlog events in 'mysql-bin.000002'; +------------------+-----+-------------+-----------+-------------+-------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +------------------+-----+-------------+-----------+-------------+-------------------------------------------+ | mysql-bin.000002 | 4 | Format_desc | 1 | 245 | Server ver: 5.5.52-MariaDB, Binlog ver: 4 | +------------------+-----+-------------+-----------+-------------+-------------------------------------------+ 1 row in set (0.00 sec) #查看二進(jìn)制文件的索引文件,通過(guò)索引文件查看有多少個(gè)滾動(dòng)日志文件 [ root@nginxser02 ~ ]#cat /var/lib/mysql/mysql-bin.index ./mysql-bin.000001 ./mysql-bin.000002 #通過(guò)專用命令查看二進(jìn)制文件 [ root@nginxser02 ~ ]#mysqlbinlog /var/lib/mysql/mysql-bin.000002 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #171130 4:13:21 server id 1 end_log_pos 245 Start: binlog v 4, server v 5.5.52-MariaDB created 171130 4:13:21 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG '
5.中繼日志,從犯服務(wù)器用于保存從主服務(wù)器的二進(jìn)制日志中讀取到的事件
6.事務(wù)日志工作原理主要是在事物開(kāi)始時(shí),在內(nèi)存中劃分一個(gè)buffer用于緩存事務(wù)日志,另外防止事務(wù)日志在內(nèi)存中磁盤文件中的事物分組文件滿時(shí)同步失敗,因此在磁盤上創(chuàng)建一組文件,一組文件默認(rèn)為2個(gè)文件,方便buffer向磁盤文件同步日志,磁盤文件中的事物日志再次向數(shù)據(jù)庫(kù)系統(tǒng)同步,防止事物異常丟失。
圖1-1 事物日志結(jié)構(gòu)圖
事務(wù)日志默認(rèn)配置buffer為8M,每文件大小為5M,每組中有2個(gè)文件,事務(wù)日志存儲(chǔ)路徑為相對(duì)路徑的根目錄,具體如下:
MariaDB [(none)]> show global variables like 'innodb_log%'; +---------------------------+---------+ | Variable_name | Value | +---------------------------+---------+ | innodb_log_block_size | 512 | | innodb_log_buffer_size | 8388608 | | innodb_log_file_size | 5242880 | | innodb_log_files_in_group | 2 | | innodb_log_group_home_dir | ./ | +---------------------------+---------+ 5 rows in set (0.00 sec)
查看事物在磁盤中同步的日志文件
[ root@nginxser02 mysql ]#ls -lh /var/lib/mysql/ib_logfile* -rw-rw---- 1 mysql mysql 5.0M Nov 30 05:06 /var/lib/mysql/ib_logfile0 -rw-rw---- 1 mysql mysql 5.0M Nov 8 22:56 /var/lib/mysql/ib_logfile1
另外事物日志有redo log 和undo log,redo即為事物完成且保存在事物日志之中,但并未同步至數(shù)據(jù)系統(tǒng)之中,因此要同步進(jìn)數(shù)據(jù)庫(kù)系統(tǒng)必須將事物日志進(jìn)行redo,undo即為并未完成的事物且保存在事物日志之中,此時(shí)需要事物回滾,將之前的事物undo。