mysql--->mysql查看數據庫操做記錄

mysql查看數據庫操做記錄

MySQL的查詢日誌記錄了全部MySQL數據庫請求的信息。不管這些請求是否獲得了正確的執行。默認文件名爲hostname.log。默認狀況下MySQL查詢日誌是關閉的。生產環境,若是開啓MySQL查詢日誌,對性能仍是有蠻大的影響的。另外不少時候,MySQL慢查詢日誌基本能夠定位那些出現性能問題的SQL,因此MySQL查詢日誌應用的場景其實很少,有點雞肋的感受,它跟SQL Server中的profiler有點相似,可是這個不能跟蹤某個會話、用戶、客戶端。它只能對整個數據庫進行跟蹤。
window 環境下
  • 找到my.ini,在下面添加: log=存放日誌的路徑/my.log
  • 保存文件,重啓mysql服務,以後就能夠在對應目錄的下找到my.log文件。
linux環境下
  • MySQL中的參數general_log用來控制開啓、關閉MySQL查詢日誌,參數general_log_file用來控制查詢日誌的位置。因此若是你要判斷MySQL數據庫是否開啓了查詢日誌,可使用下面命令。general_log爲ON表示開啓查詢日誌,OFF表示關閉查詢日誌。
mysql> show variables like '%general_log%';
+------------------+------------------------------+
| Variable_name    | Value                        |
+------------------+------------------------------+
| general_log      | OFF                          |
| general_log_file | /var/lib/mysql/DB-Server.log |
+------------------+------------------------------+
2 rows in set (0.00 sec)
mysql>
  • 另外,MySQL的查詢日誌支持寫入文件或寫入數據表兩種形式,這個由參數log_output控制,以下所示:
mysql> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | FILE  |
+---------------+-------+
1 row in set (0.00 sec)
 
mysql>
  • 一、開啓MySQL查詢日誌
mysql> set global general_log = on;
Query OK, 0 rows affected (0.11 sec)
 
mysql> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log   | ON    |
+---------------+-------+
1 row in set (0.02 sec)
 
mysql>
  • 2:關閉MySQL查詢日誌
mysql> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log   | ON    |
+---------------+-------+
1 row in set (0.01 sec)
 
mysql> set global general_log=off;
Query OK, 0 rows affected (0.01 sec)
 
mysql> show variables like 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log   | OFF   |
+---------------+-------+
1 row in set (0.00 sec)
 
mysql>
  • 3:設置日誌輸出方式爲表(若是設置log_output=table的話,則日誌結果會記錄到名爲gengera_log的表中,這表的默認引擎是CSV):
mysql> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | FILE  |
+---------------+-------+
1 row in set (0.00 sec)
 
mysql> set global log_output='table';
Query OK, 0 rows affected (0.00 sec)
 
mysql> show variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | TABLE |
+---------------+-------+
1 row in set (0.01 sec)
 
mysql>
  • 4.查看查詢日誌信息。
mysql> select * from mysql.general_log;
+---------------------+---------------------------+-----------+-----------+--------------+----------------------------------+
| event_time          | user_host                 | thread_id | server_id | command_type | argument                         |
+---------------------+---------------------------+-----------+-----------+--------------+----------------------------------+
| 2017-07-06 12:32:05 | root[root] @ localhost [] |         1 |         1 | Query        | show variables like 'general%'   |
| 2017-07-06 12:32:28 | root[root] @ localhost [] |         1 |         1 | Query        | show variables like 'log_output' |
| 2017-07-06 12:32:41 | root[root] @ localhost [] |         1 |         1 | Query        | select * from MyDB.test          |
| 2017-07-06 12:34:36 | [root] @ localhost []     |         3 |         1 | Connect      | root@localhost on                |
| 2017-07-06 12:34:36 | root[root] @ localhost [] |         3 |         1 | Query        | KILL QUERY 1                     |
| 2017-07-06 12:34:36 | root[root] @ localhost [] |         3 |         1 | Quit         |                                  |
| 2017-07-06 12:34:51 | root[root] @ localhost [] |         1 |         1 | Query        | select * from mysql.general_log  |
+---------------------+---------------------------+-----------+-----------+--------------+----------------------------------+
7 rows in set (0.02 sec)
 
mysql>

參考:http://www.javashuo.com/article/p-ryhgxyyw-m.htmlhtml

相關文章
相關標籤/搜索