找回密码
 注册
查看: 3890|回复: 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 配置启用日志
7 J, w1 u+ S5 R6 p  d
  |2 q1 m7 ^, ]6 k3 X2 {日志相关参数位于 [mysqld] 部分。+ e( [3 x! |$ S1 ?+ X: K( n& n
编辑 MySql 配置文件:
9 M1 b* _3 j6 m- F: ]nano /etc/mysql/my.cnf
  w2 |# \" d, x! T以上是 Debian 下的默认安装目录,其他 Linux 发布版可能不太一样,这个文件中 MySql 服务器的参数如下:
5 y0 {) z/ @. ]3 q* J% A# * Logging and Replication
) t( F5 @' _& J$ F9 v#$ I0 M4 P2 ?6 `9 h0 t
# Both location gets rotated by the cronjob.. I( q( Z) O+ W/ O
# Be aware that this log type is a performance killer.
, g0 @2 o1 x, w# As of 5.1 you can enable the log at runtime!
( n! d( d% ?( T/ }" C3 x#general_log_file = /var/log/mysql/mysql.log
+ l6 A, I  U; ?( Z; [3 h1 E#general_log = 1  g& g* _5 l! D) [
#
- U  H# {' Z7 r* O0 j# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.3 j1 h( U! {8 H  L
#: v% X  ~. v# e/ g: S* v" o
# Here you can see queries with especially long duration) V! L3 l( c$ f2 }) M1 z1 R
#log_slow_queries = /var/log/mysql/mysql-slow.log
" ~: [$ D% _. ~( T- l& ~: S2 d" z#long_query_time = 2& A! m! H" p7 W, z: k; i
#log-queries-not-using-indexes) `7 {* X9 W7 [* ]1 k4 r& b8 x  D: x
#) F: d: ]' G: K2 S, y8 i. N. r
# The following can be used as easy to replay backup logs or for replication.  d3 Y( b9 [4 i/ c* D
# note: if you are setting up a replication slave, see README.Debian about7 Z" `+ t+ J* W
# other settings you may need to change.) }. p, A4 M- {
#server-id = 1# @( @1 ^. Z/ N6 J/ Z. F: i% l
#log_bin = /var/log/mysql/mysql-bin.log+ I5 {5 v3 o* Y3 o- S  x
expire_logs_days = 10
2 @3 d; ?( J2 x* c8 x& t) \9 U. @max_binlog_size = 100M/ W* X/ ^$ F+ x) O
#binlog_do_db = include_database_name
+ S+ ~: ]8 p+ H/ Z! D  g) x. A5 n7 \#binlog_ignore_db = include_database_name
5 E# J2 @  h9 T( E0 aMySql 安装默认是不启用所有日志文件的(Windows 平台下的 error 日志除外)。Debian 安装 MySql 默认是将 error 日志发送给 syslog。
' f1 C7 F6 s# {/ M$ Zerror 日志
% M! i: n% s# G8 c$ [8 }7 r6 \7 D" k2 g3 _9 Z
根据 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 配置,error 日志推送给 syslog:
4 u- F, H" ^% q: _. d[mysqld_safe]
: D2 @2 U. g4 e9 ^4 D$ Gsyslog& ^8 v, l1 {# x
这是推荐的做法。如果你由于某种原因,不想讲 error 日志推给 syslog,将 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 文件中的上述行注掉,或者直接删除掉这个文件,然后在 /etc/mysql/my.cnf 中添加以下行:1 B' q: l4 `/ i( f' u) a
[mysqld_safe]
0 [7 }9 j5 m: ]4 {# T: jlog_error=/var/log/mysql/mysql_error.log% |) W! W/ x' z4 ?; G5 C- q, H6 p
( _% c. I* Y8 f7 e4 Y$ @" \: }' N4 a
[mysqld]
/ q. R' X5 h& b; {* alog_error=/var/log/mysql/mysql_error.log) d5 J/ t0 Z7 O  _
一般查询日志
7 S/ R5 ]) l$ H6 \3 q" D6 n" Q: L( l' e: S) M
要启用一般查询日志,将相关行取消注释(或者添加)即可:% a$ Z/ C/ Y! M
general_log_file = /var/log/mysql/mysql.log
6 T1 G' R5 Q6 F; Sgeneral_log = 1
# g) [! L; f  v; ?* q慢查询日志
9 S6 w! O  a' q
1 N: T% }7 `5 }0 ^要启用慢查询日志,将相关行取消注释(或者添加)即可:
! f, r/ Z& y) Z5 Q& Olog_slow_queries = /var/log/mysql/mysql-slow.log3 P' h  g  b* @
long_query_time = 2
* I$ u( n. W2 J! {, Blog-queries-not-using-indexes& u  Z1 W$ x$ x8 z
配置修改后重启 MySql 服务器
7 x. Y- i: b6 D" K: h) I5 H
& v/ a1 L/ L! h6 A5 ^" |9 G以上方法要求服务重启才能生效:& i; u6 A/ X* Q
service mysql restart
# x, v, F" J) }9 K2 K  `% q或者使用 systemd:
9 T" I& k1 M* V. p- ?$ C; w' {systemctl restart mysql.service, D/ M, z0 V* o# D% m) |- f
, S% W  A7 w8 Q/ ?, b

. k. x. O% K1 n% l) o( t! t8 Q0 G/ a& `' p: f
运行时启用日志
' U3 E0 d+ \# V' e+ h, [3 d5 }6 C4 [  Y# O; @
MySql 5.1 之后我们可以在运行时启用或者禁用日志。7 C, q7 `5 o  l/ `9 j& }( E
运行时启用日志,登录 MySql 客户端(mysql -u root -p)然后执行:
& h9 x/ K. J+ Y" _6 W+ T  I: s/ J3 ESET GLOBAL general_log = 'ON';
! ~! g, ~9 P& H- y8 |SET GLOBAL slow_query_log = 'ON';" }. c/ G5 x+ a2 E" d) B
运行时禁用日志,登录 Mysql 客户端(mysql -u root -p)后执行:
/ {' J/ _  z1 J& f/ m" @- xSET GLOBAL general_log = 'OFF';
+ T+ y$ l  i  @* h6 O2 f: ySET GLOBAL slow_query_log = 'OFF';1 A: Z5 ]+ G0 L. F: q
这种方式适用于所有平台并且不需要重启服务。. F5 l% J6 E/ q7 {

; s1 {: }9 i5 p4 O
4 r' |- Q- u; H) {  b) k
( b3 G, O8 _' L, A/ Z( H# o; g显示日志结果. z; a! \/ V; D0 }9 i, o: J' d3 {

1 `! U& ]# t/ V$ ~error 日志
4 x9 j+ h0 Y0 ?* {* ~4 i3 m$ s# L7 D- ~4 ]: E& f- |" E; M2 S: i
按以上办法设置以后,你可以通过以下命令显示 error 日志:8 w0 `: R$ H3 @" l
tail -f /var/log/syslog/ N4 i0 m2 u; m$ _3 Q" E2 w& p
备注:如果你没有配置 error 日志文件,MySql 将把 error 日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}.err 的文件中。7 \  V4 T1 P% R( @( C2 _
普通查询日志$ o/ H6 J9 N: k1 f: M7 O! W4 q4 i$ W" e

) ~5 N+ I9 c2 d$ C按以上办法设置以后,你可以通过使用以下命令来显示普通日志:" x* A5 U7 i( n9 A
tail -f /var/log/mysql/mysql.log
7 a& h% d/ G- ?备注:如果你没有配置普通日志文件,MySql 将把普通日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}.log 的文件中。/ h. ^1 d9 K# u* w! s
慢查询日志
5 N/ e3 N  m# }8 f6 R, b1 G) e
  y( k9 m# h* m4 {5 v, L按以上办法设置以后,你可以通过使用以下命令来显示慢查询日志:
, ^8 i! @$ U2 [# l; d5 atail -f /var/log/mysql/mysql-slow.log
% O2 S1 g9 w- n! a8 j, J备注:如果你没有配置慢查询日志文件,MySql 将把普通日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}-slow.log 的文件中。
: w+ h0 M7 _5 D) ?& s) z! p8 W8 r% G, r$ F

! D  M. D3 h/ E$ {8 V) I
7 g/ U; L( A% D5 l1 A# \6 n( H循环日志- ?% f3 e7 y! g
( g# D. Y6 O$ U: @
别忘了滚动日志,否则的话日志文件可能会变得很庞大。# v( n  X- I# m  R" b
在 Debian(以及 Debian 派生系列诸如 Ubuntu 等)系统,MySql 初始安装之后,循环日志就已经使用了 logrotate:( K4 G3 ^) ?: _- e) w; v
nano /etc/logrotate.d/mysql-server
* H: ?1 }: U5 I& w& g对于其他 Linux 发行版,可能需要做一些改动:
, b0 @' X# Y; V/ [# - I put everything in one block and added sharedscripts, so that mysql gets& p/ R. j" E" H; j- R
#   flush-logs'd only once.4 v6 A4 H  w7 T
#   Else the binary logs would automatically increase by n times every day./ p! q9 j. Y# q( O, R' [
# - The error log is obsolete, messages go to syslog now.
$ D. u/ Y/ p5 f9 X! R% u" n/var/log/mysql.log /var/log/mysql/mysql.log /var/log/mysql/mysql-slow.log {4 e- ?! A. x4 z. d
        daily* i: ?; B$ d; P
        rotate 7
( e, f2 \3 _7 v6 C! H: ]9 }        missingok% L; `1 j' T& Q
        create 640 mysql adm( t( a6 \' f+ O
        compress
. [$ e5 d' v* b2 O4 A        sharedscripts
+ I2 J+ o& W% ]) @3 I+ w9 U4 N0 {% x# T        postrotate
1 v/ c' a' [. j5 b2 K                test -x /usr/bin/mysqladmin || exit 0
8 y+ j( F# T# M+ u# I% \+ E. R4 H2 `                # If this fails, check debian.conf!- l  h! n2 j: r+ |9 Z
                MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"0 R# T! }' C6 M
                if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then
, ?) \9 P9 L" R: o: j" V                  # Really no mysqld or rather a missing debian-sys-maint user?
, U- J7 k+ j2 i' b                  # If this occurs and is not a error please report a bug.5 ^, e' A' ]) N$ s0 Z
                  #if ps cax | grep -q mysqld; then
  E) l1 h( S' f6 G                  if killall -q -s0 -umysql mysqld; then
9 b8 c' z) W- u2 E5 c; \                    exit 1
9 D9 q& \7 C( G* k- h                  fi% }% V: W- {7 f4 r- |  m# f6 E! c3 P+ a
                else
5 Q! c+ U& ~3 t4 B                  $MYADMIN flush-logs
; p7 G! _" Z2 C( i5 f! Q                fi) ^$ I, l0 K* [+ [% J. B
        endscript
; }8 |# @' ^/ X' Y  f. m' g( e}1 |, b2 F/ p% N
# Q  J5 O% C' I" [- X
  N- g$ [4 c( f/ _  Q9 @+ i
& t1 d) V  S% X3 V" T
检验服务器配置
) s, H: H4 d4 T$ P1 K
! P2 X* \5 B6 m; D1 Q使用 show variables like '%log%'; 来检查服务器和日志文件相关的变量:) ^( i/ Z* T- V% R
root@cosmos ~ # mysql -uroot -p
: j9 J2 \: @7 _4 j$ u& D% cEnter password:% Z, J% e: v0 m* O
Welcome to the MySQL monitor. Commands end with ; or \g.
- r7 D1 o/ ]$ Z  _) @Your MySQL connection id is 1443321 v  w/ X2 t: R
Server version: 5.5.31-0+wheezy1 (Debian)% A) _! i' h, Y/ Y: `

' \! P3 L* t/ KCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.: c% z3 \( T, O- D! G) B6 ?

$ f( I1 g' N6 A0 HOracle is a registered trademark of Oracle Corporation and/or its$ r) j' ~3 S$ D
affiliates. Other names may be trademarks of their respective
4 k" x) D, I% ?& ]+ Lowners.
, Y2 l+ @% M' I* S3 b/ A( d* [3 [* s. T3 S' n* i
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
8 K3 n9 {- x% M0 R2 [
5 }) ?& }: `- b* V0 U  Vmysql> show variables like '%log%';0 ~- x, q! ^6 b: H' W4 _9 J
+-----------------------------------------+--------------------------------+
8 v; o5 x+ x7 Y" }| Variable_name | Value |3 ]; G3 u$ T9 ^& _' _& T. P
+-----------------------------------------+--------------------------------+# f9 {/ a% F" W- O) n
| back_log | 50 |
3 A$ k5 S; Z5 x9 R| binlog_cache_size | 32768 |% |: {3 i* i  N3 w% ~# s
| binlog_direct_non_transactional_updates | OFF |9 W: H: g6 y1 \, y+ D7 H& h- S4 M
| binlog_format | STATEMENT |
9 P5 N& `* ]  I4 W0 b| binlog_stmt_cache_size | 32768 |) F/ e" x) x. |0 m, _
| expire_logs_days | 10 |$ `, h% W+ k8 Q) e3 g
| general_log | OFF |4 k3 ~% C6 T7 D! a+ y; u1 R1 o
| general_log_file | /var/lib/mysql/cosmos.log |
- F9 y9 `0 g$ V% U| innodb_flush_log_at_trx_commit | 1 |# w/ |  ?$ F2 U9 X" l% N: c$ N
| innodb_locks_unsafe_for_binlog | OFF |
, M1 Z- g/ {3 K3 W* c2 f3 N* |& A: q7 ]| innodb_log_buffer_size | 8388608 |  J9 a, C  t6 I1 `
| innodb_log_file_size | 5242880 |
# W! Z3 n) w# f| innodb_log_files_in_group | 2 |5 l* S, _& o& R8 k, ^2 {
| innodb_log_group_home_dir | ./ |  a5 g1 R& v5 r& W0 L. n
| innodb_mirrored_log_groups | 1 |
; ]0 k! ~& S1 ?0 S3 }, |3 B| log | OFF |
2 h0 t2 V2 D6 I, W7 p/ t| log_bin | OFF |) a$ Y) M; j2 v/ D- j9 @4 F
| log_bin_trust_function_creators | OFF |: V% l  E& r" j
| log_error | |$ O7 f3 J3 G7 y
| log_output | FILE |
' X3 w) a- R& I2 m| log_queries_not_using_indexes | OFF |- x; c; t( Z. X/ t5 v
| log_slave_updates | OFF |
' I; ^% q  v1 Z# u! R+ D| log_slow_queries | OFF |
% l, w  }9 J# W" L2 p9 U| log_warnings | 1 |
" Q& y* v; T2 y4 h3 M/ V3 n& B: @! S4 L| max_binlog_cache_size | 18446744073709547520 |  v: w5 v( I" |& A9 Z
| max_binlog_size | 104857600 |
: k6 s0 f- q3 V' f8 N8 n5 k| max_binlog_stmt_cache_size | 18446744073709547520 |
, n6 W1 m& ^% I| max_relay_log_size | 0 |
0 a" H. _' i  \, ?* L% O# O| relay_log | |
( G* g( L8 l% Q/ a# p5 r# i| relay_log_index | |) d/ ?7 c3 j5 ^9 p% z
| relay_log_info_file | relay-log.info |
; u& U& @5 N. Q  {$ k| relay_log_purge | ON |
8 }/ {  o  [/ F6 L/ [| relay_log_recovery | OFF |- S* k( g$ B2 C
| relay_log_space_limit | 0 |" {# L$ C$ T  |5 C- S4 f3 \
| slow_query_log | OFF |, ^5 j5 S# R' L" |' W1 ?+ r
| slow_query_log_file | /var/lib/mysql/cosmos-slow.log |+ d  L2 v4 E2 D9 \0 a: t
| sql_log_bin | ON |5 A: X5 E; F- v8 Z2 l
| sql_log_off | OFF |
) ~+ h! m4 g! t$ X1 r3 X' v| sync_binlog | 0 |
5 u, v; P0 k$ ~) M) d% B| sync_relay_log | 0 |6 A2 E/ n8 u( H, p0 R
| sync_relay_log_info | 0 |4 r- A% T% Y6 T) s. S
+-----------------------------------------+--------------------------------+
9 {* `8 j* F- {$ d: ^41 rows in set (0.00 sec)1 o! ^% S7 {$ N& r7 H0 a4 q8 c
服务器变量相关官方文档参考 https://dev.mysql.com/doc/refman/5.7/en/server-options.html
' c6 E0 h/ v: n. r/ I5 F7 _; M5 t5 X7 I+ q. X" z* m0 Y) o

' |& |% ?- c% n9 e! _
/ A" t# y4 F6 F$ q何时启用日志
8 k: S4 L. ^8 d/ u+ j& C1 [3 g6 m8 z# l+ x! X
MySql 默认安装的话,所有的日志文件都不会被启用的(除了 Windows 平台上的 error 日志)。Debian 上安装默认将 error 日志发给 syslog。
; P! k: u9 `, B' {" o$ t2 Q1 i7 u实际上,在很多情况下日志文件都可以提供关键问题的解决办法:
( X: c, j, u# N8 T. s总是启用 error 日志在这些情况下开启普通查询日志(最好在运行时):检查你的应用是否正确处理了 MySql 数据库连接(一个常见的错误就是从一个单一脚本多次连接到 MySql);监控来自你的应用的查询的执行情况;测试 memcached(或者类似的软件),检查某查询是被 db 执行还是被 memcached 处理当你的应用由于某些原因造成性能下降而你想找到这些慢查询时,启用慢查询日志(MySql 最好是在短期内这样配置,比如 2-3 天)+ ]0 @: |  V' C9 w: T, z

, a* U; E, \% V
5 J9 K9 ~) c/ [! O: B示例
3 |3 [. K8 {7 \% b
% @# z. L6 p- L# r: n; C9 C; k以下是一个 MySql 普通日志的示例:  ?: Y) u, v+ A! {6 V% `* i2 k
131021 17:43:50 43 Connect root@localhost as anonymous on pnet_blog
. a5 p+ i5 R+ i; i6 u1 _& S% P43 Init DB pnet_blog
" z' i% Y' V$ `4 V! c! i' Q43 Query SELECT count(id) as total_posts FROM posts WHERE date_published is not null AND date_published <= '20131021144350'& G* P% J6 C# a
43 Query SELECT * FROM posts WHERE date_published is not null AND date_published <= '20131021144350' ORDER BY date_published DESC LIMIT 0,10
( r" c% E) r5 |3 b! h; N$ `; U8 u" D44 Connect root@localhost as anonymous on pnet_blog! j' f. F+ s6 ^5 h
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, 109 B7 ?% O5 G- u9 k' a
44 Quit
# F  m' ]/ i: A43 Quit
/ b/ k  V0 b5 A4 k3 K. Y) C131021 17:44:28 45 Connect root@localhost as anonymous on pnet_blog
% X$ \" b: U+ r. l45 Init DB pnet_blog2 @& B# |* V9 {5 D. [: l8 K
45 Query SELECT * FROM posts WHERE url='how-and-when-to-enable-mysql-logs'+ V# @9 I  B3 ~$ k" Z$ m
45 Query UPDATE posts SET impressions=impressions+1 WHERE id='41'
8 s) b7 {8 p: U8 C9 ?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,12 D1 v. F) ?, M3 C4 n
45 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
) Z$ E& h  C2 D45 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
; b/ T: C$ X2 t* i# {- \3 L46 Connect root@localhost as anonymous on pnet_blog
  l# M/ [' ^2 ?' v' I46 Query SELECT id, title, impressions FROM tips WHERE date_published IS NOT NULL AND date_published <= '20131021144428' ORDER BY date_published DESC LIMIT 0, 106 b# e# d' }6 @
46 Quit
, }" q! u8 o! Y; e4 j: C3 t6 ]45 Quit
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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