欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TiDB开启慢查询日志记录

程序员文章站 2022-07-13 08:59:54
...
方法1:
--开启慢查询日志:
在所有的TiDB节点操作:
mysql> show variables like '%general%log%';
+------------------+-------------------------------------+
| Variable_name    | Value                               |
+------------------+-------------------------------------+
| tidb_general_log | 0                                   |
| general_log      | OFF                                 |
| general_log_file | /usr/local/mysql/data/localhost.log |
+------------------+-------------------------------------+
3 rows in set (0.01 sec)

mysql> set global tidb_general_log=1;
ERROR 1105 (HY000): Variable 'tidb_general_log' is a SESSION variable and can't be used with SET GLOBAL
mysql> set tidb_general_log=1;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%general%log%';
+------------------+-------------------------------------+
| Variable_name    | Value                               |
+------------------+-------------------------------------+
| tidb_general_log | 1                                   |
| general_log      | OFF                                 |
| general_log_file | /usr/local/mysql/data/localhost.log |
+------------------+-------------------------------------+
3 rows in set (0.01 sec)

方法2:
2.在中控机上开启配置文件中的慢查询:

cd /home/tidb/tidb-ansible/conf
vim tidb.yml 

log:
  # Log level: debug, info, warn, error, fatal.
  # level: "info"

  # Log format, one of json, text, console.
  # format: "text"

  # Disable automatic timestamps in output
  # disable-timestamp: false

  # Queries with execution time greater than this value will be logged. (Milliseconds)
  # slow-threshold: 300

  # Queries with internal result greater than this value will be logged.
  # expensive-threshold: 10000

  # Maximum query length recorded in log.
  # query-log-max-len: 2048

  # File logging.
  file:
    # Max log file size in MB. (upper limit to 4096MB).
    # max-size: 300

 # Max log file keep days. No clean up by default.
    # max-days: 0

 # Maximum number of old log files to retain. No clean up by default.
    # max-backups: 0

 # Rotate log by day
    # log-rotate: true
慢查询的日志级别设置:
默认值采用info,输出格式为txt,超过300ms即0.3s为慢查询,日志文件默认为300MB,日志文件默认永久保留,按照天自动保留日志。

slow-threshold: 300  -->200
max-size: 300  -->128
 max-days: 0  -->7
 max-backups: 0 -->10
 
 慢查询日志默认记录到 tidb.log 中(默认的位置:/data/deploy/log/tidb.log),如果希望生成单独的慢查询日志文件,修改 inventory.ini 配置文件的参数 enable_slow_query_log 为 True。
 
 
 开启TiDB cluster 的binlog文件:
 /home/tidb/tidb-ansible
 #vim inventory.ini
 ## binlog trigger
enable_binlog = False  --> True
# store slow query log into seperate file
enable_slow_query_log = False  --> True


修改上述参数后 执行:
执行 ansible-playbook rolling_update.yml --tags=tidb
实现tiDB的滚动升级

tidb-server 将在 tidb_slow_query.log 文件中记录慢查询日志。

登录tidb节点查看慢查询日志:
# pwd
/data/deploy/log
# ls -l tidb_slow_query.log 
-rw-r--r-- 1 tidb tidb 887055 Aug 23 14:55 tidb_slow_query.log

慢查询日志信息格式:

# tail -f tidb_slow_query.log 
2018/08/23 14:41:46.846 adapter.go:364: [warning] [SLOW_QUERY] cost_time:467.845041ms succ:true con:28 user:[email protected] txn_start_ts:402392745545826315 database: table_ids:[6433],index_ids:[1],sql:UPDATE `ProductDB`.`productstore` SET `Id` = '5980a327d33c41cc9a296b6fed865781', `City_Id` = 420, `Warehouse_Id` = 4201, `TotalCount_MinUnit` = 432, `ProductSpecification_Id` = 7478, `OwnerType` = 0, `Owner_Id` = NULL, `OwnerName` = NULL, `CreateTime` = '2017-09-05 02:00:07', `LastUpdateTime` = '2018-08-23 14:41:43', `SaleTotalCount_MinUnit` = 696, `productionDate` = '2018-07-27', `LastModifyTime` = '2018-08-23 14:41:43', `productSku_Id` = 42000007478623 WHERE `Id` = '5980a327d33c41cc9a296b6fed865781' LIMIT 1;
2018/08/23 14:43:19.082 adapter.go:364: [warning] [SLOW_QUERY] cost_time:6m6.056520641s succ:true con:405 user:[email protected] txn_start_ts:402392673941192710 database:benchmark sql:CREATE INDEX k_10 ON sbtest10(k)
2018/08/23 14:45:21.652 adapter.go:364: [warning] [SLOW_QUERY] cost_time:7m0.055470947s succ:true con:387 user:[email protected] txn_start_ts:402392691924271106 database:benchmark sql:CREATE INDEX k_21 ON sbtest21(k)
2018/08/23 14:47:25.688 adapter.go:364: [warning] [SLOW_QUERY] cost_time:9m3.069588427s succ:true con:394 user:[email protected] txn_start_ts:402392692186415114 database:benchmark sql:CREATE INDEX k_31 ON sbtest31(k)
2018/08/23 14:49:20.969 adapter.go:364: [warning] [SLOW_QUERY] cost_time:10m54.048474535s succ:true con:364 user:[email protected] txn_start_ts:402392693313634308 database:benchmark sql:CREATE INDEX k_23 ON sbtest23(k)
2018/08/23 14:49:40.336 adapter.go:364: [warning] [SLOW_QUERY] cost_time:1.520935603s succ:true con:486 user:[email protected] txn_start_ts:402392869448187908 database:benchmark table_ids:[7299],sql:select count(1) from sbtest1
2018/08/23 14:51:23.101 adapter.go:364: [warning] [SLOW_QUERY] cost_time:12m54.026793359s succ:true con:372 user:[email protected] txn_start_ts:402392693877243912 database:benchmark sql:CREATE INDEX k_18 ON sbtest18(k)
2018/08/23 14:53:23.773 adapter.go:364: [warning] [SLOW_QUERY] cost_time:14m51.035969934s succ:true con:370 user:[email protected] txn_start_ts:402392694834069517 database:benchmark sql:CREATE INDEX k_29 ON sbtest29(k)
2018/08/23 14:55:15.747 adapter.go:364: [warning] [SLOW_QUERY] cost_time:16m39.067074879s succ:true con:356 user:[email protected] txn_start_ts:402392695869538313 database:benchmark sql:CREATE INDEX k_8 ON sbtest8(k)
2018/08/23 14:57:14.879 adapter.go:364: [warning] [SLOW_QUERY] cost_time:18m36.104647133s succ:true con:382 user:[email protected] txn_start_ts:402392696420040711 database:benchmark sql:CREATE INDEX k_22 ON sbtest22(k)