找回密码
 注册
查看: 3892|回复: 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 配置启用日志
0 e$ o# X) o) j1 G% D
9 V- R" |/ N, l日志相关参数位于 [mysqld] 部分。+ K- R& a! I( o$ W: o4 L
编辑 MySql 配置文件:
5 \& k% \# X* \3 f! u# Knano /etc/mysql/my.cnf
( H; E2 w. p) W以上是 Debian 下的默认安装目录,其他 Linux 发布版可能不太一样,这个文件中 MySql 服务器的参数如下:
9 ^7 g" ]* {) c" N# * Logging and Replication5 C  m- h4 r6 n1 y" ~+ t% [: f
#
8 L4 a. |2 T) ?9 w( @, w$ }1 I# Both location gets rotated by the cronjob.
* ?! W/ o6 }# b! ^3 G9 G# Be aware that this log type is a performance killer.
: q/ o, {8 `  y' O' m# As of 5.1 you can enable the log at runtime!
8 H5 a6 j! b3 C/ E; ]/ n#general_log_file = /var/log/mysql/mysql.log9 h6 j: o, I3 j% a# w) J
#general_log = 1, B+ t4 w3 Y0 Q9 Y. I; ]/ A, h5 `
#5 d; j" o3 W+ S) v& t4 M; c& ~  r# A; C6 r
# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.* C6 I5 ~' K. v
#
) c8 S# o/ x* l9 M3 q/ v0 j9 \# Here you can see queries with especially long duration
7 J& }1 ]! |  Z8 Q1 H, _  ]. u#log_slow_queries = /var/log/mysql/mysql-slow.log
% w; c) g* g0 @, y/ C( c+ G#long_query_time = 2- I9 n" \; P9 w, N5 B
#log-queries-not-using-indexes: C. N( J- T$ @
#
: w8 _7 d3 p0 y) E- r  G# The following can be used as easy to replay backup logs or for replication.5 }9 y0 L' _# l0 ^4 v, X4 s
# note: if you are setting up a replication slave, see README.Debian about; `6 K3 x- H3 K3 T$ e
# other settings you may need to change.2 C( f; \0 I5 ]+ I$ @9 `5 M
#server-id = 1
1 e( V8 H* t4 g5 n9 s2 `#log_bin = /var/log/mysql/mysql-bin.log
, B( [% M9 Z/ q, pexpire_logs_days = 10
; d+ o0 c" `2 T, e! v; ~max_binlog_size = 100M: {* H* y4 t) A3 H- {- x0 ]
#binlog_do_db = include_database_name. B! m8 c% s; w1 Y1 Z
#binlog_ignore_db = include_database_name/ J: N% P! S- }+ D' u4 o
MySql 安装默认是不启用所有日志文件的(Windows 平台下的 error 日志除外)。Debian 安装 MySql 默认是将 error 日志发送给 syslog。2 s' H! D" I7 D% R3 d
error 日志2 k# {" t" k. ^$ Q' g+ S/ ?# n0 s

4 `; [% O0 j  L. l9 }" [* }  C) p7 B根据 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 配置,error 日志推送给 syslog:
8 d1 I) Y, w5 l! U% a[mysqld_safe]* m$ K# J- \8 q# U+ t
syslog
  Q( G* z$ c$ F: [这是推荐的做法。如果你由于某种原因,不想讲 error 日志推给 syslog,将 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 文件中的上述行注掉,或者直接删除掉这个文件,然后在 /etc/mysql/my.cnf 中添加以下行:4 c- U1 J/ n, O, O
[mysqld_safe]
1 K2 P+ H9 m+ g7 H& P. tlog_error=/var/log/mysql/mysql_error.log! o& a/ X% k$ {

+ ?/ D, B# A5 s. S" z7 ~[mysqld]
- V) B3 f4 `+ ?: w* g9 ?log_error=/var/log/mysql/mysql_error.log
2 }; D- O8 |4 j3 W一般查询日志: X2 ^- d% O9 m* ]2 O1 Y

& v& I! j. e+ |4 e" N' p, e要启用一般查询日志,将相关行取消注释(或者添加)即可:7 V6 l- S' S# K- F% n
general_log_file = /var/log/mysql/mysql.log
, X" i; W* r5 o, ~general_log = 1' L; y7 ~; q* H; V; z9 o7 [
慢查询日志
. V: l' {# `( @) B( {' f
0 N7 L; s( E/ J8 u9 d  S. S( F1 V要启用慢查询日志,将相关行取消注释(或者添加)即可:
' m4 i2 C* z& s3 R2 Nlog_slow_queries = /var/log/mysql/mysql-slow.log$ A2 {" ?8 p' H6 _9 {9 S3 x0 I- O
long_query_time = 2% t8 N( }/ X/ d, [8 w# W0 M
log-queries-not-using-indexes
. ^: ]. p9 O9 B5 {5 ]' t! q配置修改后重启 MySql 服务器: x  Q" Y) I/ o) W: c

& a; L$ e& |5 ]$ T$ [* i以上方法要求服务重启才能生效:0 ~9 m. X$ q6 n) h
service mysql restart, a4 m  z2 D% H7 M# g+ y: A& W; u9 v
或者使用 systemd:! X# Z7 c# _4 l. y  r' X) v
systemctl restart mysql.service
: c4 Y, o- Q; Z9 f& r; f! Z0 h! ]8 y0 c
0 g" `4 [! x. @7 [! {6 q
1 |# S- P0 W: J; W
运行时启用日志
+ B; G% G3 M, t
% R2 g4 ]& C- v* n. hMySql 5.1 之后我们可以在运行时启用或者禁用日志。" A+ w' a  U$ v) H6 d$ ?# b
运行时启用日志,登录 MySql 客户端(mysql -u root -p)然后执行:
) m! Y) B* ?1 X. n' F' Z/ P8 VSET GLOBAL general_log = 'ON';
! H- i8 S4 ]( p# \* `( v6 K( kSET GLOBAL slow_query_log = 'ON';( y. u& [5 h1 M5 [9 o# v
运行时禁用日志,登录 Mysql 客户端(mysql -u root -p)后执行:
1 ?9 a1 r% [" f* ^: V! W* OSET GLOBAL general_log = 'OFF';
8 M/ T+ Y& v2 ^3 uSET GLOBAL slow_query_log = 'OFF';7 A: Z" t6 \. k) K* E2 m8 A
这种方式适用于所有平台并且不需要重启服务。
' Q5 g& I- b2 q3 _1 j
3 M$ }3 Y# [9 M + Z2 a: j0 }: ]3 {

2 t8 G* ~; d( h' v- D显示日志结果( s/ z( v( E' m

# A" D, }% c: ^% l/ g: m8 m3 R5 ^error 日志
9 e" q6 h- D3 {8 e3 i7 t$ T; z3 ^7 ?4 H8 n/ k! |  c1 ]# j& Y& N
按以上办法设置以后,你可以通过以下命令显示 error 日志:3 b! E$ C& `2 }6 m. o0 f9 n. M
tail -f /var/log/syslog
/ s; q( C; K' D: n. Y' ]! m$ B备注:如果你没有配置 error 日志文件,MySql 将把 error 日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}.err 的文件中。0 R' T5 R( h. }+ I$ _8 A6 ?- U
普通查询日志* t5 X- L3 R% N! @2 }6 \
$ j1 v  y8 L" K; f: \# Z* i3 y: w
按以上办法设置以后,你可以通过使用以下命令来显示普通日志:8 E& q0 E; u* [* e& U, o
tail -f /var/log/mysql/mysql.log
( ]! _+ R3 c* M9 ?' F7 A7 s, A备注:如果你没有配置普通日志文件,MySql 将把普通日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}.log 的文件中。
$ L. r! G: w$ h) ~  d- s. }慢查询日志  q4 c6 d$ M, D

# X) G6 f' n- A- M9 T) S5 r按以上办法设置以后,你可以通过使用以下命令来显示慢查询日志:* V1 `5 n- P2 F
tail -f /var/log/mysql/mysql-slow.log# p9 H9 Y% ^8 g8 c
备注:如果你没有配置慢查询日志文件,MySql 将把普通日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}-slow.log 的文件中。
; y$ `- ~2 v' y9 k- p" E, [# E+ M( v, d( k9 d6 i5 ~
3 W& j, D" h' C. q" I/ D( [# N( e" U

: r3 m, K3 }& }循环日志, C  I3 y+ A8 `" ^( x7 c
$ g4 @' T2 j8 K
别忘了滚动日志,否则的话日志文件可能会变得很庞大。
& q  t7 @) R/ V在 Debian(以及 Debian 派生系列诸如 Ubuntu 等)系统,MySql 初始安装之后,循环日志就已经使用了 logrotate:
2 \1 X/ b5 C) O" |, `  ^, s+ knano /etc/logrotate.d/mysql-server0 c! j! {( A0 C, x1 d
对于其他 Linux 发行版,可能需要做一些改动:) y0 G( d) E) U7 o6 z: o& A
# - I put everything in one block and added sharedscripts, so that mysql gets+ a5 r* x  L3 X. @7 N- @2 W! m
#   flush-logs'd only once.
" K, H9 Q/ T; c, W- L#   Else the binary logs would automatically increase by n times every day.
# X4 m/ d7 x% h2 Y' t1 q" [- W! M# - The error log is obsolete, messages go to syslog now.
/ q3 U, i) O, D7 Y/var/log/mysql.log /var/log/mysql/mysql.log /var/log/mysql/mysql-slow.log {
6 ]2 [* q& k3 y0 m9 [) i$ E        daily/ f# h( X7 E" a
        rotate 7
2 ^2 J, w$ [  Z: X        missingok, W$ H' f' j. O2 A, n1 ]
        create 640 mysql adm
& ^( U1 q4 ^) ~7 p: M9 s, E        compress
7 [" Q, F. L) t7 h1 E2 a  C        sharedscripts
) O6 v. D, ^3 G* l. m: n3 c        postrotate5 i+ I. D" l3 j1 o5 h% h
                test -x /usr/bin/mysqladmin || exit 0
& {! s6 `6 X( l$ b                # If this fails, check debian.conf!
& O! i! ]$ W3 ?* c                MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
' j6 x4 v8 y1 A6 I5 \& Z- _8 D; {                if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then. M2 m* {1 C: o! v1 u% W" L
                  # Really no mysqld or rather a missing debian-sys-maint user?6 S  Q7 ^+ i# u: ^( T- p
                  # If this occurs and is not a error please report a bug./ K& y3 M4 j4 J# z+ B& ?
                  #if ps cax | grep -q mysqld; then5 t, g, S% @4 j8 m
                  if killall -q -s0 -umysql mysqld; then
. ?9 @3 |- _4 V% \& `+ @                    exit 12 a8 d$ a; {- \; C) @9 F  u
                  fi
8 t' V- o4 o9 m+ X: C8 F4 O                else
: h1 T  G1 m! P& I6 {                  $MYADMIN flush-logs7 m$ H- ~/ g  j% A) z2 W' K' y
                fi
2 p& u" M1 w& C3 b! I* v        endscript2 H- l: t2 }( R9 e
}
3 ^4 ~; N9 f5 D
  C- r' s$ M) i- M) Y8 d
! l3 _/ ]- j0 F2 f# A' q' y+ d3 m- r* M; ]1 R/ D& p( l
检验服务器配置+ j4 ]' |9 |/ ~# \2 @' F
5 c5 U+ e  C. P
使用 show variables like '%log%'; 来检查服务器和日志文件相关的变量:
) ~$ f8 p# q6 I, `  [% |! Xroot@cosmos ~ # mysql -uroot -p
, t" I; ^1 t: _1 w9 ?9 ]& cEnter password:
0 Y8 L1 u. u/ p" \1 ~Welcome to the MySQL monitor. Commands end with ; or \g.- \: H% [( V. C! H0 Q
Your MySQL connection id is 1443329 D  z% V4 _) b8 O5 F
Server version: 5.5.31-0+wheezy1 (Debian)
2 t* Y8 Y6 X  D' Y
4 W- R4 q) s2 c) c1 A4 D3 S6 ICopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.( F" H5 S8 F  d4 K5 w& l" ^; n2 L
, H3 H+ G* R2 l% z  \( |. V
Oracle is a registered trademark of Oracle Corporation and/or its2 Z$ [9 \! z( l0 M! ?, g
affiliates. Other names may be trademarks of their respective
# y& b) p3 V1 N. eowners.' [4 @2 I' W3 y" o; `) j* N) f, k

3 h" b  s/ O* OType 'help;' or '\h' for help. Type '\c' to clear the current input statement.
/ T8 {, m  [. N, r" _+ Y) D- R" }: _& n5 s8 ]) t
mysql> show variables like '%log%';
; F, a6 ^% T) u+-----------------------------------------+--------------------------------+
- m- T0 R, T4 X4 F| Variable_name | Value |! J9 |( l! O' }6 Z, y$ m
+-----------------------------------------+--------------------------------+0 R6 H8 S" d/ I$ s& L, r
| back_log | 50 |
8 U( |" w) _+ m, E! P9 u3 J. N| binlog_cache_size | 32768 |
: n6 u  |& S! H( S| binlog_direct_non_transactional_updates | OFF |; v/ m9 `4 ]6 T) C8 U2 i  W
| binlog_format | STATEMENT |, S8 N, `; Y( q+ ^/ L2 v, a, A
| binlog_stmt_cache_size | 32768 |8 t. y, E, |+ m9 m4 H
| expire_logs_days | 10 |
$ Q; b  x* ~8 {% D; u/ R| general_log | OFF |: G% W7 a/ C1 v- h0 T5 S
| general_log_file | /var/lib/mysql/cosmos.log |; _" u( A# ]$ S+ U  \3 _. E
| innodb_flush_log_at_trx_commit | 1 |
  @: {; j9 v6 M| innodb_locks_unsafe_for_binlog | OFF |6 x( z+ d# b% k" g
| innodb_log_buffer_size | 8388608 |
7 L+ ?2 p1 `' \  E* b+ e| innodb_log_file_size | 5242880 |4 y- `2 X. f' a: ]. ~
| innodb_log_files_in_group | 2 |2 \" _0 D1 F( M* E
| innodb_log_group_home_dir | ./ |
& r3 h1 A" x+ H" f. y| innodb_mirrored_log_groups | 1 |
0 y6 I+ @8 h% D% S| log | OFF |/ Y2 P  t8 T& A. `$ B4 r
| log_bin | OFF |/ g) K7 i& }6 F* Q4 j
| log_bin_trust_function_creators | OFF |
$ Y5 ^( `! f9 F5 ~| log_error | |
2 k* }' |# k3 l| log_output | FILE |3 d3 e5 t% o2 c8 t" F, T4 ^
| log_queries_not_using_indexes | OFF |
  c+ e$ ~* @! }# K| log_slave_updates | OFF |- ?5 D3 @8 C3 b0 x+ e& Y& s
| log_slow_queries | OFF |
9 y- u9 y# H7 M! a3 z# c| log_warnings | 1 |
% n+ B5 O! e% p* ~" a| max_binlog_cache_size | 18446744073709547520 |3 D  h- S5 R5 X- x+ h
| max_binlog_size | 104857600 |
6 ~  G' L& n5 {! m- z, |8 g| max_binlog_stmt_cache_size | 18446744073709547520 |; ]7 V* A" A; G; a" D9 J0 V
| max_relay_log_size | 0 |# p" J3 T* V2 |. C  y  b, h5 }
| relay_log | |
1 z$ w2 Z3 i- _4 ~| relay_log_index | |- ]% g! l( V, i' Z2 W5 X: J( z* r
| relay_log_info_file | relay-log.info |
7 C' @0 h" S) V: a3 s5 W5 `! n| relay_log_purge | ON |* W; b/ c( M8 L3 k. l+ o
| relay_log_recovery | OFF |
. m# q$ q; f* s% y| relay_log_space_limit | 0 |
& S* A2 x. i; A2 ?5 D9 `: g| slow_query_log | OFF |5 Z. Y! ~$ K$ v8 e0 h  J
| slow_query_log_file | /var/lib/mysql/cosmos-slow.log |2 S) [8 Z; o  ^5 V% q: Q7 n
| sql_log_bin | ON |/ j/ d- w1 i, r; C! P
| sql_log_off | OFF |
* _; N9 H% d- c| sync_binlog | 0 |5 U0 y& a+ z9 u; Q: ?3 f3 O
| sync_relay_log | 0 |
" ^% d( ~, b* O( c9 j/ A4 N| sync_relay_log_info | 0 |
& b. H2 d" c* g8 ?" _- H5 [2 m7 H+-----------------------------------------+--------------------------------+
; s) S0 m4 o! u# z41 rows in set (0.00 sec)
3 P: w% M. e2 H: n服务器变量相关官方文档参考 https://dev.mysql.com/doc/refman/5.7/en/server-options.html
, R9 W# A: Z- Q, v7 {; J! y9 I, T4 k- s; A
5 j) m+ T1 S4 i1 {' G7 ^  G$ D
2 b- {( r4 d  l0 t- E
何时启用日志# D6 q9 `8 @7 g% P8 t/ c' _8 a# D

' ^0 z2 |8 _+ V$ x, yMySql 默认安装的话,所有的日志文件都不会被启用的(除了 Windows 平台上的 error 日志)。Debian 上安装默认将 error 日志发给 syslog。
% C" I/ a% j2 ~0 ?4 ?实际上,在很多情况下日志文件都可以提供关键问题的解决办法:
8 S' u5 X3 @: u2 V5 i' U8 |9 `8 m总是启用 error 日志在这些情况下开启普通查询日志(最好在运行时):检查你的应用是否正确处理了 MySql 数据库连接(一个常见的错误就是从一个单一脚本多次连接到 MySql);监控来自你的应用的查询的执行情况;测试 memcached(或者类似的软件),检查某查询是被 db 执行还是被 memcached 处理当你的应用由于某些原因造成性能下降而你想找到这些慢查询时,启用慢查询日志(MySql 最好是在短期内这样配置,比如 2-3 天)+ b; y% E9 h1 X$ O& y) D% d
( R7 l3 ]7 _+ O' [7 a7 m, r
2 g4 _# M# [4 K' x6 ^2 d
示例; Y/ k) m0 [8 d5 u  u2 K6 u

, k- R2 S0 D7 d) x1 i$ J) h以下是一个 MySql 普通日志的示例:0 I1 A" j5 ?" ^! t
131021 17:43:50 43 Connect root@localhost as anonymous on pnet_blog- \7 U6 X( J+ k
43 Init DB pnet_blog
- n. q6 W* O2 i6 f4 S" Z43 Query SELECT count(id) as total_posts FROM posts WHERE date_published is not null AND date_published <= '20131021144350'0 s, w4 m; i3 y+ m/ {1 ~6 g: D
43 Query SELECT * FROM posts WHERE date_published is not null AND date_published <= '20131021144350' ORDER BY date_published DESC LIMIT 0,10) k% m, g. a! `! p
44 Connect root@localhost as anonymous on pnet_blog% h2 C: S4 o5 d6 Z! y( a
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
/ P( _+ h  Q1 a' |! ^% Z44 Quit5 e7 w1 f1 d3 N* L* C
43 Quit- C$ ^) E; _% |; r
131021 17:44:28 45 Connect root@localhost as anonymous on pnet_blog
- V& b0 @8 z* m. h45 Init DB pnet_blog
2 v7 Q0 a' T& r! C45 Query SELECT * FROM posts WHERE url='how-and-when-to-enable-mysql-logs'
5 R# a: ^6 R9 c' I45 Query UPDATE posts SET impressions=impressions+1 WHERE id='41'
+ _. J2 L4 G. q' @9 l45 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
2 u+ o+ k0 R# O) v# j45 Query SELECT url, post_title FROM posts WHERE date_published IS NOT NULL AND date_published > '20131020150000' ORDER BY date_published ASC LIMIT 0,10 c# d: C; S0 o& i& y" W
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- N# j) G7 n# ]" n8 ?0 A9 v( M
46 Connect root@localhost as anonymous on pnet_blog
! g) C' S) a/ n( f( C; M% X46 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
4 ]7 O" z# |7 g1 V. ?+ n- `1 g46 Quit
+ i( c: u1 |% c  H' L! }, I! W45 Quit
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-12 03:00 , Processed in 0.019686 second(s), 25 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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