MySql 的操做日誌 歷史記錄

如何查看mysql數據庫操做記錄日誌


一、首先確認你日誌是否啓用了mysql>show variables like 'log_bin'。html

二、若是啓用了,即ON,那日誌文件就在mysql的安裝目錄的data目錄下。mysql

三、怎樣知道當前的日誌mysql> show master status。linux

四、看二進制日誌文件用mysqlbinlog,shell>mysqlbinlog mail-bin.000001或者shell>mysqlbinlog mail-bin.000001 | tail,Windows 下用相似的。sql

MySQL的日誌操做:shell

一、首先,登錄mysql後,執行sql語句:show variables like 'log_bin'。數據庫

二、#錯誤日誌log-errol開啓方式:在my.ini的[mysqld]選項下:添加代碼:log-error=E:\log-error.txt。安全

記錄內容:主要是記錄啓動、運行或中止mysqld時出現的致命性問題,都是系統級的錯誤記錄。bash

三、#查詢日誌:log,開啓方式:在my.ini的[mysqld]選項下:添加代碼:log=E:/mysql_log.txt。服務器

四、#二進制日誌:log-bin,開啓方式:在my.ini的[mysqld]選項下:添加代碼:log-bin=E:/mysql_log_bin,記錄內容:主要是記錄全部的更改數據的語句,可以使用mysqlbinlog命令恢復數據。session


mysql查看歷史執行的sql

轉載  2015年08月31日 18:23:11
  • 15983

查看mysql的語句,比較經常使用的大概是show processlist 命令了,可是這個對於查詢時間比較長的語句比較有意義,對於一會兒就能執行的語句真心拼不過手速啊.

因而就只能從mysql的歷史記錄裏面着手了.爲了實現這個目的,咱們須要修改/etc/my.cnf文件.

在my.cnf中的[mysqld](其餘地方可能無效)下插入log=/etc/tmp/mysql.log(日誌的地址能夠本身定義,不過不要在home裏面,不然可能顯示不出來,主要要有寫的權限).而後要重啓數據庫,用service mysqld restart命令就能夠了.這個log會將全部的執行語句記錄下來,因此在數據庫很忙的時候,這個日誌可能變得很大,不宜查看.

用tail -f /etc/tmp/mysql.log 命令能夠持續觀察執行語句,也能夠將該文件下載下來慢慢看.

=========================================

另外還有一個慢查詢歷史記錄,也在my.cnf文件裏,配置語句爲

?1234 log-slow-queries=/var/lib/mysql/slowquery.log long_query_time=5 log-queries-not-using-indexes

其中log-slow-queries是慢查詢歷史記錄的地址,long_query_time限定超過X秒才能加入到歷史記錄裏面,log-queries-not-using-indexes限定沒有使用索引的查詢語句.

這個日誌因爲記錄語句很少,因此能夠長期開啓.


MySQL的日誌操做

在MySQL的初期,須要熟練掌握sql語句的運用,最簡單的方式就是多練習多運用,可是好須要有必定的目的性,什麼意思呢?

就是每一個人的習慣不同,可能容易犯的錯誤也不一樣,因此須要總結本身的常犯錯誤,有針對性的彌補,這樣就會對MySQL的學習和掌握起到事半功倍的效果。今天就說幾種記錄MySQL操做的幾種日誌:

      #錯誤日誌     log-error

      #查詢日誌     log

     #二進制日誌  log-bin

     #慢日誌  log-slow-queries  

     #更新日誌 log-update(官方建議,不開啓,因此在此忽略它)

下面一一詳述:

工具/原料

  • MySQL
  • 編輯器

方法/步驟

  1. 1

    首先,登錄mysql後,執行sql語句:

    show variables like 'log_bin';

    查看是否日誌開啓,詳細結果如圖:

    MySQL的日誌操做
    MySQL的日誌操做
  2. 2

    #錯誤日誌     log-error

    開啓方式:  

    在my.ini的[mysqld]選項下:

      添加代碼:

       log-error=E:\log-error.txt

    記錄內容:

       主要是記錄啓動、運行或中止mysqld時出現的致命性問題,都是系統級的錯誤記錄如圖:

    MySQL的日誌操做
    MySQL的日誌操做
  3. 3

    #查詢日誌     log

    開啓方式:

    在my.ini的[mysqld]選項下:

      添加代碼:

        log=E:/mysql_log.txt

    記錄內容:

       主要是記錄數據庫創建的客戶端鏈接和執行的語句如圖:

    MySQL的日誌操做
    MySQL的日誌操做
  4. 4

     #二進制日誌  log-bin

    開啓方式:

    在my.ini的[mysqld]選項下:

      添加代碼:

        log-bin=E:/mysql_log_bin

    記錄內容:

       主要是記錄全部的更改數據的語句,可以使用mysqlbinlog命令恢復數據。如圖:

    MySQL的日誌操做
  5. 5

    #慢日誌  log-slow-queries 

    開啓方式:

    在my.ini的[mysqld]選項下:

      添加代碼:    

        long_query_time =1 (設定慢查詢的標準,單位是s/秒)

        log-slow-queries= E:/mysql_log_slow.txt

    記錄內容:

       主要是記錄全部執行時間超過long_query_time的查詢或沒有使用索引的查詢如圖:

    MySQL的日誌操做
    END

注意事項

  • txt文檔用編輯器打開,記事本打開格式有點亂



MySql 的操做日誌


任何一種數據庫中,都有各類各樣的日誌。Mysql也不例外,在Mysql中有4種不一樣的日誌、分別錯誤日誌、二進制日誌、查詢日誌和慢查詢日誌。這些日誌記錄着Mysql數據庫不一樣方面的蹤影。下文將介紹這4種不一樣的日誌做用和用途。

  一.錯誤日誌

  錯誤日誌在Mysql數據庫中很重要,它記錄着mysqld啓動和中止,以及服務器在運行過程當中發生的任何錯誤的相關信息。

  1.配置信息

  --log-error=[file-name]用來指定錯誤日誌存放的位置。

  若是沒有指定[file-name],默認hostname.err作爲文件名,默認存放在DATADIR目錄中。

  也能夠將log-error配置到my.cnf文件中,這樣就省去了每次在啓動mysqld時都手工指定--log-error.例如:

  [mysql@test2]$ vi /etc/my.cnf

  # The MySQL server

  [mysqld]

  ....

  log-error = /var/lib/mysql/test2_mysqld.err

  .....

  2.錯誤信息樣板

  080313 05:21:55 mysqld started

  080313 5:21:55 InnoDB: Started; log sequence number 0 43655

  080313 5:21:55 [Note] /usr/local/mysql/bin/mysqld: ready for connections.

  Version: '5.0.26-standard-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Edition - Standard (GPL)

  080313 5:24:13 [Note] /usr/local/mysql/bin/mysqld: Normal shutdown

  080313 5:24:13 InnoDB: Starting shutdown...

  080313 5:24:16 InnoDB: Shutdown completed; log sequence number 0 43655

  080313 5:24:16 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete

  080313 05:24:16 mysqld ended

  080313 05:24:47 mysqld started

  080313 5:24:47 InnoDB: Started; log sequence number 0 43655

  080313 5:24:47 [Note] /usr/local/mysql/bin/mysqld: ready for connections.

  Version: '5.0.26-standard-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Edition - Standard (GPL)

  080313 5:33:49 [Note] /usr/local/mysql/bin/mysqld: Normal shutdown


三.查詢日誌

  查詢日誌記錄了clinet的全部的語句。

  Note:因爲log日誌記錄了數據庫全部操做,對於訪問頻繁的系統,此種日誌會形成性能影響,建議關閉。

  1.配置信息

  --log=[file-name]用來指定錯誤日誌存放的位置。

  若是沒有指定[file-name],默認爲主機名(hostname)作爲文件名,默認存放在DATADIR目錄中。

  也能夠將log配置到my.cnf文件中,這樣就省去了每次在啓動mysqld時都手工指定--log.例如:

  # The MySQL server

  [mysqld]

  ......

  #query-log

  log = /var/lib/mysql/query_log.log

  ......

  2.讀取查詢日誌

  查詢日誌是純文本格可,可使用OS文本讀取工具直接打開查看。例如:

  [mysql@test2]$ tail -n 15 query_log.log

  080313 7:58:28 17 Query show tables

  080313 8:07:45 17 Quit

  080313 10:01:48 18 Connect root@localhost on

  080313 10:02:38 18 Query SELECT DATABASE()

  18 Init DB test

  080313 10:02:42 18 Query show tables

  080313 10:03:07 18 Query select * from pet

  080313 10:06:26 18 Query insert into pet values('hunter','yxyup','cat','f','1996-04-29',null)

  080313 10:06:39 18 Query select * from pet

  080313 10:07:13 18 Query update pet set sex='m' where name='hunter'

  080313 10:07:38 18 Query delete from pet where name='hunter'

  080313 10:13:48 18 Query desc test8

  080313 10:14:13 18 Query create table t1(id int,name char(10))

  080313 10:14:41 18 Query alter table t1 add sex char(2)


  [mysql@test2]$


四.慢查詢日誌

  慢查詢日誌是記錄了執行時間超過參數long_query_time(單位是秒)所設定值的SQL語句日誌。

  Note:慢查詢日誌對於咱們發現性能有問題的SQL有很幫助,建議使用並常常分析

  1.配置信息

  --log-slow-queries=[file-name]用來指定錯誤日誌存放的位置。

  若是沒有指定[file-name],默認爲hostname-slow.log作爲文件名,默認存放在DATADIR目錄中。

  也能夠將log-slow-queries配置到my.cnf文件中,這樣就省去了每次在啓動mysqld時都手工指定--log-slow-queries.例如:

  # The MySQL server

  [mysqld]

  ......

  #slow-query-log

  log-slow-queries = /var/lib/mysql/slow_query_log.log

  ......

  2.讀取慢查詢日誌

  [mysql@test2]$ cat slow_query_log.log

  /usr/local/mysql/bin/mysqld, Version: 5.0.26-standard-log. started with:

  Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock

  Time Id Command Argument

  # Time: 080313 5:41:46

  # User@Host: root[root] @ localhost []

  # Query_time: 108 Lock_time: 0 Rows_sent: 0 Rows_examined: 8738

  use test;

  select count(1) from t1 a, t1 b,t1 c where a.id=b.id and b.name=c.name;

  # Time: 080313 5:52:04

  # User@Host: root[root] @ localhost []

  # Query_time: 583 Lock_time: 0 Rows_sent: 0 Rows_examined: 508521177

  select count(1) from t1 a, t1 b where a.id=b.id;

  /usr/local/mysql/bin/mysqld, Version: 5.0.26-standard-log. started with:

  Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock

  Time Id Command Argument

  # Time: 080313 10:39:59

  # User@Host: root[root] @ localhost []

  # Query_time: 11 Lock_time: 0 Rows_sent: 4537467 Rows_examined: 4537467

  use test;

  select id from tail;

  若是慢查詢日誌記錄不少可使用mysqldumpslow進行分類彙總

  [mysql@test2]$ mysqldumpslow slow_query_log.log

  Reading mysql slow query log from slow_query_log.log

  Count: 1 Time=583.00s (583s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost

  select count(N) from t1 a, t1 b where a.id=b.id

  Count: 1 Time=108.00s (108s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost

  select count(N) from t1 a, t1 b,t1 c where a.id=b.id and b.name=c.name

  Count: 1 Time=11.00s (11s) Lock=0.00s (0s) Rows=4537467.0 (4537467), root[root]@localhost

  select id from tail;

mysql有如下幾種日誌:   
錯誤日誌:   -log-err   
查詢日誌:   -log   
慢查詢日誌:     -log-slow-queries   
更新日誌:     -log-update   
二進制日誌:   -log-bin   
    
在mysql的安裝目錄下,打開my.ini,在後面加上上面的參數,保存後重啓mysql服務就好了。   
例如:   
#Enter   a   name   for   the   binary   log.   Otherwise   a   default   name   will   be   used.   
#log-bin=   
#Enter   a   name   for   the   query   log   file.   Otherwise   a   default   name   will   be   used.   
#log=   
#Enter   a   name   for   the   error   log   file.   Otherwise   a   default   name   will   be   used.   
log-error=   
#Enter   a   name   for   the   update   log   file.   Otherwise   a   default   name   will   be   used.   
#log-update=     

查看日至:
1. 首先確認你日誌是否啓用了
mysql>show variables like 'log_bin';
若是啓用了,即ON
那日誌文件就在mysql的安裝目錄的data目錄下
cat/tail 日誌文件名

2. 怎樣知道當前的日誌
mysql> show master status;
3. 查看從某一段時間到某一段時間的日誌


mysqlbinlog --start-datetime='2008-01-19 00:00:00' --stop-datetime='2008-01-30 00:00:00' /var/log/mysql/mysql-bin.000006 > mysqllog1.log


附錄:

//顯示全部本機上的二進制日誌
mysql> SHOW MASTER LOGS;
//刪除全部本機上的二進制日誌
mysql> RESET MASTER;
//刪除全部建立時間在binary-log.xxx以前的二進制日誌
mysql> PURGE MASTER LOGS TO 'binary-log.xxx';
//只保留最近6天的日誌,以前的都刪掉
find /var/intra -type f -mtime +6 -name "*.log" -exec rm -f {} ;
//用鍵盤左上角(也就是Esc下面)那個鍵包圍起來,說明是命令。-1d是昨天,以此類推-1m是上個月等等
day=`/bin/date -v -1d +%Y%m%d`;
//給文件更名
mv xxx.log xxx-${day}.log;
//這裏還要加上數據庫的用戶名密碼,做用是更新日誌(包括二進制日誌和查詢日誌等等)
mysqladmin flush-logs

 


  二進制日誌也一般被稱爲binlog,它記當着全部的DDL和DML,但不包括數據查詢語句。

  1.配置信息

  --log-bin=[file-name]用來指定錯誤日誌存放的位置。

  若是沒有指定[file-name],默認爲主機名後面跟-bin作爲文件名,默認存放在DATADIR目錄中。

  也能夠將log-bin配置到my.cnf文件中,這樣就省去了每次在啓動mysqld時都手工指定--log-bin.例如:

  # The MySQL server

  [mysqld]

  ......

  log-bin = /var/lib/mysql/log-bin

  ......

  2.查看blnlog

  因爲binlog以是binary方式存取,不能直接查看,須要用mysql提供的mysqlbinlog工具查看。

  3.刪除binlog

  (1).用reset master命令刪除全部日誌,新日誌從新從000001開始編號

  (2).用purge master logs to 'mysq-bin.******' 命令能夠刪除指定編號前的全部日誌

  (3).用purge master logs to before 'YYYY-MM-DD HH24:MI:SS'命令能夠刪除'YYYY-MM-DD HH24:MI:SS'以前的產生的全部日誌

  (4).能夠在my.cnf中指定--expire_logs_days=#,此參數設置了binlog日誌的過時天數

  4.測試案例

  [mysql@test2]$ mysql -uroot -p

  Enter password:

  Welcome to the MySQL monitor. Commands end with ; or g.

  Your MySQL connection id is 18 to server version: 5.0.26-standard-log

  Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

  mysql> use test;

  Database changed

  mysql> select * from pet;

  +----------+--------+---------+------+------------+------------+

  | name | owner | species | sex | birth | death |

  +----------+--------+---------+------+------------+------------+

  | Fluffy | Harold | cat | f | 1993-02-04 | NULL |

  | Claws | Gwen | cat | m | 1994-03-17 | NULL |

  | Buffy | Harold | dog | f | 1989-05-13 | NULL |

  | Fang | Benny | dog | m | 1990-08-27 | NULL |

  | Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |

  | Chirpy | Gwen | bird | f | 1998-09-11 | NULL |

  | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |

  | Slim | Benny | snake | m | 1996-04-29 | NULL |

  +----------+--------+---------+------+------------+------------+

  8 rows in set (0.06 sec)

  mysql> insert into pet values('hunter','yxyup','cat','f','1996-04-29',null);

  Query OK, 1 row affected (0.03 sec)

  mysql> select * from pet;

  +----------+--------+---------+------+------------+------------+

  | name | owner | species | sex | birth | death |

  +----------+--------+---------+------+------------+------------+

  | Fluffy | Harold | cat | f | 1993-02-04 | NULL |

  | Claws | Gwen | cat | m | 1994-03-17 | NULL |

  | Buffy | Harold | dog | f | 1989-05-13 | NULL |

  | Fang | Benny | dog | m | 1990-08-27 | NULL |

  | Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |

  | Chirpy | Gwen | bird | f | 1998-09-11 | NULL |

  | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |

  | Slim | Benny | snake | m | 1996-04-29 | NULL |

  | hunter | yxyup | cat | f | 1996-04-29 | NULL |

  +----------+--------+---------+------+------------+------------+

  9 rows in set (0.00 sec)

mysql> update pet set sex='m' where name='hunter';

  Query OK, 1 row affected (0.00 sec)

  Rows matched: 1 Changed: 1 Warnings: 0

  mysql> delete from pet where name='hunter';

  Query OK, 1 row affected (0.00 sec)

  [mysql@test2]$ mysqlbinlog log-bin.000002

  /*!40019 SET @@session.max_insert_delayed_threads=0*/;

  /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

  # at 4

  #080313 7:52:47 server id 1 end_log_pos 98 Start: binlog v 4, server v 5.0.26-standard-log created 080313 7:52:47

  # Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.

  # at 98

  #080313 10:06:26 server id 1 end_log_pos 229 Query thread_id=18 exec_time=0 error_code=0

  use test;

  SET TIMESTAMP=1205373986;

  SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1;

  SET @@session.sql_mode=0;

  /*!C latin1 */;

  SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8;

  insert into pet values('hunter','yxyup','cat','f','1996-04-29',null);

  # at 229

  #080313 10:07:13 server id 1 end_log_pos 334 Query thread_id=18 exec_time=0 error_code=0

  SET TIMESTAMP=1205374033;

  update pet set sex='m' where name='hunter';

  # at 334

  #080313 10:07:38 server id 1 end_log_pos 432 Query thread_id=18 exec_time=0 error_code=0

  SET TIMESTAMP=1205374058;

  delete from pet where name='hunter';

  # at 432

  #080313 10:14:13 server id 1 end_log_pos 532 Query thread_id=18 exec_time=0 error_code=0

  SET TIMESTAMP=1205374453;

  create table t1(id int,name char(10));

  # at 532

  #080313 10:14:41 server id 1 end_log_pos 625 Query thread_id=18 exec_time=0 error_code=0

  SET TIMESTAMP=1205374481;

  alter table t1 add sex char(2);

  # End of log file

  ROLLBACK /* added by mysqlbinlog */;

  /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

  [mysql@test2]$

  能夠看出,三條DML操做和兩條DDL都記錄到了binlog中了,而select並無記錄。

linux下MySQL操做歷史記錄 ~/.mysql_history

mysql會給出咱們最近執行的SQL命令和腳本;同linux command保存在~/.bash_history同樣,你用mysql鏈接MySQL server的全部操做也會被記錄到~/.mysql_history文件中,這樣就會有很大的安全風險了,如添加MySQL用戶的sql也一樣會被明文記錄到此文件中。

1,查看你係統的~/.mysql_history隱藏文件

(個人測試環境下,通常linux的mysql用戶來管理,因此在/home/mysql目錄下會有這個文件)
-bash-3.2$ ls -al | grep mysql_
-rw------- 1 mysql mysql 5006 Apr 10 18:53 .mysql_history
 
 

2,測試MySQL用戶管理的SQL會被記錄

 
2.1 用linux用戶mysql, 使用mysql命令行工具登陸MySQL server. 添加用戶"his_user@localhost",並設置密碼
 
mysql> grant select on rep.* to his_user@localhost identified by '123';
Query OK, 0 rows affected (0.00 sec)
 
mysql>

2.2 斷開剛纔的mysql鏈接,查看/home/mysql/.mysql_history文件,可見剛纔添加mysql user的操做已被記錄,包括明文密碼123.
 
-bash-3.2$ tail -1 ~/.mysql_history
grant select on rep.* to his_user@localhost identified by '123';
 
注意說明:這個.mysql_history不是隻存在於MySQL所在的Server, 任何你可以遠程用mysql鏈接,都會在此server上的當前用戶的~目錄下建立這個隱藏文件。
 

3, 如何清除使用痕跡,若是在生產環境中,通常不會依賴此記錄來做審計,從上面演示能夠看出,還存在必定的風險。

 
2.1 徹底清除~/.mysql_history。
 
2.1.1 刪除如今的.mysql_history文件
-bash-3.2$ rm ~/.mysql_history
 
2.1.2 建立它的軟鏈接(緣由後面說明)
-bash-3.2$ ln -s /dev/null ~/.mysql_history
 
查看軟鏈接建立成功。
-bash-3.2$ ls -al | grep mysql_
lrwxrwxrwx 1 mysql mysql 9 Apr 10 20:30 .mysql_history -> /dev/null
測試是否生效:鏈接mysql, 操做,斷開鏈接,查看操做是否還被記錄。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| backup |
-------------------------
mysql> exit
Bye
-bash-3.2$ cat ~/.mysql_history
 
可見,上面的show databases;操做命令沒有被記錄,同時當你斷開ssh後,從新鏈接mysql, 此時按「向上」鍵已無歷史操做記錄提示,說明生效了。
想要從新生效, 你直接刪除掉~/.mysql_history,當你下次再鏈接,退出後,就會從新建立此文件了。
 
2.2 只清除敏感信息;若是不想徹底禁用此功能,只是想每次作一些敏感操做後,把此文件清空即可以了。
 
-bash-3.2$ cat /dev/null > ~/.mysql_history
 
好比你修改了MySQL用戶信息,退mysql connection後,執行上操做,把所有操做痕跡清空了。
 
3 ~/.mysql_history文件的產生原理
 
3.1 由於mysql工具自己就是有一個shell, 每次mysql鏈接退出後,都會把這次操做的信息記錄到~/.mysql_history文件中,
若是此文件不存在,會先建立,再記錄(像上面的把它刪除後,或才安裝的MySQL)
 
3.2 此文件的名字,實際上是根據MYSQL_HISTFILE這個linux環境變量來設置了, 默認是這個~/.mysql_history值,那咱們測試一下其餘值。

3.2.1 在linux用戶的~/.bash_profile 中添加一行export MYSQL_HISTFILE=/home/mysql/.mydb_history
目錄根據你的linux用戶本身設置,個人測試用戶是mysql.
-bash-3.2$ vi ~/.bash_profile
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
PATH=$PATH:/usr/sbin
export MYSQL_HISTFILE=/home/mysql/.mydb_history
 
3.2.2 退出linux鏈接,從新登陸linux; 使用mysql來鏈接數據庫,操做,退出connection, 檢查~/.mydb_history隱藏文件是否建立,並檢查剛纔操做是否被記錄。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
----------------------
//退出mysql
mysql> exit
Bye
//查看隱藏文件是否建立
-bash-3.2$ cd ~; ls -tal | grep mydb_history
-rw------- 1 mysql mysql 16 Apr 10 20:55 .mydb_history
// 查看「show databases"命令是否被正確記錄到文件中
-bash-3.2$ tail -1 ~/.mydb_history
show databases;
 
從上能夠說明:此文件的文件名來自於MYSQL_HISTFILE。

原文地址:http://www.cnblogs.com/milantgh/p/3602206.html?utm_source=tuicool&utm_medium=referral