您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“mysql怎么開啟各種日志”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
general_log支持熱開啟,熱關(guān)閉。開啟general_log會(huì)記錄所有操作mysql命令,所以會(huì)產(chǎn)生大量文件,一般不開啟。
相關(guān)參數(shù)general_log、log_output、general_log_file
mysql> show variables like 'general_log'; --查看日志是否開啟 +---------------+-------+ | Variable_name | Value | +---------------+-------+ | general_log | OFF | +---------------+-------+ 1 row in set (1.09 sec) mysql> show variables like 'general_log_file'; --general_log_file日志保存位置 +------------------+--------------------------------------+ | Variable_name | Value | +------------------+--------------------------------------+ | general_log_file | /opt/sudytech/mysql/data/general.log | +------------------+--------------------------------------+ 1 row in set (2.41 sec) mysql> show variables like 'log_output'; --日志輸出類型 table和file兩種類型 +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | FILE | +---------------+-------+ 1 row in set (0.00 sec) log_output='FILE' 表示將日志存入文件,默認(rèn)值是FILE log_output='TABLE'表示將日志存入數(shù)據(jù)庫,這樣日志信息就會(huì)被寫入到mysql.slow_log表中. mysql數(shù)據(jù)庫支持同時(shí)兩種日志存儲(chǔ)方式,配置的時(shí)候以逗號(hào)隔開即可,如:log_output='FILE,TABLE'. 日志記錄到系統(tǒng)專用日志表中,要比記錄到文件耗費(fèi)更多的系統(tǒng)資源,因此對(duì)于需要啟用慢查日志,又需要比夠獲得更高的系統(tǒng)性能,那么建議優(yōu)先記錄到文件。
開啟general_log日志 mysql> set global general_log=on; --開啟日志 Query OK, 0 rows affected (2.60 sec) mysql> show variables like 'general_log'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | general_log | ON | +---------------+-------+ 1 row in set (0.00 sec) mysql> set global general_log_file='/opt/sudytech/mysql/data/general.log'; --指定日志產(chǎn)生位置 Query OK, 0 rows affected (0.05 sec) mysql> show variables like 'general_log_file'; +------------------+--------------------------------------+ | Variable_name | Value | +------------------+--------------------------------------+ | general_log_file | /opt/sudytech/mysql/data/general.log | +------------------+--------------------------------------+ 1 row in set (0.04 sec) 由于log_output默認(rèn)值為FILE。所以不需要修改。 查看/opt/sudytech/mysql/data/目錄下已經(jīng)產(chǎn)生了general.log日志 [root@localhost data]# pwd /opt/sudytech/mysql/data [root@localhost data]# tail -f general.log 2021-05-18T06:45:32.140829Z 2 Query set global general_log=OFF /opt/sudytech/mysql/bin/mysqld, Version: 5.7.32-log (MySQL Community Server (GPL)). started with: Tcp port: 3306 Unix socket: /tmp/mysql.sock Time Id Command Argument 2021-05-18T10:43:17.049473Z 3 Query show variables like 'general_log' 2021-05-18T10:44:09.060990Z 3 Query set global general_log_file='/opt/sudytech/mysql/data/general.log' /opt/sudytech/mysql/bin/mysqld, Version: 5.7.32-log (MySQL Community Server (GPL)). started with: Tcp port: 3306 Unix socket: /tmp/mysql.sock Time Id Command Argument 2021-05-18T10:44:18.375549Z 3 Query show variables like 'general_log_file' ...... 永久修改需要在my.cnf中[mysqld]添加 general_log = 1 general_log_file=/opt/sudytech/mysql/data/general.log
log_bin不支持熱開啟。 mysql> set global log_bin=on; ERROR 1238 (HY000): Variable 'log_bin' is a read only variable 需要在my.cnf [mysqld]中添加 log_bin=/opt/sudytech/mysql/data/mysql-bin expire_logs_days = 180 #日志過期天數(shù) max_binlog_size = 500M #單日文件最大大小 開啟后會(huì)在/opt/sudytech/mysql/data目錄下產(chǎn)生mysql-bin.xxxxx和mysql-bin.index兩個(gè)文件。mysql-bin.xxxxxx是記錄binlog日志的文件,而index是存放mysql-bin文件名的文件 [root@localhost data]# ll mysql-bin.* -rw-r-----. 1 mysql mysql 372 5月 18 18:58 mysql-bin.000001 -rw-r-----. 1 mysql mysql 154 5月 18 18:58 mysql-bin.000002 -rw-r-----. 1 mysql mysql 84 5月 18 18:58 mysql-bin.index [root@localhost data]# cat mysql-bin.index /opt/sudytech/mysql/data/mysql-bin.000001 /opt/sudytech/mysql/data/mysql-bin.000002
遇到以下3種情況時(shí),MySQL會(huì)重新生成一個(gè)新的日志文件,文件序號(hào)遞增
1、MySQL服務(wù)器停止或重啟時(shí)(其實(shí)重啟時(shí)也是調(diào)用flush logs命令)
2、使用 flush logs 命令;
3、當(dāng) binlog 文件大小超過 max_binlog_size 變量的值時(shí);
max_binlog_size 的最小值是4096字節(jié),最大值和默認(rèn)值是 1GB (1073741824字節(jié))。事務(wù)被寫入到binlog的一個(gè)塊中,所以它不會(huì)在幾個(gè)二進(jìn)制日志之間被拆分。因此,如果你有很大的事務(wù),為了保證事務(wù)的完整性,不可能做切換日志的動(dòng)作,只能將該事務(wù)的日志都記錄到當(dāng)前日志文件中,直到事務(wù)結(jié)束,你可能會(huì)看到binlog文件大于 max_binlog_size 的情況。
查看mysql-bin.xxxxx信息,mysql-bin.xxxxx是以二進(jìn)制形式存儲(chǔ),vim、cat查看是亂碼,這時(shí)可以使用mysqlbinlog命令查看
[root@localhost data]# /opt/sudytech/mysql/bin/mysqlbinlog -v --base64-output=decode-rows --start-datetime='2021-04-11 00:00:00' --stop-datetime='2021-05-19 15:00:00' /opt/sudytech/mysql/data/mysql-bin.000002 base64-output,可以控制輸出語句輸出base64編碼的BINLOG語句;decode-rows:選項(xiàng)將把基于行的事件解碼成一個(gè)SQL語句 .............. create database aaaaa /*!*/; # at 316 #210518 19:15:01 server id 1 end_log_pos 381 CRC32 0x6f4cdc6c Anonymous_GTID last_committed=1 sequence_number=2 ....... create database bbbb .....
開啟audit_log需要安裝審計(jì)插件,將audit-plugin-mysql-5.7-1.1.4-725-linux-x86_64.zip文件上傳到/opt下解壓,登錄數(shù)據(jù)庫查看插件存放位置
mysql> show global variables like 'plugin_dir'; +---------------+----------------------------------+ | Variable_name | Value | +---------------+----------------------------------+ | plugin_dir | /opt/sudytech/mysql//lib/plugin/ | +---------------+----------------------------------+ 1 row in set (0.02 sec)
將插件復(fù)制該路徑下,并授權(quán)
[root@localhost mysql]# cp /opt/sudytech/audit-plugin-mysql-5.7-1.1.4-725/lib/libaudit_plugin.so /opt/sudytech/mysql//lib/plugin/ [root@localhost mysql]# chmod +x /opt/sudytech/mysql//lib/plugin/libaudit_plugin.so [root@localhost mysql]# chown mysql:mysql /opt/sudytech/mysql//lib/plugin/libaudit_plugin.so
登錄數(shù)據(jù)庫進(jìn)行安裝
mysql> install plugin audit soname 'libaudit_plugin.so'; ERROR 1123 (HY000): Can't initialize function 'audit'; Plugin initialization function failed.
解決方法:
[root@localhost mysql]# /opt/sudytech/audit-plugin-mysql-5.7-1.1.4-725/utils/offset-extract.sh /opt/sudytech/mysql/bin/mysqld ERROR: gdb not found. Make sure gdb is installed and on the path. [root@localhost mysql]# yum -y instal gdb [root@localhost mysql]# /opt/sudytech/audit-plugin-mysql-5.7-1.1.4-725/utils/offset-extract.sh /opt/sudytech/mysql/bin/mysqld //offsets for: /opt/sudytech/mysql/bin/mysqld (5.7.32) {"5.7.32","30165bbd00a2077d2e4b1d3c6768c2f7", 7824, 7872, 3632, 4792, 456, 360, 0, 32, 64, 160, 536, 7988, 4360, 3648, 3656, 3660, 6072, 2072, 8, 7056, 7096, 7080},
編輯my.cnf在[mysql]中添加,重啟mysql
audit_json_file=on #保證mysql重啟后自動(dòng)啟動(dòng)插件 audit_record_cmds='insert,delete,update,create,drop,alter,grant,truncate,show' #記錄操作 plugin-load=AUDIT=libaudit_plugin.so #防止刪除了插件,重啟后又會(huì)加載 audit_json_log_file=/opt/sudytech/mysql/stat/logs/mysql_audit.json #日志路徑 audit_offsets=7824, 7872, 3632, 4792, 456, 360, 0, 32, 64, 160, 536, 7988, 4360, 3648, 3656, 3660, 6072, 2072, 8, 7056, 7096, 7080
查看/opt/sudytech/mysql/stat/logs/目錄下會(huì)產(chǎn)生mysql_audit.json日志
[root@localhost logs]# cat mysql_audit.json .......... {"msg-type":"activity","date":"1621349743813","thread-id":"2","query-id":"6","user":"root","priv_user":"root","ip":"","host":"localhost","connect_attrs":{"_os":"linux-glibc2.12","_client_name":"libmysql","_pid":"8826","_client_version":"5.7.32","_platform":"x86_64","program_name":"mysql"},"pid":"8826","os_user":"root","appname":"/opt/sudytech/mysql/bin/mysql","cmd":"create_db","query":"create database bbbbb"} {"msg-type":"activity","date":"1621349802594","thread-id":"2","query-id":"8","user":"root","priv_user":"root","ip":"","host":"localhost","connect_attrs":{"_os":"linux-glibc2.12","_client_name":"libmysql","_pid":"8826","_client_version":"5.7.32","_platform":"x86_64","program_name":"mysql"},"pid":"8826","os_user":"root","appname":"/opt/sudytech/mysql/bin/mysql","cmd":"drop_db","query":"drop database bbbbb"}
server_audit.log支持熱開啟,熱關(guān)閉。下載mariadb-5.5.68壓縮包,解壓獲取mariadb-5.5.68-linux-x86_64/lib/plugin/server_audit.so(mysql8后不支持該插件)
MariaDB_5.x.x和MariaDB_10.x.x區(qū)別:
MariaDB_5.x.x:兼容MySQL5.x.x的,接口幾乎一致,只限于社區(qū)版
MariaDB_10.x.x:10.x.x使用新技術(shù),接口會(huì)與MySQL逐漸區(qū)別開來,向MariaDB新接口過渡
因?yàn)闇y試數(shù)據(jù)庫版本為5.7.32,所以選擇mariadb-5.5.68
登錄數(shù)據(jù)庫查看插件存放位置
mysql> show global variables like 'plugin_dir'; +---------------+----------------------------------+ | Variable_name | Value | +---------------+----------------------------------+ | plugin_dir | /opt/sudytech/mysql//lib/plugin/ | +---------------+----------------------------------+ 1 row in set (0.02 sec)
將插件復(fù)制該路徑下,并授權(quán)
[root@localhost plugin]# cp /opt/sudytech/mariadb-5.5.68-linux-x86_64/lib/plugin/server_audit.so /opt/sudytech/mysql/lib/plugin/ [root@localhost plugin]# chmod +x /opt/sudytech/mysql/lib/plugin/server_audit.so
登錄數(shù)據(jù)庫進(jìn)行安裝
mysql> install plugin server_audit soname 'server_audit.so'; Query OK, 0 rows affected (0.00 sec) mysql> show plugins; +----------------------------+----------+--------------------+-----------------+---------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+-----------------+---------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL | ....... | SERVER_AUDIT | ACTIVE | AUDIT | server_audit.so | GPL | +----------------------------+----------+--------------------+-----------------+---------+
開啟server_audit.log,日志默認(rèn)會(huì)在mysql/data目錄下,可通過server_audit_file_path指定文件存放位置
mysql> show variables like '%server_audit_logging%'; +----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | server_audit_logging | OFF | +----------------------+-------+ 1 row in set (0.00 sec) mysql> set global server_audit_logging=on; Query OK, 0 rows affected (0.00 sec)
在my.cnf中[mysqld]添加配置
server_audit_logging = ON #開啟日志記錄,默認(rèn)是關(guān)閉 server_audit = FORCE_PLUS_PERMANENT #防止插件被卸載 server_audit_file_path = /opt/sudytech/mysql/stat/logs/server_audit.log #定義審計(jì)日志路徑與文件名 server_audit_file_rotations = 2 #定義審計(jì)日志的輪詢個(gè)數(shù),0為不輪詢,值為2會(huì)產(chǎn)生3個(gè)文件server_audit.log server_audit.log.1 server_audit.log.2 server_audit_file_rotate_size = 1073741824 #定義切割審計(jì)日志的文件大小1073741824=1GB,當(dāng)server_audit_file_rotations為0時(shí),設(shè)置該值無意義
在/opt/sudytech/mysql/stat/logs目錄下就會(huì)產(chǎn)生server_audit.log日志
[root@localhost logs]# tail -f server_audit.log 20210519 10:05:00,localhost.localdomain,root,localhost,2,27,QUERY,,'show variables like \'server_audit_file_rotations\'',0 20210519 10:05:01,localhost.localdomain,root,localhost,2,28,QUERY,,'show variables like \'server_audit_file_rotations\'',0 20210519 10:05:01,localhost.localdomain,root,localhost,2,29,QUERY,,'show variables like \'server_audit_file_rotations\'',0 20210519 10:05:01,localhost.localdomain,root,localhost,2,30,QUERY,,'show variables like \'server_audit_file_rotations\'',0 20210519 10:05:02,localhost.localdomain,root,localhost,2,31,QUERY,,'show variables like \'server_audit_file_rotations\'',0 20210519 10:35:02,localhost.localdomain,root,localhost,2,0,DISCONNECT,,,0
server_audit.log參數(shù)說明:
server_audit_output_type
指定日志輸出類型,可為SYSLOG或FILE,為SYSLOG時(shí),記錄在/var/log/message中
server_audit_logging
啟動(dòng)或關(guān)閉審計(jì)
server_audit_events
指定記錄事件的類型,可以用逗號(hào)分隔的多個(gè)值(connect,query,table),如果開啟了查詢緩存(query cache),查詢直接從查詢緩存返回?cái)?shù)據(jù),將沒有table記錄
server_audit_file_path
如server_audit_output_type為FILE,使用該變量設(shè)置存儲(chǔ)日志的文件,可以指定目錄,默認(rèn)存放在mysql/data目錄的server_audit.log文件中
server_audit_file_rotations
指定日志文件的數(shù)量,如果為0日志將從不輪轉(zhuǎn)
server_audit_file_rotate_size
限制日志文件的大小,當(dāng)server_audit_file_rotations為0時(shí),該值無意義
server_audit_file_rotate_now
是否立即切割日志,當(dāng)server_audit_file_rotations為0時(shí),該值無意義
server_audit_incl_users
指定哪些用戶的活動(dòng)將記錄,connect將不受此變量影響,該變量比server_audit_excl_users優(yōu)先級(jí)高
server_audit_syslog_facility
默認(rèn)為LOG_USER,指定facility
server_audit_syslog_ident
設(shè)置ident,作為每個(gè)syslog記錄的一部分
server_audit_syslog_info
指定的info字符串將添加到syslog記錄
server_audit_syslog_priority
定義記錄日志的syslogd priority
server_audit_excl_users
該列表的用戶行為將不記錄,connect將不受該設(shè)置影響
“mysql怎么開啟各種日志”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。