欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mysql如何開啟各種日志

 更新時(shí)間:2022年11月05日 12:06:00   作者:吉松松  
這篇文章主要介紹了mysql如何開啟各種日志,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

以下日志開啟均在mysql5.7.32進(jìn)行測(cè)試

general_log

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ù)庫(kù),這樣日志信息就會(huì)被寫入到mysql.slow_log表中.
mysql數(shù)據(jù)庫(kù)支持同時(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

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,可以控制輸出語(yǔ)句輸出base64編碼的BINLOG語(yǔ)句;decode-rows:選項(xiàng)將把基于行的事件解碼成一個(gè)SQL語(yǔ)句
..............
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(mysql_audit.json)

開啟audit_log需要安裝審計(jì)插件,將audit-plugin-mysql-5.7-1.1.4-725-linux-x86_64.zip文件上傳到/opt下解壓,登錄數(shù)據(jù)庫(kù)查看插件存放位置

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ù)庫(kù)進(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"}

audit_log(server_audit.log)

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(cè)試數(shù)據(jù)庫(kù)版本為5.7.32,所以選擇mariadb-5.5.68

登錄數(shù)據(jù)庫(kù)查看插件存放位置

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ù)庫(kù)進(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)為L(zhǎng)OG_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è)置影響

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在VB.NET應(yīng)用中使用MySQL的方法

    在VB.NET應(yīng)用中使用MySQL的方法

    這篇文章主要介紹了在VB.NET應(yīng)用中使用MySQL的方法,操作基于Visual Studio IDE進(jìn)行,需要的朋友可以參考下
    2015-06-06
  • MySQL 查詢的排序、分頁(yè)相關(guān)

    MySQL 查詢的排序、分頁(yè)相關(guān)

    這篇文章主要介紹了MySQL 查詢的排序、分頁(yè)相關(guān)的相關(guān)知識(shí),幫助大家更好的理解和使用數(shù)據(jù)庫(kù),感興趣的朋友可以了解下
    2020-11-11
  • MySQL開啟Slow慢查詢的方法示例

    MySQL開啟Slow慢查詢的方法示例

    這篇文章主要給大家介紹了關(guān)于MySQL開啟Slow慢查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • MySQL視圖簡(jiǎn)介及基本操作教程

    MySQL視圖簡(jiǎn)介及基本操作教程

    這篇文章主要給大家介紹了關(guān)于MySQL視圖簡(jiǎn)介及基本操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 詳解MySql如何不插入重復(fù)數(shù)據(jù)

    詳解MySql如何不插入重復(fù)數(shù)據(jù)

    本文主要介紹了詳解MySql如何不插入重復(fù)數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • SQL優(yōu)化老出錯(cuò),那是你沒弄明白MySQL解釋計(jì)劃用法

    SQL優(yōu)化老出錯(cuò),那是你沒弄明白MySQL解釋計(jì)劃用法

    本篇文章講的是SQL優(yōu)化老出錯(cuò),那是你沒弄明白MySQL解釋計(jì)劃用法,有興趣的小伙伴速度來看看吧,希望本篇文章能夠幫助到你
    2021-11-11
  • Mysql 5.7.19 免安裝版遇到的坑(收藏)

    Mysql 5.7.19 免安裝版遇到的坑(收藏)

    這篇文章給大家分享了mysql 5.7.19免安裝版在安裝過程中遇到的一些問題,以前有mysql服務(wù)的話 需要去停掉mysql服務(wù)。具體內(nèi)容介紹大家參考下本文
    2017-08-08
  • 徹底搞懂?dāng)?shù)據(jù)庫(kù)操作truncate delete drop關(guān)鍵詞的區(qū)別

    徹底搞懂?dāng)?shù)據(jù)庫(kù)操作truncate delete drop關(guān)鍵詞的區(qū)別

    這篇文章主要為大家介紹了數(shù)據(jù)庫(kù)操作truncate delete drop關(guān)鍵詞的區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 深入mysql YEAR() MONTH() DAYOFMONTH()日期函數(shù)的詳解

    深入mysql YEAR() MONTH() DAYOFMONTH()日期函數(shù)的詳解

    本篇文章是對(duì)mysql中的YEAR() MONTH() DAYOFMONTH()日期函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • MySQL中的行級(jí)鎖定示例詳解

    MySQL中的行級(jí)鎖定示例詳解

    這篇文章主要給大家介紹了關(guān)于MySQL中行級(jí)鎖定的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05

最新評(píng)論