1. 程式人生 > 其它 >MySQL-13-日誌管理

MySQL-13-日誌管理

常用日誌引數

經常用到的有錯誤、快慢查詢、二進位制等日誌



錯誤日誌

1 作用
記錄啟動\關閉\日常執行過程中,狀態資訊,警告,錯誤,排查MySQL執行過程的故障


2 錯誤日誌配置
預設就是開啟的:  /資料路徑下/hostname.err

自定義日誌路徑和名字:
mysql> select @@log_error;
vim /etc/my.cnf
log_error=/var/log/mysql.log
log_timestamps=system

重啟生效
show variables like 'log_error';


3 日誌內容檢視
主要關注[ERROR],看上下文



二進位制日誌

作用

(1)備份恢復必須依賴二進位制日誌
(2)主從環境必須依賴二進位制日誌

binlog配置(5.7必須加上server_id)

注意:MySQL預設是沒有開啟二進位制日誌的
基礎引數檢視:
mysql> select @@log_bin;
日誌路徑及名字
mysql> select @@log_bin_basename;
服務ID號:
mysql> select @@server_id;
二進位制日誌格式:
mysql> select @@binlog_format;
雙一標準之二:
mysql> select @@sync_binlog;


1 建立日誌目錄
mkdir /data/binlog
chown -R mysql.mysql /data/binlog

2 修改配置檔案
vim /etc/my.cnf
server_id=6                         # 5.6中,單機可以不需要此引數              
log_bin=/data/binlog/mysql-bin			# 該目錄需要有mysql.mysql許可權
binlog_format=row                   # 5.7的預設配置就是row,可以省略不寫
sync_binlog=1                       # 每次事務提交都立即刷寫binlog到磁碟

3 重啟資料庫生效
[root@db01 mysql]# /etc/init.d/mysqld restart

4 引數說明
server_id=6 
主要是在主從複製過程中必須要加的,但是在5.7版本中,要用以下引數(log_bin),開啟binlog日誌,即使是單機也是必加的
log_bin=/data/binlog/mysql-bin
(1)開啟二進位制日誌功能
(2)設定二進位制日誌目錄及名稱字首
binlog_format=row

binlog記錄了什麼

引入
binlog是SQL層的功能。記錄的是變更SQL語句,不記錄查詢語句。

1 記錄SQL語句種類
DDL :原封不動的記錄當前DDL(statement語句方式)。
DCL :原封不動的記錄當前DCL(statement語句方式)。
DML :只記錄已經提交的事務DML

2 DML三種記錄方式
binlog_format(binlog的記錄格式)引數影響
(1)statement(5.6預設)SBR(statement based replication):語句模式原封不動的記錄當前DML。
(2)row      (5.7預設)RBR(ROW based replication):記錄資料行的變化(使用者看不懂,需要工具分析)
(3)mixed    (混合)MBR(mixed based replication)模式:以上兩種模式的混合

3 題
SBR與RBR模式的對比
statement:可讀性較高,日誌量少,但是不夠嚴謹,有可能出現記錄不準確的情況
row      :可讀性很低,日誌量大,足夠嚴謹
update t1 set xxx=xxx where id>1000  -->一共500w行,row模式怎麼記錄的日誌,為什麼row模式嚴謹?
id     name    intime
insert into t1 values(1,'zs',now())
我們建議使用:row記錄模式

event(事件)是什麼

1 事件的簡介
event是二進位制日誌的最小記錄單元
對於DDL,DCL,一個語句就是一個event
對於DML語句來講:只記錄已提交的事務
例如以下列子,就被分為了4個event
begin;      120  - 340
DML1        340  - 460
DML2        460  - 550
commit;     550  - 760

2 event的組成
三部分構成:
(1) 事件的開始標識
(2) 事件內容
(3) 事件的結束標識
Position:
開始標識: at 194
結束標識: end_log_pos 254
194? 254?
某個事件在binlog中的相對位置號
位置號的作用是什麼?
為了方便我們擷取事件

日誌檔案檢視

1 檢視日誌的開啟情況
log_bin引數設定的路徑,可以找到二進位制日誌
Master [(none)]>show variables like '%log_bin%';
+---------------------------------+------------------------------+
| Variable_name                   | Value                        |
+---------------------------------+------------------------------+
| log_bin                         | ON                           |
| log_bin_basename                | /data/binlog/mysql-bin       |
| log_bin_index                   | /data/binlog/mysql-bin.index |
| log_bin_trust_function_creators | OFF                          |
| log_bin_use_v1_row_events       | OFF                          |
| sql_log_bin                     | ON                           |
+---------------------------------+------------------------------+
6 rows in set (0.01 sec)


2 檢視一共多少個binlog
Master [(none)]>show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       154 |
+------------------+-----------+
1 row in set (0.01 sec)

Master [(none)]>flush logs;
Query OK, 0 rows affected (0.03 sec)

Master [(none)]>flush logs;
Query OK, 0 rows affected (0.01 sec)

Master [(none)]>show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       201 |
| mysql-bin.000002 |       201 |
| mysql-bin.000003 |       154 |
+------------------+-----------+
3 rows in set (0.00 sec)


3 檢視mysql正在使用的日誌檔案
Master [(none)]>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
Master [(none)]>

file:當前MySQL正在使用的檔名
Position:最後一個事件的結束位置號

日誌內容檢視

1 event檢視
Master [binlog]>show binlog events in 'mysql-bin.000003';
+------------------+-----+----------------+-----------+-------------+----------------------------------------+
| Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                   |
+------------------+-----+----------------+-----------+-------------+----------------------------------------+
| mysql-bin.000003 |   4 | Format_desc    |         6 |         123 | Server ver: 5.7.20-log, Binlog ver: 4  |
| mysql-bin.000003 | 123 | Previous_gtids |         6 |         154 |                                        |
| mysql-bin.000003 | 154 | Anonymous_Gtid |         6 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mysql-bin.000003 | 219 | Query          |         6 |         319 | create database binlog                 |
| mysql-bin.000003 | 319 | Anonymous_Gtid |         6 |         384 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'   |
| mysql-bin.000003 | 384 | Query          |         6 |         486 | use `binlog`; create table t1 (id int) |
+------------------+-----+----------------+-----------+-------------+----------------------------------------+

Log_name:binlog檔名
Pos:開始的position    *****
Event_type:事件型別
Format_desc:格式描述,每一個日誌檔案的第一個事件,多使用者沒有意義,MySQL識別binlog必要資訊
Server_id:mysql服務號標識
End_log_pos:事件的結束位置號 *****
Info:事件內容*****
補充:
SHOW BINLOG EVENTS
   [IN 'log_name']
   [FROM pos]
   [LIMIT [offset,] row_count]
[root@db01 binlog]# mysql -e "show binlog events in 'mysql-bin.000004'" |grep drop


2 binlog檔案內容詳細檢視
mysqlbinlog /data/mysql/mysql-bin.000006
mysqlbinlog --base64-output=decode-rows -vvv /data/binlog/mysql-bin.000003
mysqlbinlog  -d binlog /data/binlog/mysql-bin.000003
[root@db01 binlog]# mysqlbinlog --start-datetime='2019-05-06 17:00:00' --stop-datetime='2019-05-06 17:01:00'  /data/binlog/mysql-bin.000004 


基於Position號進行日誌擷取

核心就是找擷取的起點和終點
--start-position=321
--stop-position=513
 mysqlbinlog --start-position=219 --stop-position=1347 /data/binlog/mysql-bin.000003 >/tmp/bin.sql

案例: 使用binlog日誌進行資料恢復
模擬:
1. 
[(none)]>create database binlog charset utf8;
2. 
[(none)]>use binlog;
[binlog]>create table t1(id int);
3. 
[binlog]>insert into t1 values(1);
[binlog]>commit;
[binlog]>insert into t1 values(2);
[binlog]>commit;
[binlog]>insert into t1 values(3);
[binlog]>commit;
4. 
[binlog]>drop database binlog;

恢復:
[(none)]>show master status ;
[(none)]>show binlog events in 'mysql-bin.000004';
[root@db01 binlog]# mysqlbinlog --start-position=1227 --stop-position=2342 /data/binlog/mysql-bin.000004 >/tmp/bin.sql
[(none)]>set sql_Log_bin=0; # 在此會話中,臨時將二進位制日誌關閉,恢復時不會產生新的日誌
[(none)]>source /tmp/bin.sql

[(none)]>set sql_Log_bin=1; # 恢復完成把引數改回去


案例:
1. 備份策略每天全備,有全量的二進位制日誌
2. 業務中一共10個庫,其中一個被誤drop了
3. 需要在其他9個庫正常工作過程中進行資料恢復

假如被刪除的庫為A,
1.恢復昨天A的全備
2.找到A庫到今天drop執行之前的所有二進位制日誌
3.mysqlbinlog工具匯出二進位制日誌為sql
4.執行第3部匯入的sql,恢復今天的資料


思考
實驗:兩個庫中的表交叉執行插入語句,然後刪除其中一個庫,要怎麼恢復?
mysqlbinlog -d 指定被刪除的庫 --start-position=1227 --stop-position=2342 /data/binlog/mysql-bin.000004 >/tmp/bin.sql
另外,如果有多個二進位制日誌怎麼恢復?
一個一個的binlog日誌檔案匯入為sql,然後恢復到資料庫

binlog日誌的GTID新特性

1 GTID 介紹
5.6版本新加的特性,5.7中做了加強
5.6中不開啟,沒有這個功能.
5.7中的GTID,即使不開也會有自動生成
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'


2. GTID(Global Transaction ID)
是對於一個已提交事務的編號,並且是一個全域性唯一的編號。
它的官方定義如下:
GTID = source_id:transaction_id


[root@db01 data]# pwd
/data/mysql/data
[root@db01 data]# ls
auto.cnf  db01.pid  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  mysql  performance_schema  school  sys  test  world  zabbix
[root@db01 data]# cat auto.cnf 
[auto]
server-uuid=a4b648cb-11b8-11ea-8636-000c2990cef0

7E11FA47-31CA-19E1-9E56-C43AA21293967:29


重要引數介紹:
vim /etc/my.cnf
gtid-mode=on	                   # 開啟gtid模式
enforce-gtid-consistency=true	   # 強調gtid一致性
# systemctl restart mysqld	     # 重啟服務

Master [(none)]>create database gtid charset utf8;
Query OK, 1 row affected (0.01 sec)

Master [(none)]>show master status ;
+------------------+----------+--------------+------------------+----------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                      |
+------------------+----------+--------------+------------------+----------------------------------------+
| mysql-bin.000004 |      326 |              |                  | dff98809-55c3-11e9-a58b-000c2928f5dd:1 |
+------------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)

Master [(none)]>use gtid
Database changed
Master [gtid]>create table t1 (id int);
Query OK, 0 rows affected (0.01 sec)

Master [gtid]>show master status ;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000004 |      489 |              |                  | dff98809-55c3-11e9-a58b-000c2928f5dd:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

Master [gtid]>create table t2 (id int);
Query OK, 0 rows affected (0.01 sec)

Master [gtid]>create table t3 (id int);
Query OK, 0 rows affected (0.02 sec)

Master [gtid]>show master status ;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000004 |      815 |              |                  | dff98809-55c3-11e9-a58b-000c2928f5dd:1-4 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

Master [gtid]>begin;
Query OK, 0 rows affected (0.00 sec)

Master [gtid]>insert into t1 values(1);
Query OK, 1 row affected (0.00 sec)

Master [gtid]>commit;
Query OK, 0 rows affected (0.00 sec)

Master [gtid]>show master status ;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000004 |     1068 |              |                  | dff98809-55c3-11e9-a58b-000c2928f5dd:1-5 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

Master [gtid]>begin;
Query OK, 0 rows affected (0.00 sec)

Master [gtid]>insert into t2 values(1);
Query OK, 1 row affected (0.00 sec)

Master [gtid]>commit;
Query OK, 0 rows affected (0.01 sec)

Master [gtid]>show master status ;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000004 |     1321 |              |                  | dff98809-55c3-11e9-a58b-000c2928f5dd:1-6 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)


3. 基於GTID進行檢視binlog
具備GTID後,擷取檢視某些事務日誌:
--include-gtids
--exclude-gtids
mysqlbinlog --include-gtids='dff98809-55c3-11e9-a58b-000c2928f5dd:1-6' --exclude-gtids='dff98809-55c3-11e9-a58b-000c2928f5dd:4'  /data/binlog/mysql-bin.000004


4 GTID的冪等性
開啟GTID後,MySQL恢復Binlog時,重複GTID的事務不會再執行了
就想恢復?怎麼辦?  --skip-gtids
# mysqlbinlog --include-gtids='3ca79ab5-3e4d-11e9-a709-000c293b577e:4' /data/binlog/mysql-bin.000004 >/tmp/binlog.sql
set sql_log_bin=0;
source /tmp/binlog.sql
set sql_log_bin=1;


使用二進位制日誌恢復資料案例

1 故障環境介紹
建立了一個庫db,匯入了表t1,t1表中錄入了很多資料
一個開發人員,drop database db;
沒有備份,日誌都在,怎麼恢復?
思路:找到建庫語句到刪庫之前所有的日誌,進行恢復(開啟了GTID模式)

故障案例模擬:
drop database if exists db;
create database db charset utf8; 
use db;
create table t1 (id int);
insert into t1 values(1),(2),(3);
insert into t1 values(4),(5),(6);
commit;
update t1 set id=30 where id=3;
commit;
delete from t1 where id=4;
commit;
insert into t1 values(7),(8),(9);
commit;
drop database db;
執行以上語句,模擬故障場景
需求:將資料庫恢復到以下狀態(提示第13步drop是誤操作,其他都是正常操作)


2 有GTID的恢復
2.1檢視當前使用的binlog檔案
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+------------------+----------+--------------+------------------+-------------------------------------------+
| mysql-bin.000012 |     1941 |              |                  | a4b648cb-11b8-11ea-8636-000c2990cef0:1-14 |
+------------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

2.2檢視事件:
mysql> show binlog events in 'mysql-bin.000012';
+------------------+------+----------------+-----------+-------------+-------------------------------------------------------------+
| Log_name         | Pos  | Event_type     | Server_id | End_log_pos | Info                                                        |
+------------------+------+----------------+-----------+-------------+-------------------------------------------------------------+
| mysql-bin.000012 |    4 | Format_desc    |         6 |         123 | Server ver: 5.7.26-log, Binlog ver: 4
| mysql-bin.000012 |  123 | Previous_gtids |         6 |         194 | a4b648cb-11b8-11ea-8636-000c2990cef0:1-6
| mysql-bin.000012 |  194 | Gtid           |         6 |         259 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:7'  |
| mysql-bin.000012 |  259 | Query          |         6 |         350 | drop database if exists db                                         |
| mysql-bin.000012 |  350 | Gtid           |         6 |         415 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:8'  |
| mysql-bin.000012 |  415 | Query          |         6 |         516 | create database db charset utf8                                    |
| mysql-bin.000012 |  516 | Gtid           |         6 |         581 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:9'  |
| mysql-bin.000012 |  581 | Query          |         6 |         675 | use `db`; create table t1 (id int)                                 |
| mysql-bin.000012 |  675 | Gtid           |         6 |         740 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:10' |
| mysql-bin.000012 |  740 | Query          |         6 |         810 | BEGIN                                                              |
| mysql-bin.000012 |  810 | Table_map      |         6 |         853 | table_id: 110 (db.t1)                                              |
| mysql-bin.000012 |  853 | Write_rows     |         6 |         903 | table_id: 110 flags: STMT_END_F                                    |
| mysql-bin.000012 |  903 | Table_map      |         6 |         946 | table_id: 110 (db.t1)                                              |
| mysql-bin.000012 |  946 | Write_rows     |         6 |         996 | table_id: 110 flags: STMT_END_F                                    |
| mysql-bin.000012 |  996 | Xid            |         6 |        1027 | COMMIT /* xid=42 */                                                |
| mysql-bin.000012 | 1027 | Gtid           |         6 |        1092 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:11' |
| mysql-bin.000012 | 1092 | Query          |         6 |        1162 | BEGIN                                                              |
| mysql-bin.000012 | 1162 | Table_map      |         6 |        1205 | table_id: 110 (db.t1)                                              |
| mysql-bin.000012 | 1205 | Update_rows    |         6 |        1251 | table_id: 110 flags: STMT_END_F                                    |
| mysql-bin.000012 | 1251 | Xid            |         6 |        1282 | COMMIT /* xid=45 */                                                |
| mysql-bin.000012 | 1282 | Gtid           |         6 |        1347 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:12' |
| mysql-bin.000012 | 1347 | Query          |         6 |        1417 | BEGIN                                                              |
| mysql-bin.000012 | 1417 | Table_map      |         6 |        1460 | table_id: 110 (db.t1)                                              |
| mysql-bin.000012 | 1460 | Delete_rows    |         6 |        1500 | table_id: 110 flags: STMT_END_F                                    |
| mysql-bin.000012 | 1500 | Xid            |         6 |        1531 | COMMIT /* xid=47 */                                                |
| mysql-bin.000012 | 1531 | Gtid           |         6 |        1596 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:13' |
| mysql-bin.000012 | 1596 | Query          |         6 |        1666 | BEGIN                                                              |
| mysql-bin.000012 | 1666 | Table_map      |         6 |        1709 | table_id: 110 (db.t1)                                              |
| mysql-bin.000012 | 1709 | Write_rows     |         6 |        1759 | table_id: 110 flags: STMT_END_F                                    |
| mysql-bin.000012 | 1759 | Xid            |         6 |        1790 | COMMIT /* xid=49 */                                                |
| mysql-bin.000012 | 1790 | Gtid           |         6 |        1855 | SET @@SESSION.GTID_NEXT= 'a4b648cb-11b8-11ea-8636-000c2990cef0:14' |
| mysql-bin.000012 | 1855 | Query          |         6 |        1941 | drop database db                                    
+------------------+------+----------------+-----------+-------------+-----------------------------------------------------------+

3.擷取
[root@db01 binlog]# mysqlbinlog --skip-gtids --include-gtids='a4b648cb-11b8-11ea-8636-000c2990cef0:7:7-13' mysql-bin.000012 >/tmp/bin.sql
引數說明:
--skip-gtids:   跳過gtid檢測
--include-gtids:包含的gtid
--exclude-gtids:不包含的gtid  --exclude-gtids='3ca79ab5-3e4d-11e9-a709-000c293b577e:8','3ca79ab5-3e4d-11e9-a709-000c293b577e:10'

4.恢復
mysql> set sql_log_bin=0;
mysql> source /tmp/bin.sql;
mysql> set sql_log_bin=0;

5.驗證資料
mysql> select * from t1;
+------+
| id   |
+------+
|    1 |
|    2 |
|   30 |
|    5 |
|    6 |
|    7 |
|    8 |
|    9 |
+------+
8 rows in set (0.00 sec)

二進位制日誌其他操作

1 自動清理日誌
show variables like '%expire%';
expire_logs_days  0   
自動清理時間,是要按照全備週期+1
set global expire_logs_days=8;
永久生效:
my.cnf
expire_logs_days=15;
企業建議,至少保留兩個全備週期+1的binlog


2 手工清理
mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |      1052 |
| mysql-bin.000002 |       201 |
| mysql-bin.000003 |      1041 |
| mysql-bin.000004 |       969 |
| mysql-bin.000005 |       969 |
| mysql-bin.000006 |      3149 |
| mysql-bin.000007 |       201 |
| mysql-bin.000008 |      2604 |
| mysql-bin.000009 |      1072 |
| mysql-bin.000010 |       602 |
| mysql-bin.000011 |      1368 |
| mysql-bin.000012 |      1941 |
+------------------+-----------+
12 rows in set (0.01 sec)

mysql> PURGE BINARY LOGS TO 'mysql-bin.000010';
Query OK, 0 rows affected (0.00 sec)

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000010 |       602 |
| mysql-bin.000011 |      1368 |
| mysql-bin.000012 |      1941 |
+------------------+-----------+
3 rows in set (0.00 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000010 |       602 |
| mysql-bin.000011 |      1368 |
| mysql-bin.000012 |      1988 |
| mysql-bin.000013 |       194 |
+------------------+-----------+
4 rows in set (0.00 sec)

注意:不要手工rm binlog檔案
1. my.cnf binlog關閉掉,啟動資料庫
2.把資料庫關閉,開啟binlog,啟動資料庫
刪除所有binlog,並從000001開始重新記錄日誌

reset master; 主從關係中,主庫執行此操作,主從環境必崩

3 日誌是怎麼滾動
flush logs; 
重啟mysql也會自動滾動一個新的
日誌檔案達到1G大小(max_binlog_size)
max_binlog_size                          | 1073741824    

備份時,加入引數也可以自動滾動



慢日誌

1 作用
記錄慢SQL語句的日誌,定位低效SQL語句的工具日誌


2 開啟慢日誌(預設沒開啟)
開關:
slow_query_log=1 
檔案位置及名字 
slow_query_log_file=/data/mysql/slow.log
設定慢查詢時間:
long_query_time=0.1	# 根據業務的需求更改,預設是10s
沒走索引的語句也記錄:
log_queries_not_using_indexes

vim /etc/my.cnf
slow_query_log=1 
slow_query_log_file=/data/mysql/slow.log
long_query_time=0.1
log_queries_not_using_indexes

systemctl restart mysqld


3 mysqldumpslow 分析慢日誌
mysqldumpslow -s c -t 10 /data/mysql/slow.log

# 第三方工具 這個工具很好用
https://www.percona.com/downloads/percona-toolkit/LATEST/
yum install perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-IO-Socket-SSL perl-Digest-MD5
toolkit工具包中的命令:
pt-query-diagest  /data/mysql/slow.log
Anemometer基於pt-query-digest將MySQL慢查詢視覺化


優化案例:
SELECT SUM(invalid_count) as invalid_count, SUM(confirm_count) as confirm_count,SUM(not_confirm_count) as not_confirm_count,SUM(total_count) as total_count
FROM (
	 		select count(att.LogID) as invalid_count, 0 as confirm_count,0 as not_confirm_count,0 as total_count,bp.city_id
      FROM builder_project_info as bp
      JOIN attendentlog as att ON bp.project_id = att.CompanyID
      WHERE att.CompanyID in (SELECT project_id from builder_project_info WHERE city_id = 407) and  att.LogTime like "2019-06%" and att.state = 2
      GROUP BY bp.city_id
UNION all (
			select 0 as invalid_count, count(att.LogID) as confirm_count,0 as not_confirm_count,0 as total_count,bp.city_id
      FROM builder_project_info as bp
      JOIN attendentlog as att ON bp.project_id = att.CompanyID
      WHERE att.CompanyID in (SELECT project_id from builder_project_info WHERE city_id = 407) and  att.LogTime like "2019-06%" and att.state = 1
      GROUP BY bp.city_id
		)
UNION all (
			select 0 as invalid_count, 0 as confirm_count,count(att.LogID) as not_confirm_count,0 as total_count,bp.city_id
      FROM builder_project_info as bp
      JOIN attendentlog as att ON bp.project_id = att.CompanyID
      WHERE att.CompanyID in (SELECT project_id from builder_project_info WHERE city_id = 407) and  att.LogTime like "2019-06%" and att.state = 0
      GROUP BY bp.city_id
		)
    UNION all (
	 select 0 as invalid_count, 0 as confirm_count,0 as not_confirm_count,count(att.LogID) as total_count,bp.city_id
   FROM builder_project_info as bp
   JOIN attendentlog as att ON bp.project_id = att.CompanyID
   WHERE att.CompanyID in (SELECT project_id from builder_project_info WHERE city_id = 407) and  att.LogTime like  "2019-06%"
   GROUP BY bp.city_id
		)
   ) as a
 GROUP BY a.city_id


desc city 
desc country

show index from city 

轉載請註明出處喲~ https://www.cnblogs.com/lichengguo