簡(jiǎn)介
該篇文章對(duì)mysql中的日志進(jìn)行總結(jié)與簡(jiǎn)單介紹,不會(huì)涉及的太深。主要的目的是為了對(duì)mysql中的日志文件有一個(gè)體系化的了解。
日志分類(lèi)
mysql中的日志文件,配置文件、錯(cuò)誤日志文件、二進(jìn)制文件(binary log)、慢查詢(xún)?nèi)罩?slow-query-log)、全量日志(genera log)、審計(jì)日志(audit log)、數(shù)據(jù)庫(kù)文件&數(shù)據(jù)表文件、存儲(chǔ)引擎文件、中繼日志(relay log)、進(jìn)程文件(pid)和socket文件。
參數(shù)文件
參數(shù)文件就是mysql中的配置文件,在linux下的my.cnf文件、windows下的my.ini文件。文件內(nèi)容主要分為server和client兩個(gè)模塊。server模塊配置的是有關(guān)mysql的服務(wù)信息,例如慢查詢(xún)?nèi)罩尽lient模塊配置的是有關(guān)mysql客戶(hù)端連接信息,例如客戶(hù)端連接的端口號(hào)。
文件格式大致如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[client] port = 3306 default - character - set = utf8mb4 [mysqld] user = mysql port = 3306 sql_mode = "" default -storage-engine = innodb default -authentication-plugin = mysql_native_password character - set -server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect = 'set names utf8mb4' slow_query_log long_query_time = 3 slow-query-log-file = /var/lib/mysql/mysql.slow.log log-error = /var/lib/mysql/mysql.error.log default - time -zone = '+8:00' |
錯(cuò)誤日志文件
錯(cuò)誤日志文件記錄了mysql從啟動(dòng)、運(yùn)行和關(guān)閉幾個(gè)環(huán)節(jié)中的日志信息。例如連接mysql連接失敗、查詢(xún)命令錯(cuò)誤、sql執(zhí)行流程等等。對(duì)于定位mysql錯(cuò)誤有著很大的幫助。
文件大致內(nèi)容如下:
1
2
3
4
5
6
7
8
9
10
|
version: '5.7.28-log' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mysql community server (gpl) 2021-04-17t21:23:00.865868z 3 [note] aborted connection 3 to db: 'exam_wechat' user : 'root' host: '172.18.0.1' (got timeout reading communication packets) 2021-04-17t21:23:00.865969z 2 [note] aborted connection 2 to db: 'exam_wechat' user : 'root' host: '172.18.0.1' (got timeout reading communication packets) 2021-04-19t22:33:24.137143z 0 [note] innodb: page_cleaner: 1000ms intended loop took 18415ms. the settings might not be optimal. (flushed=0 and evicted=0, during the time .) 2021-04-20t07:03:21.765208z 79 [note] access denied for user 'root' @ '172.18.0.1' (using password : no ) 2021-04-20t07:03:23.825044z 81 [note] aborted connection 81 to db: 'unconnected' user : 'root' host: '172.18.0.1' (got an error reading communication packets) 2021-04-20t07:14:25.033983z 82 [note] access denied for user 'root' @ '172.18.0.1' (using password : no ) 2021-04-20t07:14:27.442608z 84 [note] aborted connection 84 to db: 'unconnected' user : 'root' host: '172.18.0.1' (got an error reading communication packets) 2021-04-20t07:27:13.971644z 83 [note] aborted connection 83 to db: 'unconnected' user : 'root' host: '172.18.0.1' (got timeout reading communication packets) 2021-04-20t07:41:02.916249z 85 [note] aborted connection 85 to db: 'unconnected' user : 'root' host: '172.18.0.1' (got timeout reading communication packets) |
如何開(kāi)始錯(cuò)誤日志。只要在mysql中的配置文件中配置意向log_error即可。
1
2
3
4
5
6
7
8
9
10
|
+ ---------------------+--------------------------------+ | variable_name | value | + ---------------------+--------------------------------+ | binlog_error_action | abort_server | | log_error | /var/lib/mysql/mysql.error.log | | log_error_verbosity | 3 | + ---------------------+--------------------------------+ 3 rows in set time : 0.010s |
全量日志文件
全量日志文件記錄的是mysql所有的sql操作日志記錄。例如增刪改查等操作都會(huì)被記錄下來(lái)。
1
2
3
4
5
6
7
8
|
reconnecting... + ------------------+---------------------------------+ | variable_name | value | + ------------------+---------------------------------+ | general_log | off | | general_log_file | /var/lib/mysql/7fdc5f723ff9.log | + ------------------+---------------------------------+ |
配置項(xiàng)有三種值,table,none和file。配置file則會(huì)記錄在日志文件中,配置none則不會(huì)記錄,配置table則會(huì)在mysql默認(rèn)的mysql數(shù)據(jù)中創(chuàng)建一張表(表名叫做general-log)來(lái)記錄日志。
不推薦開(kāi)啟,記錄的日志文件太多,不僅僅有性能消耗同時(shí)也占用太多無(wú)效空間。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 日志記錄文件格式 mysqld, version: 5.7.28-log (mysql community server (gpl)). started with : tcp port: 3306 unix socket: /var/run/mysqld/mysqld.sock time id command argument 2021-04-20t09:16:48.574591z 88 connect access denied for user 'root' @ '172.18.0.1' (using password : no ) 2021-04-20t09:16:50.329894z 89 query select connection_id() 2021-04-20t09:16:50.335222z 89 query select @@version 2021-04-20t09:16:50.339621z 89 query select @@version_comment 2021-04-20t09:16:50.343525z 90 query select connection_id() 2021-04-20t09:16:50.347115z 90 query show databases 2021-04-20t09:16:50.380236z 90 query select table_name, column_name from information_schema.columns where table_schema = 'none' order by table_name,ordinal_position 2021-04-20t09:16:50.391019z 90 query select concat( "'" , user , "'@'" ,host, "'" ) from mysql. user 2021-04-20t09:16:50.415062z 90 query select routine_name from information_schema.routines where routine_type= "function" and routine_schema = "none" 2021-04-20t09:16:50.432015z 90 query select name from mysql.help_topic where name like "show %" 2021-04-20t09:16:52.572608z 89 query show variables like '%general%' 2021-04-20t09:17:13.532046z 89 query show variables like '%general%' |
慢查詢(xún)?nèi)罩?/h2>
慢查詢(xún)?nèi)罩臼嵌ㄎ籹ql語(yǔ)句查詢(xún)快與慢而記錄的一種日志文件。當(dāng)某一條sql語(yǔ)句查詢(xún)時(shí)間超過(guò)一個(gè)固定的閾值,這條sql語(yǔ)句將被定義為慢查詢(xún)的sql語(yǔ)句,被記錄在慢查詢(xún)?nèi)罩疚募小?/p>
慢查詢(xún)的配置主要有如下三個(gè)參數(shù)。
是否開(kāi)啟慢查詢(xún)與慢查詢(xún)?nèi)罩疚募?/p>
1
2
3
4
5
6
7
8
9
|
+ ---------------------------+-------------------------------+ | variable_name | value | + ---------------------------+-------------------------------+ | slow_query_log | on | | slow_query_log_file | /var/lib/mysql/mysql.slow.log | + ---------------------------+-------------------------------+ 5 rows in set time : 0.014s |
慢查詢(xún)時(shí)間閾值。
1
2
3
4
5
6
7
8
|
+ -----------------+----------+ | variable_name | value | + -----------------+----------+ | long_query_time | 3.000000 | + -----------------+----------+ 1 row in set time : 0.013 |
二進(jìn)制日志文件
二進(jìn)制日志(binary log)文件用于記錄mysql的dml語(yǔ)句,記錄了操作之后的物理日志內(nèi)容,不會(huì)記錄mysql中的select、show等語(yǔ)句。二進(jìn)制日志文件主要的作用如下:
用戶(hù)主從復(fù)制,主服務(wù)器將二進(jìn)制文件中的物理日志發(fā)送給從服務(wù)器,從服務(wù)器在將日志寫(xiě)入到自身。
用于數(shù)據(jù)恢復(fù)。根據(jù)物理日志,找回?cái)?shù)據(jù)丟失之前的操作日志。
可以通過(guò)如下幾個(gè)參數(shù)進(jìn)行配置:
1
2
3
4
5
6
7
8
9
10
11
|
reconnecting... + ---------------------------------+--------------------------------+ | variable_name | value | + ---------------------------------+--------------------------------+ | log_bin | on | | log_bin_basename | /var/lib/mysql/mysql-bin | | log_bin_index | /var/lib/mysql/mysql-bin. index | + ---------------------------------+--------------------------------+ 6 rows in set time : 0.015s |
log_bin是否開(kāi)啟二進(jìn)制日志文件,log_bin_basename存儲(chǔ)的目錄以及日志文件前綴,log_bin_index存儲(chǔ)日志文件索引(日志文件名稱(chēng))。如果日志文件沒(méi)有指定文件名稱(chēng),則默認(rèn)使用本機(jī)名稱(chēng)。
日志文件列表。
1
2
3
4
|
-rw-r ----- 1 mysql root 154 apr 12 09:31 mysql-bin.000041 -rw-r ----- 1 mysql root 154 apr 12 19:45 mysql-bin.000042 -rw-r ----- 1 mysql root 1459325 apr 17 20:26 mysql-bin.000043 -rw-r ----- 1 mysql mysql 24576 apr 17 22:18 mysql-bin.000044 |
1
2
3
4
5
6
7
|
# cat mysql-bin. index ./mysql-bin.000001 ./mysql-bin.000002 ./mysql-bin.000003 ./mysql-bin.000004 ./mysql-bin.000005 ./mysql-bin.000006 |
審計(jì)日志
審計(jì)日志用來(lái)記錄mysql的網(wǎng)絡(luò)活動(dòng),對(duì)mysql的操作記錄做統(tǒng)計(jì)、分析與報(bào)告等。屬于對(duì)mysql安全監(jiān)控記錄類(lèi)的日志文件。
mysql自身不包含該功能的,并且該功能在mysql官網(wǎng)也是收費(fèi)的。這里也不做具體的演示。
中繼日志
中繼日志是mysql主從復(fù)制,在從服務(wù)器上的一個(gè)重要角色。當(dāng)主服務(wù)器將二進(jìn)制文件發(fā)送給從服務(wù)器時(shí),從服務(wù)器不會(huì)立馬執(zhí)行,而是放在一個(gè)指定的一類(lèi)日志文件中,從服務(wù)器在開(kāi)啟一個(gè)sql線程去讀取中繼日志文件內(nèi)容并寫(xiě)入到自身數(shù)據(jù)中。
pid文件
pid是一個(gè)mysql實(shí)例的進(jìn)程文件號(hào)。mysql屬于單進(jìn)程服務(wù),在啟動(dòng)一個(gè)mysql實(shí)例,就會(huì)創(chuàng)建一個(gè)pid文件。
socket文件
socket也是mysql通信的一種方式。mysql通信有兩種方式,tcp和socket方式。tcp是走網(wǎng)絡(luò)通信,可以將服務(wù)部署到任意可以訪問(wèn)的服務(wù)器上。socket是走的文件通信方式,必須在同一臺(tái)服務(wù)器上。
1
2
|
# tcp模式 mysql -hxxxx -pxxxx -uxxxx -pxxx |
1
|
mysql -uxxxx -pxxxx -s /path/socket |
數(shù)據(jù)庫(kù)與表
數(shù)據(jù)庫(kù)與表值的就是mysql中的表結(jié)構(gòu)文件、數(shù)據(jù)文件和索引文件。
innodb存儲(chǔ)引擎的數(shù)據(jù)表結(jié)構(gòu)
1
2
|
-rw-r ----- 1 mysql root 13650 apr 13 09:46 wechat_user.frm -rw-r ----- 1 mysql mysql 98304 apr 17 13:43 wechat_user.ibd |
myisam存儲(chǔ)引擎的數(shù)據(jù)表結(jié)構(gòu)
1
2
3
|
-rw-r ----- 1 mysql mysql 0 apr 20 17:53 users.myd -rw-r ----- 1 mysql mysql 1024 apr 20 17:53 users.myi -rw-r ----- 1 root root 8586 apr 20 17:53 users.frm |
存儲(chǔ)引擎文件
不同的存儲(chǔ)引擎,實(shí)現(xiàn)起來(lái)也不同。innodb存儲(chǔ)引擎分為redolog和undolog兩種日志文件。
到此這篇關(guān)于全面盤(pán)點(diǎn)mysql中的那些重要日志文件的文章就介紹到這了,更多相關(guān)mysql日志文件內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.51cto.com/u_10992108/4680893