mysql日志查詢指令_MySQL查詢日志總結

MySQL查詢日志介紹

MySQL的查詢日志記錄了所有MySQL數據庫請求的信息。無論這些請求是否得到了正確的執行。默認文件名為hostname.log。默認情況下MySQL查詢日志是關閉的。生產環境,如果開啟MySQL查詢日志,對性能還是有蠻大的影響的。另外很多時候,MySQL慢查詢日志基本可以定位那些出現性能問題的SQL,所以MySQL查詢日志應用的場景其實不多,有點雞肋的感覺,它跟SQL Server中的profiler有點類似,但是這個不能跟蹤某個會話、用戶、客戶端。它只能對整個數據庫進行跟蹤。MySQL查詢日志本身比較簡單,網上介紹的不多,官方資料也就那么短短一篇。

MySQL查詢日志配置

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>

這三個參數general_log、 general_log_file、 log_output都是動態參數,可以隨時動態修改。

1、開啟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>

查看查詢日志信息。

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>

4: 查詢日志歸檔

mysql> system mv /var/lib/mysql/DB-Server.log??/var/lib/mysql/DB-Server.log.20170706

mysql> system mysqladmin flush-logs -p

Enter password:

或者你在shell中執行下面命令

[root@DB-Server mysql]# mv /var/lib/mysql/DB-Server.log??/var/lib/mysql/DB-Server.log.20170706

[root@DB-Server mysql]# mysqladmin flush-logs -p

Enter password:

5: 修改查詢日志名稱或位置

mysql> show variables like?'general_log%';

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

| Variable_name??? | Value??????????????????????? |

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

| general_log????? | ON?????????????????????????? |

| general_log_file | /var/lib/mysql/DB-Server.log |

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

2 rows?in?set (0.00 sec)

mysql> set?global general_log='OFF';

Query OK, 0 rows affected (0.00 sec)

mysql> set?global general_log_file='/u02/mysql_log.log';

Query OK, 0 rows affected (0.00 sec)

mysql> set?global general_log='ON';

Query OK, 0 rows affected (0.02 sec)

如果你遇到下面類似問題,這個是因為權限問題導致。

mysql> set global general_log_file='/u02/mysql_log.log';

ERROR 1231 (42000): Variable 'general_log_file' can't be set to the value of '/u02/mysql_log.log'

將對應目錄的owner修改為mysql即可解決問題。如下所示:

[root@DB-Server u02]# chown -R mysql:mysql??/u02

另外,MySQL的查詢日志記錄了所有MySQL數據庫請求的信息。無論這些請求是否得到了正確的執行。這個就是即使我查詢一個不存在的表的SQL,查詢日志依然會記錄。如下測試所示:

mysql> select * from MyDB.test1;

ERROR 1146 (42S02): Table?'MyDB.test1' doesn't exist

mysql> select * from MyDB.test2;

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

| id??? | sex? |

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

| 10001 |????? |

| 10002 |????? |

| 10003 |???? |

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

3 rows in set (0.07 sec)

mysql> select * from MyDB.kkk;

ERROR 1146 (42S02): Table 'MyDB.kkk' doesn't exist

mysql>

MySQL查詢日志的應用場景

存在即是合理,既然MySQL提供了查詢日志,那么肯定有其應用的地方。比如,我就遇到這樣一個例子,前陣子碰到別人問mysqlslap壓力測試工具中參數的問題,問題如下:

-c, --concurrency=nameNumber of clients to simulate for query to run.

--number-of-queries=#Limiteachclienttothisnumberofqueries(thisisnotexact).

比如我指定:--concurrency=50--number-of-queries=100, 那么這次測試總的sql執行次數是=100還是50*100=5000 ?

我們不知道--number-of-queries這個參數代表所有客戶端的執行次數還是每一個客戶端的執行次數,這些英文如果看得不是太明白,那么我們就動手測試一下,這個時候MySQL查詢日志就能發揮其用武之地了。

[root@DB-Server u02]# mysqlslap -u root -p --concurrency=50 --create-schema='MyDB'? --query='select * from MyDB.test2'? --number-of-queries=100;

Enter password:

Benchmark

Average number of seconds to run all queries: 0.023 seconds

Minimum number of seconds to run all queries: 0.023 seconds

Maximum number of seconds to run all queries: 0.023 seconds

Number of clients running queries: 50

Average number of queries per client: 2

如下所示,我們可以查到這個SQL總共執行了100次,也就是說number-of-queries代表總共要運行多少次查詢。每個客戶運行的查詢數量可以用查詢總數/并發數來計算。--concurrency表示并發量,也就是模擬多少個客戶端同時執行select。如果你指定--number-of-queries=100,--concurrency=50測試總的SQL執行應該是100

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541988.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541988.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541988.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

thinkphp5.0助手函數占用服務器資源

db(user) 默認情況下,每次請求都會重新連接數據庫,這樣會占用服務器資源 方法1.如果不想每次都重連可以這樣 db("List",[],false) 方法2.還可以直接改function,及則需要把 function db($name , $config [], $force true){retu…

python 無符號整數_Python中的有符號和無符號整數數組

python 無符號整數An array can be declared by using "array" module in Python. 可以通過在Python中使用“數組”模塊來聲明數組 。 Syntax to import "array" module: 導入“數組”模塊的語法: import array as array_alias_nameHere, imp…

mysql快速批量入庫_MySQL-批量入庫優化

MySQL批量入庫的方式循環一條一條入庫批量入庫通過程序組合 insert into (字段) tbl vlaues(), vlaues(), vlaues(),...事務入庫$autoCommit (isset($this->startTransaction) ? !$this->startTransaction : true);$ids array();if ($autoCommit) {$this->startTra…

docker集群搭建(k8s)

1 . 理解: Kubernetes是一個開源的,用于管理云平臺中多個主機上的容器化的應用,Kubernetes的目標是讓部署容器化的應用簡單并且高效(powerful),Kubernetes提供了應用部署,規劃,更新&#xff0c…

Python模擬刪除字符串兩邊的空白

目標:   1.使用string模塊的whitespace   2.刪除左邊、右邊以及兩邊的空白 代碼如下: [rootlocalhost python]# cat rmspace.py #!/usr/bin/env python #coding:utf8 """ 使用字符串刪除左右兩端的空白。 """from str…

xml分析錯誤:注釋未終止_錯誤:C中的未終止注釋(無效的注釋塊) 常見的C程序錯誤...

xml分析錯誤:注釋未終止Comments are used to write logic explain or anything that you do not want to compile. In C language there are two types of comments 1) Single line comment and 2) Multi-line comment. 注釋用于編寫邏輯解釋或您不想編譯的任何內容。 在C語言…

查看 mysql 狀態_查看mysql狀態的常用命令

在mysql客戶端輸入"show status"之后將會看到如下輸出:如果想要查看某個具體的值,可以使用如下命令:show status LIKE "%具體變量%";Aborted_clients 由于客戶沒有正確關閉連接已經死掉,已經放棄的連接數量.A…

常用數學符號的讀法及其含義

2019獨角獸企業重金招聘Python工程師標準>>> 常用數學符號的讀法及其含義 近來發現很多學生對一些數學符號的讀法及其含義不是很清楚。今天特把一些常用的列表如下。希望能夠提供一些幫助! 大寫 小寫 英文注音 國際音標注音 中文注音 Α…

math.atan_JavaScript中帶有示例的Math.atan()方法

math.atanJavaScript | Math.atan()方法 (JavaScript | Math.atan() Method) Math.atan() is a function in math library of JavaScript that is used to find the value of arctangent of a number. Math.atan()是JavaScript數學庫中的函數,用于查找數字的反正切值…

mysql 計算工作日_mysql計算工作日_MySQL

bitsCN.commysql計算工作日Sql代碼 DELIMITER $$ drop procedure if exists pGetWorkDays$$ create procedure pGetWorkDays(s datetime,e datetime) begin select floor(days/7)*5days%7 -case when 6 between wd and wddays%7-1 then 1 else 0 end -case when 7 between wd a…

后端碼農談前端(HTML篇)第三課:常見屬性

一、HTML全局屬性 1、核心屬性 屬性描述id設置元素的唯一 id。class設置元素的一個或多個類名(引用樣式表中的類)。style設置元素的行內樣式(CSS內聯樣式)。title設置有關元素的額外信息(可在工具提示中顯示&#xff0…

mysql數據庫的服務無法啟動_mysql5數據庫服務無法啟動

mysql5數據庫服務無法啟動我最近安裝了MySQL 5.0 Community Edition因為聽很多人介紹,mysql開源,免費,速度快,于是終于按捺不住,嘗試一下。誰知這一嘗試,噩夢就來了。[安裝環境]windows xp sp2MySQL 5.0 Co…

math.asin_JavaScript中帶有示例的Math.asin()方法

math.asinJavaScript | Math.asin()方法 (JavaScript | Math.asin() Method) Math.asin() is a function in math library of JavaScript that is used to find the value of arcsine of a number and return the value in radians. Math.asin()是JavaScript數學庫中的函數&…

POJ 3422 費用流

思路&#xff1a; 把每個方塊拆成兩個點 1個入點 1個出點 當前格子的入->出連費用-w[i][j] 容量1的邊 當前格子的入->出連費用0 容量k-1的邊 此格子的出向右&下&#xff08;如果有的話&#xff09;的格子的入連費用0容量k的邊 //By SiriusRen #include <queu…

mysql內置變量_MySQL常用內置變量

MySQL用很多常用的內置變量&#xff0c;掌握這些內置變量后對于我們快速獲取當前MySQL的配置有很大幫助&#xff0c;下面就來列舉幾個常用的變量。查看當前MySQL版本號信息。show variables like version;MariaDB [(none)]> show variables like version;------------------…

Android-TextView跑馬燈效果

要實現跑馬燈還是比較簡單的。 同時有幾個需要注意的點&#xff0c;先上代碼&#xff1a; 1 public class MTView extends TextView {2 3 4 public MTView(Context context) {5 super(context);6 }7 8 public MTView(Context context, AttributeSet attrs)…

qt沒有mysql文件夾_qt5-qt目錄下沒有mysql文件夾

我安裝的qt5.2.1&#xff0c;為什么我的qt安裝目錄下沒有 (例如&#xff1a;D:\Qt\Qt5.0.1\Sources\qtbase\src\plugins\sqldrivers\mysql\)mysql這個文件夾&#xff1f;無法Create MySQL driver for Qt5 on WindowsWindows下解決QSqlDatabase:QMySQL driver not loaded解決參考…

Linux Shell命令能力傾向問題和解答

This section contains Aptitude Questions and Answers on Linux Shell Commands. 本節包含有關Linux Shell命令的 Aptitude問答。 1) Which of the following command is used to check a Linux command is a built-in shell command or external command? cmdtypetypectyp…

php mysql 權重_PHP對MySql的常用操作

關于PHP對MySql的常用操作最近做網站&#xff0c;用PHP操作數據庫也很多次了&#xff0c;但總是忘記&#xff0c;參考了網上的很多資料&#xff0c;算是整理記錄下。數據庫操作類實現數據庫的連接&#xff0c;斷開&#xff0c;以及請求&#xff1a;/*** Created by PhpStorm.* …

Python timedelta total_seconds()方法與示例

Python timedelta.total_seconds()方法 (Python timedelta.total_seconds() Method) timedelta.timedeltotal_seconds() method is used in the timedelta class of module datetime. timedelta.timedeltotal_seconds()方法在模塊datetime的timedelta類中使用。 It uses an i…