|
|
楼主 |
发表于 2017-9-27 16:20:58
|
显示全部楼层
通过 MySql 配置启用日志
+ Y$ I0 Q, L8 t- B. u r) y- @( ^& y# c: S- ^7 s
日志相关参数位于 [mysqld] 部分。
" x' {1 Z# o3 w0 Z编辑 MySql 配置文件:
9 p! a' N4 I" A* ~* x+ C7 Tnano /etc/mysql/my.cnf6 ]( P2 c5 g% a6 I" V6 \) @: l
以上是 Debian 下的默认安装目录,其他 Linux 发布版可能不太一样,这个文件中 MySql 服务器的参数如下:2 F/ \! m8 V" B. O( U7 a E5 N
# * Logging and Replication6 r+ i8 |3 F" D, Y; I) }* B
#
1 a |* t' R% U' |! C) S5 J+ u# Both location gets rotated by the cronjob.2 g" ]) y$ `) |; }, Q; _ g8 `1 k
# Be aware that this log type is a performance killer." e$ B2 q# M! g, g. i
# As of 5.1 you can enable the log at runtime!
* B" C: c9 b0 a8 p7 \#general_log_file = /var/log/mysql/mysql.log
8 n( e" X/ a, |; Z* g#general_log = 1
, x- ^- h O0 A) d$ p# B- ]7 i1 d; c* K
# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
8 Z+ }8 I3 T2 l#+ J& k9 Q9 n* O6 b" t6 S8 \
# Here you can see queries with especially long duration! P4 m* |% E! i- Y4 N
#log_slow_queries = /var/log/mysql/mysql-slow.log0 v0 Z i% _* z# {
#long_query_time = 2$ ?- w+ Y; B b+ {, j# y4 z Z
#log-queries-not-using-indexes/ J: h1 S6 h8 b: a
#
. Y/ ] s% G( h* c; I3 T; e( ]# The following can be used as easy to replay backup logs or for replication.
) A( z! _5 R6 Z4 g. R# note: if you are setting up a replication slave, see README.Debian about
$ P% z+ e# O/ s) H# other settings you may need to change.
; m. M" j5 ` ^) D" M+ S$ z9 Q#server-id = 16 M$ U; _% C, v
#log_bin = /var/log/mysql/mysql-bin.log
, s4 d0 { n: i4 U4 o1 a nexpire_logs_days = 10) W0 r$ n- k& U1 I8 \1 g
max_binlog_size = 100M
8 _7 i% \, H+ B# t6 S" Z#binlog_do_db = include_database_name2 Z: e9 }; y# Y- ?6 r( v' Z$ k+ `
#binlog_ignore_db = include_database_name
5 U6 d8 R+ f3 c. AMySql 安装默认是不启用所有日志文件的(Windows 平台下的 error 日志除外)。Debian 安装 MySql 默认是将 error 日志发送给 syslog。
) |( K% c' U" ?. L* r+ merror 日志9 B" O" t+ R" {6 R/ q1 P9 B8 f: [
: {9 q9 Z1 n) h( @7 c9 ], t' i
根据 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 配置,error 日志推送给 syslog:
4 r, B3 g2 s+ F$ x* J3 t[mysqld_safe], K3 W9 u1 o) g: O5 `, c# O+ b
syslog
7 M& U9 a$ ^) Q7 |$ R* X) W' _8 ?这是推荐的做法。如果你由于某种原因,不想讲 error 日志推给 syslog,将 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 文件中的上述行注掉,或者直接删除掉这个文件,然后在 /etc/mysql/my.cnf 中添加以下行:! |: V: U% T, {& I$ g
[mysqld_safe]' j: R( ?3 E& [/ \& J1 W# B6 l
log_error=/var/log/mysql/mysql_error.log
9 [# h8 ^% I/ {* _0 R9 P8 |5 g$ u, H1 F4 j( Z, G) J8 j5 S0 ^9 Z& z
[mysqld]3 I0 H4 S4 n9 u7 R" x( k+ A
log_error=/var/log/mysql/mysql_error.log% v3 c5 ^( L0 D$ f( _& c
一般查询日志
$ u6 n4 A7 y! t: k" z5 r% `5 I5 ? Z9 ?
要启用一般查询日志,将相关行取消注释(或者添加)即可:
! I* K t$ C H- h" U, c7 t% wgeneral_log_file = /var/log/mysql/mysql.log
' e: U6 [( [: f0 Jgeneral_log = 1, c3 B$ Z) p9 ^# \; h) w: i
慢查询日志
! B5 W9 s0 R. x) i% E2 F J2 f
4 z+ R" A$ t2 k% M* P要启用慢查询日志,将相关行取消注释(或者添加)即可:; \* m. D) s& T0 |
log_slow_queries = /var/log/mysql/mysql-slow.log v% G6 i& C3 i; G, \, F& c
long_query_time = 2
8 P; m- ]+ h2 X, nlog-queries-not-using-indexes
( ]+ o2 m0 N$ a+ }! R1 O% _3 s$ ~配置修改后重启 MySql 服务器
3 @* f: ~+ O! U5 ?0 ^* \! B4 m- A1 i6 Q/ y( H
以上方法要求服务重启才能生效:3 t \0 O4 K6 h% O/ W: m: U
service mysql restart
9 A! l* @9 Z# J" x或者使用 systemd:
) G6 @6 x, K7 g r8 C, Esystemctl restart mysql.service
9 c* G6 t0 O$ \# \% E) D) i! w9 h z0 q2 p, K& Q _! f
" t0 A( \) i* s7 k' t1 b) O
8 p1 p* H9 ~9 A0 `% |5 Q运行时启用日志/ j8 P$ I8 T" e) H& q3 p2 B& l7 C
7 k% u( C: D6 g9 ^; d4 `
MySql 5.1 之后我们可以在运行时启用或者禁用日志。. o9 h* g' X+ h! q5 H# J' ~8 L* e* M
运行时启用日志,登录 MySql 客户端(mysql -u root -p)然后执行:
9 |% b I2 x1 d6 u4 `; s! }SET GLOBAL general_log = 'ON';3 \- f8 o. R1 R3 N) \
SET GLOBAL slow_query_log = 'ON';
3 W( T1 Q- i5 X运行时禁用日志,登录 Mysql 客户端(mysql -u root -p)后执行:
. }: a0 v+ H( N: s+ K8 I6 SSET GLOBAL general_log = 'OFF';) F, L6 Q; h4 U1 s
SET GLOBAL slow_query_log = 'OFF';) a3 x9 n, v9 Y, b( g5 l; t" P
这种方式适用于所有平台并且不需要重启服务。' g G' L" p4 v" g9 [
1 @1 e4 x& P% u2 K9 ?
1 M ]+ _( I- h% N
! J C# Q/ Y0 R1 P g. W. z显示日志结果
8 Y ~" S$ v3 @( Z# Q1 x/ ^& v6 O# O$ c( G$ E( E
error 日志9 n/ w6 D5 n/ E$ S7 A' S1 k' t+ k
) F+ u" Q+ M- ?9 X( Q按以上办法设置以后,你可以通过以下命令显示 error 日志:7 E# x6 g8 a$ |+ O
tail -f /var/log/syslog
L9 x( ]: @0 B' c备注:如果你没有配置 error 日志文件,MySql 将把 error 日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}.err 的文件中。 q8 F4 o3 L9 K6 `5 C
普通查询日志
5 B0 `+ q- V; C& U' `/ `8 q& u. @# H4 n& n" Q
按以上办法设置以后,你可以通过使用以下命令来显示普通日志:
( S' K1 c5 ~* N& J) q. K0 Gtail -f /var/log/mysql/mysql.log7 \* m9 O- r! P% ^6 ~7 c
备注:如果你没有配置普通日志文件,MySql 将把普通日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}.log 的文件中。
1 O6 x: h& ?2 X: q. D: h& x" w, T慢查询日志
+ Z4 h- z* l K2 @0 B. A
" s/ w0 _* T. A( U. z按以上办法设置以后,你可以通过使用以下命令来显示慢查询日志:
; ^( W$ f/ y- h& ~tail -f /var/log/mysql/mysql-slow.log
! u: ~+ i4 G V( }8 [5 r备注:如果你没有配置慢查询日志文件,MySql 将把普通日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}-slow.log 的文件中。
$ J/ F- `' I! O$ n3 {7 D& w% t/ _2 M+ ^& d5 V% p2 x) \6 \1 r) L. l& p
# w: w* L V# v9 B5 Y: d
- y3 Y; ]# n" O& Q循环日志
/ \( ~3 b+ d! w, L% d
& ?; T2 @2 D9 s" |0 L, W别忘了滚动日志,否则的话日志文件可能会变得很庞大。4 Q% U8 R/ m: i
在 Debian(以及 Debian 派生系列诸如 Ubuntu 等)系统,MySql 初始安装之后,循环日志就已经使用了 logrotate:
( X8 r" T B8 R1 B0 `9 vnano /etc/logrotate.d/mysql-server
$ n7 O8 ?0 p. {7 X: a2 G N3 Y对于其他 Linux 发行版,可能需要做一些改动:
M5 _; M& v# Q; S5 q# - I put everything in one block and added sharedscripts, so that mysql gets
D: ?: s2 o8 \5 r! g3 Y# flush-logs'd only once.
4 h- f3 R/ d1 V* W( {# Else the binary logs would automatically increase by n times every day.% T: d& S9 R1 f4 d M, u
# - The error log is obsolete, messages go to syslog now.% S5 D: t. W- h/ m
/var/log/mysql.log /var/log/mysql/mysql.log /var/log/mysql/mysql-slow.log {
! j. Z1 s3 u5 c& f% F0 }$ S5 o3 X daily3 w- ~1 b% X( I- s- W3 ~
rotate 7
! c) \% a: U o' l- g% D% j' l missingok# t# _: I4 `5 _$ v! p% o
create 640 mysql adm
+ z# z, ]4 V( m, A! g compress! r% \( S* {8 H
sharedscripts
8 H2 s( h8 E% M' |, Q! C" o postrotate( c' f# G8 ?0 |1 u& K
test -x /usr/bin/mysqladmin || exit 0! p2 ^% g l; f4 S6 t- a
# If this fails, check debian.conf!! s2 h& o1 n! t& \
MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"; K s0 x/ O6 F# Y/ U, p) i/ F
if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then
' x: g$ ?5 y0 B* s- V; K8 \ # Really no mysqld or rather a missing debian-sys-maint user?4 i* `& `2 j' W6 ?1 w
# If this occurs and is not a error please report a bug." f4 V$ N4 v2 H* I
#if ps cax | grep -q mysqld; then# E2 P; T9 P% L% p9 [. I; n4 C
if killall -q -s0 -umysql mysqld; then
3 u0 A6 a; x% k- [, j exit 1" U4 e3 q+ I' m% K' m
fi/ h* b0 {6 i- c: L
else
; z) L' H% v, z* } ^$ D9 ] $MYADMIN flush-logs& Q+ U% Y& ]/ _2 W
fi9 |( Q C: q7 \+ D
endscript
) q* }7 J& Z6 ?}
- n! ?' @2 [3 O2 k% \7 h' J, B! v
. Y4 a2 o7 x: O* D2 u# `4 H( }: ]! z% z' f" i2 u
检验服务器配置4 U+ @9 p3 A" p( l" r1 N w
2 Q V! ^8 q$ ]5 a6 h使用 show variables like '%log%'; 来检查服务器和日志文件相关的变量:; I' `! R D- a2 u- ~
root@cosmos ~ # mysql -uroot -p
$ b q7 O; D5 ^; B* t: ^5 }Enter password:, J# t$ x+ Y e0 |/ c0 Q4 W
Welcome to the MySQL monitor. Commands end with ; or \g.8 K5 x$ ?9 w9 W
Your MySQL connection id is 144332, R9 o( k7 Y, i8 ?
Server version: 5.5.31-0+wheezy1 (Debian)
. p/ O+ C d- S3 F6 R' U R$ m2 b* l5 ~' F
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.5 w% c% E$ {& i# `9 N5 W
; E3 b3 ]4 |% H6 U
Oracle is a registered trademark of Oracle Corporation and/or its
$ _- V& y9 q! L: saffiliates. Other names may be trademarks of their respective$ j: s' G4 B* o
owners./ p' H$ h1 e0 X
( Q8 X5 V$ w( \( t8 U
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.; I0 ^2 D) i4 ~. x8 k4 _, F2 R: c
) c1 Z" }/ h5 O' X3 wmysql> show variables like '%log%';, R$ x! e! B. j; n1 ^! a- i+ b' X" c
+-----------------------------------------+--------------------------------+
1 Q' d. R, O7 K( M| Variable_name | Value |! F* D3 X6 o0 ?, t# e/ h5 D N" v
+-----------------------------------------+--------------------------------+; e, [. S6 j8 b
| back_log | 50 |! n+ y3 P) E0 a Y G1 k, V
| binlog_cache_size | 32768 |, A9 f) z1 H D
| binlog_direct_non_transactional_updates | OFF |
7 b8 d1 p. l b1 F| binlog_format | STATEMENT |
7 A/ B& I9 o5 e4 @ G- k| binlog_stmt_cache_size | 32768 |
* |. k) q" U) o. z+ x# N4 \| expire_logs_days | 10 |
0 \) D: _9 h; n1 s1 @6 t| general_log | OFF |
" j+ m, h# }8 h% E| general_log_file | /var/lib/mysql/cosmos.log |( A( n& j8 ~5 A+ M+ E3 a) ^
| innodb_flush_log_at_trx_commit | 1 |6 O0 V: W! ~3 X3 W6 O( h( r0 r
| innodb_locks_unsafe_for_binlog | OFF |% R, G6 ~2 r) l/ l% e' u
| innodb_log_buffer_size | 8388608 |
7 ~$ C' P. w5 X2 [0 \9 v" t| innodb_log_file_size | 5242880 |
5 T% G! {' v! E+ X- H- S| innodb_log_files_in_group | 2 |
! q7 l& x8 p) Q6 d$ F7 y9 z7 J| innodb_log_group_home_dir | ./ |
9 _" B6 e9 C" a6 ]: S7 ~; H| innodb_mirrored_log_groups | 1 |
$ y0 \3 P* `' K/ Q+ @| log | OFF |+ ]* q: g3 i* ]0 p+ i) Y
| log_bin | OFF |3 s( o4 ^7 r9 R/ i1 v. r
| log_bin_trust_function_creators | OFF |$ H" [2 }2 Y" {. v& ?& W
| log_error | |
# q. ^( F0 C3 J+ |4 a| log_output | FILE |& T4 _% r. z8 L) V4 _4 D9 q8 m
| log_queries_not_using_indexes | OFF |. A. B7 a7 ^* ]- t' |4 w% B
| log_slave_updates | OFF |
0 @( i% [' w: z* W! G| log_slow_queries | OFF |
$ T( A8 K g+ b& s- R) O; I5 p; o| log_warnings | 1 |7 d/ P; n6 R# _
| max_binlog_cache_size | 18446744073709547520 |
5 L( ?: \. n: t% d a2 G# \| max_binlog_size | 104857600 |
/ K4 R5 s: ~/ v" G) E! || max_binlog_stmt_cache_size | 18446744073709547520 |/ O. U: K: }- h; n0 ?. {5 k
| max_relay_log_size | 0 |( Q" g; e4 |& K: S. B
| relay_log | |4 M: n! a8 g$ C! S6 d& l
| relay_log_index | |
5 K" q, k% L1 q) {| relay_log_info_file | relay-log.info |
; p& [; R* o8 L, Z$ r| relay_log_purge | ON |/ r6 X+ p/ X: |: T }
| relay_log_recovery | OFF |$ N( W1 e( z2 P" R
| relay_log_space_limit | 0 |
" D8 D: s* j [7 h* K7 o| slow_query_log | OFF |) P6 u7 y d3 w% }; B0 o
| slow_query_log_file | /var/lib/mysql/cosmos-slow.log |9 o6 K1 ?. c* ^% h
| sql_log_bin | ON |
$ x' ^- A1 Y; T4 _ m8 L. s' }$ X) F| sql_log_off | OFF |
6 y% O! f8 b6 R- k, Y| sync_binlog | 0 |3 a* a+ k/ w9 s4 |
| sync_relay_log | 0 |) w3 h4 N3 G. i
| sync_relay_log_info | 0 |
U8 F' U1 C9 z: M+-----------------------------------------+--------------------------------+
7 E( s4 x8 I( o* d" y41 rows in set (0.00 sec)% f9 U: T8 h7 a5 }1 e
服务器变量相关官方文档参考 https://dev.mysql.com/doc/refman/5.7/en/server-options.html。8 W% I$ Q- g8 Y2 Y/ g/ s
" Y( r9 ~# y& g! l. B9 U 1 M" j' x* r: a$ Q5 x! e. _
. @3 E6 ~6 a: ^+ q
何时启用日志1 a$ L8 t7 |2 l P8 ^( C
0 K5 ]$ q/ g( V5 S% R6 k! M+ H
MySql 默认安装的话,所有的日志文件都不会被启用的(除了 Windows 平台上的 error 日志)。Debian 上安装默认将 error 日志发给 syslog。
0 z3 K& U) p5 G0 T9 T实际上,在很多情况下日志文件都可以提供关键问题的解决办法:: |% Q) K: ^% b" C* x# O
总是启用 error 日志在这些情况下开启普通查询日志(最好在运行时):检查你的应用是否正确处理了 MySql 数据库连接(一个常见的错误就是从一个单一脚本多次连接到 MySql);监控来自你的应用的查询的执行情况;测试 memcached(或者类似的软件),检查某查询是被 db 执行还是被 memcached 处理当你的应用由于某些原因造成性能下降而你想找到这些慢查询时,启用慢查询日志(MySql 最好是在短期内这样配置,比如 2-3 天)' G0 g% F' p ]: a" ]$ T
( z2 p3 _2 \: R5 m+ d5 |
4 C. ?( q4 C7 A! |$ z示例( \8 j9 i$ D0 L2 k Z1 Y; _7 ?
4 }9 q. V) j; [, I/ i# E6 c
以下是一个 MySql 普通日志的示例:
4 e/ C9 |' w, D' k/ [131021 17:43:50 43 Connect root@localhost as anonymous on pnet_blog4 X l3 z( F7 X# } L. P: F' K
43 Init DB pnet_blog
9 p% F6 T6 N2 k; z! U43 Query SELECT count(id) as total_posts FROM posts WHERE date_published is not null AND date_published <= '20131021144350'
; g. d7 h2 [( R. a43 Query SELECT * FROM posts WHERE date_published is not null AND date_published <= '20131021144350' ORDER BY date_published DESC LIMIT 0,10
/ i/ c$ s8 l0 T7 E" R44 Connect root@localhost as anonymous on pnet_blog% I9 G) `" N4 U& D$ e2 j# c
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, 100 V2 d8 d* m [2 o5 x |( U! ]
44 Quit% x) ?1 V5 y) }2 j0 d4 n
43 Quit* a, |% R! C0 b# G# n! Z
131021 17:44:28 45 Connect root@localhost as anonymous on pnet_blog3 d8 H; N& K8 k
45 Init DB pnet_blog
, g5 [" @# i4 G. K45 Query SELECT * FROM posts WHERE url='how-and-when-to-enable-mysql-logs'
' [: w. g& r# O* m6 S45 Query UPDATE posts SET impressions=impressions+1 WHERE id='41'
7 Z1 v, ~" ^( T1 S45 Query SELECT url, post_title FROM posts WHERE date_published IS NOT NULL AND date_published < '20131020150000' ORDER BY date_published DESC LIMIT 0,10 S# b' z. x' Z) x
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+ t, _' l0 R, U8 ]% R+ h6 }
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,100 S9 U) O2 y3 a
46 Connect root@localhost as anonymous on pnet_blog# M8 d$ ?; z! w6 R+ h* |4 v
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 z. z O0 N/ R9 O3 i
46 Quit3 u3 d; j# H0 ^- A
45 Quit |
|