找回密码
 注册
查看: 3891|回复: 1

centos下mysql日志采集

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2017-9-27 16:19:58 | 显示全部楼层 |阅读模式
购买主题 本主题需向作者支付 2 金钱 才能浏览

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2017-9-27 16:20:58 | 显示全部楼层
通过 MySql 配置启用日志
9 n% [% Z$ j: R9 h# F1 c) C- N+ t7 O. o# N; d- D& z3 R* K
日志相关参数位于 [mysqld] 部分。" k8 K* R( i  F- c
编辑 MySql 配置文件:
: q" Q) P- o5 z3 enano /etc/mysql/my.cnf
2 }  c, `3 y- L& m  T以上是 Debian 下的默认安装目录,其他 Linux 发布版可能不太一样,这个文件中 MySql 服务器的参数如下:
. \; d- ]% ]' a# g& }8 B# * Logging and Replication1 V0 n/ L# S' Z' q7 b
#
6 h" z" p7 H& @- [# Both location gets rotated by the cronjob." N5 G" \# ^: Z8 k$ f! k5 u
# Be aware that this log type is a performance killer.
$ |( A2 a6 D' ~, a# As of 5.1 you can enable the log at runtime!
( h/ H) q- J, w! F( r6 A#general_log_file = /var/log/mysql/mysql.log$ B: `3 k3 }; W. X0 o( K
#general_log = 1
  }6 z; f# t, Y: L+ d7 W#
2 U1 F+ P& f0 b/ c" J+ k! C# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
" G8 B7 V0 |! W/ N. L8 |( i' f8 }#+ |+ a& X  r3 B  O5 J' i1 @8 V
# Here you can see queries with especially long duration
1 k/ B4 v9 T: v7 i3 X' P2 s#log_slow_queries = /var/log/mysql/mysql-slow.log. p: ?3 \6 D) F1 A
#long_query_time = 2. A& V' r2 k8 g
#log-queries-not-using-indexes
, Y8 m0 _$ b1 E9 `9 ~# ^#
+ h5 v2 }. y3 o, Q+ M6 h# The following can be used as easy to replay backup logs or for replication.
. @8 x6 @% u4 P) H# note: if you are setting up a replication slave, see README.Debian about" Y* @) }3 F- B& m
# other settings you may need to change.
. |. j/ @/ R6 W#server-id = 1! t7 r- V6 Z4 _( p% c. a
#log_bin = /var/log/mysql/mysql-bin.log
* X8 Z1 V9 Z: Jexpire_logs_days = 10
6 f) @% u( Y: r( Z5 lmax_binlog_size = 100M
, m+ H2 p. `% _#binlog_do_db = include_database_name
  U( `6 ?- p3 a5 Z( C% g#binlog_ignore_db = include_database_name( S9 {; s$ b/ K7 X- e( f
MySql 安装默认是不启用所有日志文件的(Windows 平台下的 error 日志除外)。Debian 安装 MySql 默认是将 error 日志发送给 syslog。! V" F/ g8 H4 i# E9 Z& T  f! E
error 日志* u+ b# K8 G9 W' y  g, x5 }
, ]! k: {# j/ T) X  ^* k2 Z  q# m
根据 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 配置,error 日志推送给 syslog:
" H7 q8 G& _/ ~( ~" ~. j[mysqld_safe]$ E& Y$ m% m% w* ^0 A: O+ B; L8 d
syslog
5 I9 W* g! U5 T3 C6 Y# l这是推荐的做法。如果你由于某种原因,不想讲 error 日志推给 syslog,将 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 文件中的上述行注掉,或者直接删除掉这个文件,然后在 /etc/mysql/my.cnf 中添加以下行:( E7 a" @% k( c4 i4 x: S, c. Q2 V
[mysqld_safe]1 U% z! g; }& l9 [! B. s
log_error=/var/log/mysql/mysql_error.log
; J# s. ?* n& r+ o$ ~3 X( |- Q* W" H4 Y- D3 P# y/ v
[mysqld]
" n- V: D/ Z7 z$ c, ~& Hlog_error=/var/log/mysql/mysql_error.log
) L! d7 O8 _5 _4 R% n一般查询日志; ~8 {/ t5 }/ {4 L( b- o# P  ~

- _" _/ n% x" O要启用一般查询日志,将相关行取消注释(或者添加)即可:" _# Z7 E! o, S
general_log_file = /var/log/mysql/mysql.log8 D6 w2 t- Q- D* x- C. Y
general_log = 1
* [/ Y9 R) N7 j% x$ N' q慢查询日志% F0 a4 a$ I( j6 V" h
, F; m3 N) f3 e/ y& J6 V5 k
要启用慢查询日志,将相关行取消注释(或者添加)即可:
1 Y  Y/ P2 ], n! t$ Z( Llog_slow_queries = /var/log/mysql/mysql-slow.log
- x8 G5 d9 {0 F  Hlong_query_time = 2
5 h$ m6 P/ k4 v# }) C9 Zlog-queries-not-using-indexes8 C" l! n; s+ s' h6 V/ K. L
配置修改后重启 MySql 服务器
3 G# w) g4 l! o+ u+ h4 N) }% t3 b8 v% x
以上方法要求服务重启才能生效:2 N' h" p6 I/ ~- s! R
service mysql restart$ v$ W" O* D1 B, @( @" A! M
或者使用 systemd:
/ i+ E) y$ ?6 ksystemctl restart mysql.service* e9 T7 e% N% M4 g* z
2 }4 [6 u! Y, F$ C) M

: Y9 q! E5 l/ k, W: V! K5 q" w2 x% s# O
" {7 ]5 ]' S* b( i4 q) F  Z  r+ o5 [运行时启用日志
1 A7 H; O. \) B8 A0 o' f
; S% G' `# b5 `: c, k! q9 C" BMySql 5.1 之后我们可以在运行时启用或者禁用日志。
0 n0 g8 d  a3 Y( N6 E( l运行时启用日志,登录 MySql 客户端(mysql -u root -p)然后执行:
0 D7 i; }' H/ I) I; U0 C9 Q! oSET GLOBAL general_log = 'ON';
/ j5 c# g8 x' r" V- j" OSET GLOBAL slow_query_log = 'ON';
1 t; I% M$ c7 I7 m4 k" q( P. V" [运行时禁用日志,登录 Mysql 客户端(mysql -u root -p)后执行:
0 H1 P+ d5 ~! u( [4 v8 X0 ISET GLOBAL general_log = 'OFF';
( k6 G2 X  c: ?9 a1 _# aSET GLOBAL slow_query_log = 'OFF';
- l4 [9 S7 n! P: `( ~这种方式适用于所有平台并且不需要重启服务。3 M5 X  Z, y* ]+ r' m( o# `
; r4 A& {% J& M" O4 [

6 ^# |- z' e( W9 @
7 Q/ K9 q0 ^" ?3 @显示日志结果
7 i. k" Q$ B6 ?0 A6 x" X+ g+ A  U: x8 [/ }5 x$ m
error 日志
) E- {2 \6 [. r- r5 D8 H5 {$ B
9 b9 Y/ G- I1 E$ E( A( z7 c( R9 A按以上办法设置以后,你可以通过以下命令显示 error 日志:
) z3 U. n+ n4 c- Y0 gtail -f /var/log/syslog
/ T* d9 ?) h$ `+ r& h* o备注:如果你没有配置 error 日志文件,MySql 将把 error 日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}.err 的文件中。
/ {/ b; k& S7 n) Z! J$ r普通查询日志
4 G) Q6 k4 U0 B# X. m, N( g3 i1 ^1 |6 r  a4 Z, d2 I* |- T
按以上办法设置以后,你可以通过使用以下命令来显示普通日志:
' d; h4 B* W$ p2 N# q& q7 F3 ztail -f /var/log/mysql/mysql.log* z5 [1 n& S# O  P* [  F
备注:如果你没有配置普通日志文件,MySql 将把普通日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}.log 的文件中。3 a. ?9 `2 C8 F8 ]) T" b7 {+ y! u
慢查询日志, b4 [5 d7 N+ k. P7 K6 N

4 X+ I$ o/ W) z# c' z按以上办法设置以后,你可以通过使用以下命令来显示慢查询日志:7 A& o, ~9 K0 G- G) p
tail -f /var/log/mysql/mysql-slow.log  b; f) J6 F. ]3 S
备注:如果你没有配置慢查询日志文件,MySql 将把普通日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}-slow.log 的文件中。6 K7 G' d9 u0 \8 G% g$ z3 D
1 U/ ~& X- ?/ k
" R; Q! K5 x% A5 t/ C- _
1 q+ l& u( H% U. c; B) P0 s
循环日志) }+ H" j. g! R" S
7 s9 S% [. k$ [  S
别忘了滚动日志,否则的话日志文件可能会变得很庞大。, H& g1 q1 C4 S9 Q
在 Debian(以及 Debian 派生系列诸如 Ubuntu 等)系统,MySql 初始安装之后,循环日志就已经使用了 logrotate:
5 f: x- S# a  t4 g0 r+ L% Pnano /etc/logrotate.d/mysql-server8 j* ~7 Z/ t1 k% E; l/ s
对于其他 Linux 发行版,可能需要做一些改动:- Y* n* c/ O9 r0 x4 s5 \% M2 U
# - I put everything in one block and added sharedscripts, so that mysql gets. s  Q4 I$ z/ L' }
#   flush-logs'd only once.3 h8 T% P8 b9 @0 E7 F# c+ S, Y
#   Else the binary logs would automatically increase by n times every day.
$ n# H8 R: Y# @3 X# - The error log is obsolete, messages go to syslog now.. E0 q) s, e! o7 B5 v& \
/var/log/mysql.log /var/log/mysql/mysql.log /var/log/mysql/mysql-slow.log {
; S2 J+ f" u8 c# Z$ U        daily8 w9 s+ d6 E8 t8 J0 g
        rotate 7
3 R' J! b0 W, p3 Z' V( |        missingok
& x( D! H* V5 x  X7 x8 v        create 640 mysql adm) U* M: |$ b. {% `  n* e3 _; j& [3 E
        compress
' L, \' {+ B" n& f( L% Y) @7 t        sharedscripts
' F5 o  N5 Z5 J        postrotate: M& C5 P5 q! D& y
                test -x /usr/bin/mysqladmin || exit 0; h! ~0 M, q& ^' H
                # If this fails, check debian.conf!5 j/ G5 Y/ E& K
                MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
; k" n; U+ t1 e) F4 g# b                if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then
) i2 P! X. ^2 d  S                  # Really no mysqld or rather a missing debian-sys-maint user?% E$ z0 m2 b, a2 m- y
                  # If this occurs and is not a error please report a bug.
1 R& S4 x2 E' {! S4 a                  #if ps cax | grep -q mysqld; then0 y7 y  c$ p0 |$ z
                  if killall -q -s0 -umysql mysqld; then
5 K% ]" W  k+ A                    exit 14 z. e, j) d5 ~6 e( A
                  fi
4 _+ |! g( Q" Y  e* h2 W! E+ m                else
: j+ @  a  b! u3 \- Z  d8 `                  $MYADMIN flush-logs# o5 N' P5 }- Z( T2 J& d
                fi
+ D/ E8 ~" Y$ p! D1 N3 e1 C* L        endscript' Z  }5 v& Q9 m9 O3 _( C; }
}5 g. m9 c" ]6 }1 k

# v& o; M3 O& {' k6 r- K   I7 w9 V6 g2 n* D, s6 A

* G' s4 e8 F5 H检验服务器配置
7 \: p' N2 q, u4 @
1 v& v- f' K" N1 n' y8 I) K使用 show variables like '%log%'; 来检查服务器和日志文件相关的变量:
8 E$ V! T0 z! P0 ?1 R9 i1 `root@cosmos ~ # mysql -uroot -p
3 W5 }& ]: d5 g- s1 @Enter password:
6 W/ [; d; F( U. SWelcome to the MySQL monitor. Commands end with ; or \g.
) z( f! e- c# G* a# w8 TYour MySQL connection id is 144332
* E: _- A/ i6 }9 x, N% N* YServer version: 5.5.31-0+wheezy1 (Debian)7 L3 W; R% P  C( X2 N) M
& E0 K( O  g/ E9 c- V
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
1 \1 |$ A" w+ Z( I& H6 S3 I8 P$ c# T5 N, N( _, r  F
Oracle is a registered trademark of Oracle Corporation and/or its
5 r  ~; ^2 O5 A( @affiliates. Other names may be trademarks of their respective
1 U6 H7 U' _: Q7 F  L3 p3 c, aowners.
) X4 W2 H! [2 D3 y: M2 t
2 E4 {) ~7 w! A, S1 PType 'help;' or '\h' for help. Type '\c' to clear the current input statement." T- ?8 S7 e! t3 _% D, u

' E5 l+ m/ [3 j- Qmysql> show variables like '%log%';1 n2 D! Y2 _* ]( E
+-----------------------------------------+--------------------------------+
* A$ |9 F; L7 s( J5 Z| Variable_name | Value |
+ J3 M+ q# `0 H$ U" E  u1 R+-----------------------------------------+--------------------------------+
. a3 k0 t" X& _' r/ N4 F| back_log | 50 |9 }4 x% m3 ]9 d
| binlog_cache_size | 32768 |* _7 x& \& ^# N& a+ ^! v
| binlog_direct_non_transactional_updates | OFF |+ `) r" k1 l3 R. r! Z
| binlog_format | STATEMENT |
4 f( c1 \- T* S# p. Y6 y+ j| binlog_stmt_cache_size | 32768 |
1 }, Q& ?" ]( S+ f* b+ J: ?| expire_logs_days | 10 |0 P  _* F8 n& y9 Y
| general_log | OFF |
0 [' L5 `- l5 x6 I$ {| general_log_file | /var/lib/mysql/cosmos.log |  D4 T/ Y% @  s1 t3 u! Q
| innodb_flush_log_at_trx_commit | 1 |- l$ q6 y* B0 c7 u
| innodb_locks_unsafe_for_binlog | OFF |/ ^( T- s7 W; b3 k% W, K) ^  k
| innodb_log_buffer_size | 8388608 |7 v5 E" N% g, o
| innodb_log_file_size | 5242880 |5 e' x; u1 z: R! P7 J' j
| innodb_log_files_in_group | 2 |
. ?& V+ X: c$ d" ^4 z9 Q| innodb_log_group_home_dir | ./ |
5 T4 z' o* j" q: ^  L| innodb_mirrored_log_groups | 1 |1 R9 q- W% s! s( f) G
| log | OFF |, C5 a# r+ P4 f, v; L1 E+ E' g  N
| log_bin | OFF |
& ~' `/ [' {  D$ u| log_bin_trust_function_creators | OFF |# C" L3 f( T$ d7 [
| log_error | |
. T% Z" Y8 o( Q. i! Z| log_output | FILE |8 s' z% T( A% z2 V( r
| log_queries_not_using_indexes | OFF |; X# x. q5 @; e" o) F$ o: b/ g
| log_slave_updates | OFF |
& A1 y' [) J# g| log_slow_queries | OFF |. G" n% m7 g  N' w  z& P
| log_warnings | 1 |, s' I. \. e( f% y' X$ n
| max_binlog_cache_size | 18446744073709547520 |
/ e  a$ P/ o6 R  k| max_binlog_size | 104857600 |1 b/ f* ~* q! Z) L" }! M. g4 n) W
| max_binlog_stmt_cache_size | 18446744073709547520 |$ h# U/ O' r8 o' K2 h
| max_relay_log_size | 0 |
4 S4 Y, i" `; G- Z| relay_log | |
! G9 p1 H2 r( `3 ]+ Z- K. s| relay_log_index | |
! V" d# b7 s: [) T| relay_log_info_file | relay-log.info |# V0 z8 {* H4 }. t6 @; s$ `
| relay_log_purge | ON |
" B, |; E# C% F& a  \% _4 S# s| relay_log_recovery | OFF |
6 R. G% j( M* K* G5 T| relay_log_space_limit | 0 |
  A, l- v' o+ I. G| slow_query_log | OFF |
2 t# _1 F( o1 m- V3 y" Z9 G0 n- G| slow_query_log_file | /var/lib/mysql/cosmos-slow.log |- ?, D$ M2 n! P6 V' C* n: z
| sql_log_bin | ON |! D- Z9 V5 \8 C9 Q# w( ^! s( ~
| sql_log_off | OFF |
5 }8 _' D  ?) R3 X$ e. S| sync_binlog | 0 |3 u( m+ R& l  _6 p
| sync_relay_log | 0 |# _# Q3 [3 H; p! V9 P6 O2 {9 `3 x3 c# T+ k
| sync_relay_log_info | 0 |
+ o- e3 j4 v4 x$ n+-----------------------------------------+--------------------------------+' O' l1 `: j' z1 y0 D- t, K3 M1 n
41 rows in set (0.00 sec)
! O6 N: \* }; C7 S服务器变量相关官方文档参考 https://dev.mysql.com/doc/refman/5.7/en/server-options.html
7 R9 e0 @; |, F1 s9 H; S. p, J2 [; k6 c: m
6 m3 Q2 Q* X: w+ Z& B+ ]

" v/ O. S4 {- o# a: |+ O何时启用日志  D  a5 h/ x0 P2 x3 o. W
$ {, V& O/ c% u& l7 y* C  z- \' U
MySql 默认安装的话,所有的日志文件都不会被启用的(除了 Windows 平台上的 error 日志)。Debian 上安装默认将 error 日志发给 syslog。8 S9 T  d8 z0 O# n/ f# V
实际上,在很多情况下日志文件都可以提供关键问题的解决办法:0 i  M1 u% l; A* N
总是启用 error 日志在这些情况下开启普通查询日志(最好在运行时):检查你的应用是否正确处理了 MySql 数据库连接(一个常见的错误就是从一个单一脚本多次连接到 MySql);监控来自你的应用的查询的执行情况;测试 memcached(或者类似的软件),检查某查询是被 db 执行还是被 memcached 处理当你的应用由于某些原因造成性能下降而你想找到这些慢查询时,启用慢查询日志(MySql 最好是在短期内这样配置,比如 2-3 天)
+ p3 U. `  u, ?
/ b: R) s3 V7 x) V  d
! q; o) f2 Z6 ^9 K1 d示例0 A0 I: E0 Y1 U2 H$ W# I0 W* H) ~! b( o

# d- d. G* U/ [/ n: E1 l: v以下是一个 MySql 普通日志的示例:% `; f( H* p9 x* T) }
131021 17:43:50 43 Connect root@localhost as anonymous on pnet_blog
. I4 b) B' e( w0 G43 Init DB pnet_blog
- c* W; r& W% T0 r43 Query SELECT count(id) as total_posts FROM posts WHERE date_published is not null AND date_published <= '20131021144350', q" _! q; q$ W
43 Query SELECT * FROM posts WHERE date_published is not null AND date_published <= '20131021144350' ORDER BY date_published DESC LIMIT 0,10: w: z$ d" _) @, {4 q
44 Connect root@localhost as anonymous on pnet_blog! j9 m5 c8 O5 ]8 G( g: l
44 Query SELECT id, title, impressions FROM tips WHERE date_published IS NOT NULL AND date_published <= '20131021144350' ORDER BY date_published DESC LIMIT 0, 10
* q4 V& I- A8 _- W% c, m4 m44 Quit
9 }9 B- U% \& w; u& x+ x0 l) g/ X1 A43 Quit
# J0 _7 _" A# u! x9 ]131021 17:44:28 45 Connect root@localhost as anonymous on pnet_blog# P. n: y/ m- I( A
45 Init DB pnet_blog
5 q7 q) U3 q0 G* r* i$ ]45 Query SELECT * FROM posts WHERE url='how-and-when-to-enable-mysql-logs'/ I2 V" r* Z  g( S
45 Query UPDATE posts SET impressions=impressions+1 WHERE id='41'. s4 p& r- d9 E6 e5 [; E
45 Query SELECT url, post_title FROM posts WHERE date_published IS NOT NULL AND date_published < '20131020150000' ORDER BY date_published DESC LIMIT 0,1
+ A0 o) r- i1 P: P  h; H45 Query SELECT url, post_title FROM posts WHERE date_published IS NOT NULL AND date_published > '20131020150000' ORDER BY date_published ASC LIMIT 0,1* d2 B* R, n2 e
45 Query SELECT * FROM posts WHERE date_published is not null AND date_published <= '20131021144428' AND date_published >= '20130421144428' ORDER BY impressions DESC LIMIT 0,10  b7 M# a( G6 d8 `) n$ }
46 Connect root@localhost as anonymous on pnet_blog, }5 S. \+ ?7 \! ~/ x( k. V" N
46 Query SELECT id, title, impressions FROM tips WHERE date_published IS NOT NULL AND date_published <= '20131021144428' ORDER BY date_published DESC LIMIT 0, 10
( D  I% P! Z5 _+ u46 Quit7 ]* h9 u' x+ P' k
45 Quit
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 01:59 , Processed in 0.016922 second(s), 25 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表