- 积分
- 16843
在线时间 小时
最后登录1970-1-1
|

楼主 |
发表于 2017-9-27 16:20:58
|
显示全部楼层
通过 MySql 配置启用日志( `/ b# j0 {; F
. r: {' [+ a8 c日志相关参数位于 [mysqld] 部分。
m- w4 v* _ {; m; e# g: p编辑 MySql 配置文件:
9 B O7 E( B/ G( @( P! enano /etc/mysql/my.cnf4 F( n' Z3 N5 T( l) C
以上是 Debian 下的默认安装目录,其他 Linux 发布版可能不太一样,这个文件中 MySql 服务器的参数如下:
9 m9 a4 g" x }6 O# * Logging and Replication
$ d% w) H1 T) ~5 ?0 \#+ }1 \- E" a; C8 \: P
# Both location gets rotated by the cronjob.
4 l+ r4 ^$ R7 F! g4 u) p7 p& I# Be aware that this log type is a performance killer.0 Z- X, _2 y' l* @# J5 s
# As of 5.1 you can enable the log at runtime!5 y. `* d: Z/ l9 D. q% I
#general_log_file = /var/log/mysql/mysql.log3 W1 f- x4 A& X
#general_log = 1
& P4 [! c" d; ~. ^9 k5 q#+ B3 \* B( c; H$ y! x
# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
- I0 B a/ I* c P#
1 Z4 N! O4 r/ d8 @2 f* w# Here you can see queries with especially long duration
! S2 T# o) F1 b: E4 }3 A#log_slow_queries = /var/log/mysql/mysql-slow.log
5 p1 p$ I( p- j- d! A! a! ^#long_query_time = 2
4 g$ Y* X& f4 v: \0 X% B6 x3 h& `#log-queries-not-using-indexes' x, u. E- H' B5 d: [8 B
#
4 r; x7 D* y. O/ E+ M# The following can be used as easy to replay backup logs or for replication., Q0 L7 w) ]2 {& y+ I
# note: if you are setting up a replication slave, see README.Debian about& v9 t6 ^# e9 j
# other settings you may need to change.9 u2 _' J! x7 K7 R
#server-id = 1
1 q* z. ?3 S+ y8 `# E2 @6 |' J' l#log_bin = /var/log/mysql/mysql-bin.log( N# f, q9 G. k5 P+ P7 v$ M ^
expire_logs_days = 10
3 i: ~8 _3 F: m9 Jmax_binlog_size = 100M( X2 z _$ R/ f8 Q9 `; c( P
#binlog_do_db = include_database_name! l. h3 \$ _: J- @0 C! @
#binlog_ignore_db = include_database_name
9 p& y. e9 y: a/ UMySql 安装默认是不启用所有日志文件的(Windows 平台下的 error 日志除外)。Debian 安装 MySql 默认是将 error 日志发送给 syslog。
$ i2 ]6 @/ }; m, Z' ^' Q6 p6 Derror 日志
" {7 |% i1 C" U6 S3 E: T7 ~' ^
; P! ?) k6 d2 @6 }根据 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 配置,error 日志推送给 syslog:
/ h* r0 n. [: x+ s4 x: s3 r/ E[mysqld_safe]
9 T2 U& H% A% c* k3 ]: `* isyslog
9 K7 V& l9 ?' G1 F! h: f这是推荐的做法。如果你由于某种原因,不想讲 error 日志推给 syslog,将 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 文件中的上述行注掉,或者直接删除掉这个文件,然后在 /etc/mysql/my.cnf 中添加以下行: D# F9 \) x, E
[mysqld_safe]
% ]6 Z h" _1 N" j, c( z D' y3 zlog_error=/var/log/mysql/mysql_error.log: V$ z! _% P! o& r
; [( P% X1 J" a. c# s5 Q[mysqld]* U3 H& U% n: J
log_error=/var/log/mysql/mysql_error.log; M. y$ H) l% D' s
一般查询日志( U& D/ h2 M+ O+ v- C% i
. V5 \$ l( }4 X; P要启用一般查询日志,将相关行取消注释(或者添加)即可:5 [/ S+ e( L# @7 s6 J4 O$ ~) z
general_log_file = /var/log/mysql/mysql.log
9 ]* C3 L5 P, p# P, r8 o- W8 \general_log = 13 i2 Q* t0 P9 t# D. ^
慢查询日志
) A3 B7 H/ N: C1 t0 t3 e# O \6 X9 d2 |
要启用慢查询日志,将相关行取消注释(或者添加)即可:) |$ f4 U5 ]3 |: @
log_slow_queries = /var/log/mysql/mysql-slow.log
" n' m P/ f7 C% ilong_query_time = 2
, a& Z1 }* A' ?& o' `2 t: Rlog-queries-not-using-indexes
' s2 V: S# g9 i$ B# z9 ^配置修改后重启 MySql 服务器- F! s6 {* S3 U/ ~
/ n6 G) {1 `/ ?- G
以上方法要求服务重启才能生效:% a6 B: @! S9 o1 G+ b3 J
service mysql restart- B x. g. n p* R
或者使用 systemd:, `9 L. y. h. O& u, U6 ^* g9 ]
systemctl restart mysql.service
$ b( ?, N) b5 S+ w
5 x: l* o* T6 w/ f! ]7 e
: l: |0 Q$ Z, s! b; o# W: s2 _5 ]3 z
运行时启用日志! p4 G1 U: P7 A6 M
4 ~8 f W: i; U$ k
MySql 5.1 之后我们可以在运行时启用或者禁用日志。
" D$ \: W4 n6 h5 N6 \# F4 O( Q运行时启用日志,登录 MySql 客户端(mysql -u root -p)然后执行:
7 N3 S5 J4 f2 o0 y7 |SET GLOBAL general_log = 'ON';7 T% a: x1 d4 B: \
SET GLOBAL slow_query_log = 'ON';
/ k9 A; { ~ G$ S% \! d7 o# f Z运行时禁用日志,登录 Mysql 客户端(mysql -u root -p)后执行:) L& Q, V. b) Y
SET GLOBAL general_log = 'OFF';2 b/ P5 \# g. t% ?' E
SET GLOBAL slow_query_log = 'OFF';
. A' t' |5 e1 h. I( B+ B2 i这种方式适用于所有平台并且不需要重启服务。% T A$ a# A8 u
/ m4 p4 b$ I7 Z3 ^! x* U% c7 @
, o h2 X$ [# e m! V: f: `# U; D) D% q+ S# r' K
显示日志结果
4 ?! ^$ |; R" J5 O4 m6 @& v& G) P! O; @- k# i7 ]
error 日志
|2 [6 }/ k. j9 v2 j" ^( Q/ A" ?
按以上办法设置以后,你可以通过以下命令显示 error 日志:' s. W* {3 ^* R
tail -f /var/log/syslog3 N" t$ u$ Q& w! a6 I- f' }: ]) r3 g& V
备注:如果你没有配置 error 日志文件,MySql 将把 error 日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}.err 的文件中。4 A6 `1 ^4 p1 O' P
普通查询日志
1 j' t3 X# |+ J V) o ^1 R7 d3 l5 Y9 \" s K' R
按以上办法设置以后,你可以通过使用以下命令来显示普通日志:
0 v* y+ ?* k2 utail -f /var/log/mysql/mysql.log
. n) b! H! |1 f3 |7 y: u备注:如果你没有配置普通日志文件,MySql 将把普通日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}.log 的文件中。
* x" G- S+ l- z' l* f! E: ?慢查询日志 i( A7 V% ^& F% f8 n
& @4 ~9 Q5 ~6 e. f9 O
按以上办法设置以后,你可以通过使用以下命令来显示慢查询日志:7 F+ [, ^% J) c* l5 x5 T
tail -f /var/log/mysql/mysql-slow.log
9 f0 F8 |0 o+ I" b; I7 F# @备注:如果你没有配置慢查询日志文件,MySql 将把普通日志保存在数据目录(通常是 /var/lib/mysql)下的一个名为 {host_name}-slow.log 的文件中。7 t$ L* O1 Q0 e1 u2 f* W
7 b+ H {7 W4 F0 h: x
2 W) J- ^- l' _4 |/ O
! R% v& M* N9 T, d1 ~循环日志& x* z7 m& P4 ~) K) _8 o
' B0 X0 Z8 }$ s# [4 E& a! L# n
别忘了滚动日志,否则的话日志文件可能会变得很庞大。
. n$ i2 x- z* ^% Y/ ?( l4 a在 Debian(以及 Debian 派生系列诸如 Ubuntu 等)系统,MySql 初始安装之后,循环日志就已经使用了 logrotate:3 |3 A( i) g V1 C' \. N
nano /etc/logrotate.d/mysql-server
* z* x0 Y0 x1 {6 O" E* W) @对于其他 Linux 发行版,可能需要做一些改动:
% N, b* v; d2 L+ G# - I put everything in one block and added sharedscripts, so that mysql gets8 \; S0 I, L: \. O" }( c
# flush-logs'd only once.8 F8 K" F! S/ {. ], o) [- `) Z
# Else the binary logs would automatically increase by n times every day.
$ u) b6 U; X( X( s) g+ d8 c6 d8 @3 h# - The error log is obsolete, messages go to syslog now.
& k0 t# S0 E4 ]# u4 e/var/log/mysql.log /var/log/mysql/mysql.log /var/log/mysql/mysql-slow.log {
, R/ z: f7 L# q daily5 ~ ]. h- L& C- _- U7 L
rotate 7
; N! [/ c8 R3 x. f+ v1 x- [ missingok
" @( o7 u$ g% A2 m create 640 mysql adm
/ E& v0 S2 o- s3 W5 I- k compress
; g/ t$ l5 _1 P. I sharedscripts4 s! x& p0 ^1 s, Z
postrotate
6 ^- a9 `* s1 p% Z ~ test -x /usr/bin/mysqladmin || exit 0" c2 T2 O! K4 K/ S) G
# If this fails, check debian.conf!
" S. R2 o( J. [' B0 E MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"- ~) i! P ~4 f# S/ s0 S
if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then
# Q. C. F" s+ M # Really no mysqld or rather a missing debian-sys-maint user?
( B; }6 o; C0 w" M( }6 B# T # If this occurs and is not a error please report a bug.
% c7 Q" Z! D& |" G #if ps cax | grep -q mysqld; then9 a+ A9 K5 L ` x. J- G/ \
if killall -q -s0 -umysql mysqld; then
- k* \2 y+ ~6 J% U$ a1 H. F exit 1( U: w* Y8 b# y9 T
fi- s# h+ a/ k; f' e/ J& R- s
else
, b9 d+ B; r8 l! {$ l% O $MYADMIN flush-logs
$ {/ Z; N+ t2 a3 [0 l! W fi
/ ` u( y- Q; t9 ?4 w# K endscript
8 |4 J4 g1 S/ p+ w) Y}
7 i# s5 Y4 H* o8 Z& D
4 c. W s. {2 u. z
+ u2 C: K: o) N; m# M' r! ~ I% m
5 B1 a, [: R* k% C/ ~/ l% d检验服务器配置
; Y3 T5 m# L! X9 T2 {8 `: C8 p8 C, e! Q) ~( X( e
使用 show variables like '%log%'; 来检查服务器和日志文件相关的变量:
w! u6 o. e3 p8 Wroot@cosmos ~ # mysql -uroot -p
( {0 B( s* ^2 u) d; o$ m9 QEnter password:1 A# t7 N4 B- v b' _
Welcome to the MySQL monitor. Commands end with ; or \g., X6 M; o' o3 Y, P5 H
Your MySQL connection id is 144332
* E! ]; W! f: P% ^. nServer version: 5.5.31-0+wheezy1 (Debian)7 }$ n4 c/ @& O9 C4 D7 T: g& V
. M6 f3 j8 `+ d& d
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
8 t* z) g9 ^! P) |, o/ i9 O# W. Z9 d! m. q& [
Oracle is a registered trademark of Oracle Corporation and/or its
/ L$ |/ ?( _. }: E" j, o) }+ A& ?affiliates. Other names may be trademarks of their respective; K4 M( H8 C0 z* x
owners.5 U# {' U" V3 z
" r) c. _. p' |6 n* QType 'help;' or '\h' for help. Type '\c' to clear the current input statement.% ` o) v: Z% W1 z( {
$ D, F4 e% ~' p* `/ v# i4 W
mysql> show variables like '%log%';
Z. W: [4 W0 ^- a5 m# m- H: G+-----------------------------------------+--------------------------------+
) J7 _6 h5 U- p1 k| Variable_name | Value |4 Q# w2 t) X% S- ^9 ]) o
+-----------------------------------------+--------------------------------+
8 b6 \6 [; g# f; M# f0 W8 r| back_log | 50 |
# P4 X5 X( S2 \# g" \| binlog_cache_size | 32768 |
% `; N4 \! J. C/ U6 r, l' z% V" ~9 u6 b| binlog_direct_non_transactional_updates | OFF |) x E& X- y8 ~! i
| binlog_format | STATEMENT |
, x: j! S, E5 c) }| binlog_stmt_cache_size | 32768 |) j/ M& x; k$ G8 `3 ^* K
| expire_logs_days | 10 |0 D* t1 y' ]+ P8 B* v* \3 s5 u
| general_log | OFF |1 X, p5 _! u- I% Q5 M
| general_log_file | /var/lib/mysql/cosmos.log |
l5 i( w5 [* j7 ~0 ]* m/ r| innodb_flush_log_at_trx_commit | 1 |
( c$ ^% A; z2 N| innodb_locks_unsafe_for_binlog | OFF |
/ J. |7 m v9 W3 [| innodb_log_buffer_size | 8388608 |
( c$ ~1 N& \% f7 z1 v| innodb_log_file_size | 5242880 |, f# j8 }: e( I) K
| innodb_log_files_in_group | 2 |, d& g+ O6 \/ N0 [9 [
| innodb_log_group_home_dir | ./ |
/ ~' Q) x+ @$ D( l- H| innodb_mirrored_log_groups | 1 |8 P" E* V0 l; X! Z( W$ g1 ]2 ~7 l
| log | OFF |: o% F6 D6 L* d" A$ k- M1 s- X
| log_bin | OFF |/ L. L0 q4 T( [
| log_bin_trust_function_creators | OFF |
* B4 W% @- a/ I/ q' g| log_error | |1 A' Q0 y5 r' P/ b9 H- I
| log_output | FILE |) X) X- H% a! D& R( Q
| log_queries_not_using_indexes | OFF |
2 m' X7 r" x% Y' W0 z| log_slave_updates | OFF |
; n$ D- Y1 y) M3 @| log_slow_queries | OFF |
+ G& _8 ?- D/ i| log_warnings | 1 | F2 B' E2 b9 k2 j) N3 m- _. r
| max_binlog_cache_size | 18446744073709547520 |1 L( j0 F* S: r) k1 ]
| max_binlog_size | 104857600 |1 x' l: E [, @' H1 V
| max_binlog_stmt_cache_size | 18446744073709547520 |8 ~# }. }5 W. Z k x+ ~4 _
| max_relay_log_size | 0 |
+ O# c( W, W0 X \/ U x$ D# `| relay_log | |
7 D& Z& v: c+ p- H& z| relay_log_index | |
8 h3 |0 B ^( d( F| relay_log_info_file | relay-log.info |6 I5 ?( Y* t2 Y. R. H
| relay_log_purge | ON |
X/ c% B6 G& B$ A| relay_log_recovery | OFF |5 P/ m1 r. G4 U3 Z
| relay_log_space_limit | 0 |
& \" z# E$ k: x) F p| slow_query_log | OFF |2 C6 b) O$ Q$ Y @0 g' T
| slow_query_log_file | /var/lib/mysql/cosmos-slow.log |
4 y6 T& u i1 T. h( I' g| sql_log_bin | ON |1 u6 u6 r4 L( @0 ?5 M. R L) z
| sql_log_off | OFF |
* V1 k9 P. R2 B& v' V4 ]1 u| sync_binlog | 0 |6 U5 K2 H- u( u- c, Y, F a' h
| sync_relay_log | 0 |
; f$ W" D, N$ G+ h# A& J( z4 ]| sync_relay_log_info | 0 |
6 B: l4 u6 A: b$ r+-----------------------------------------+--------------------------------+
n9 A0 X* ]' V) q! ~41 rows in set (0.00 sec)
) m8 m1 u. v# T5 k服务器变量相关官方文档参考 https://dev.mysql.com/doc/refman/5.7/en/server-options.html。
# |; V3 c/ T0 I% d
+ P2 D' m' n) I% a
, I r+ [( c3 G
! M' E0 |8 L: t+ H V* [何时启用日志
& {2 W1 T. m3 [" @6 C0 g; K, F* k" F8 U: m5 m
MySql 默认安装的话,所有的日志文件都不会被启用的(除了 Windows 平台上的 error 日志)。Debian 上安装默认将 error 日志发给 syslog。
: Y8 w# s2 {# l实际上,在很多情况下日志文件都可以提供关键问题的解决办法:
. ?$ F6 G& j+ |( ~- Z: ~8 _; n' l总是启用 error 日志在这些情况下开启普通查询日志(最好在运行时):检查你的应用是否正确处理了 MySql 数据库连接(一个常见的错误就是从一个单一脚本多次连接到 MySql);监控来自你的应用的查询的执行情况;测试 memcached(或者类似的软件),检查某查询是被 db 执行还是被 memcached 处理当你的应用由于某些原因造成性能下降而你想找到这些慢查询时,启用慢查询日志(MySql 最好是在短期内这样配置,比如 2-3 天)
; G6 l, C& p- C/ |
6 M* a* J6 p9 ]3 Y: H- T% T; l2 k$ `, O* X9 u
示例
! c/ ?: Q, g7 d C8 Z" V7 ~
$ p# Y4 K! I: O/ m以下是一个 MySql 普通日志的示例:
: z0 a% F( t# T1 }& {* a131021 17:43:50 43 Connect root@localhost as anonymous on pnet_blog: Y( @5 i# f3 B: F( E2 U3 O6 ?# l
43 Init DB pnet_blog
$ E) W$ E4 c* L% B- f8 \1 ~43 Query SELECT count(id) as total_posts FROM posts WHERE date_published is not null AND date_published <= '20131021144350'
; p- v/ S5 p# v. W- D9 X: u4 m43 Query SELECT * FROM posts WHERE date_published is not null AND date_published <= '20131021144350' ORDER BY date_published DESC LIMIT 0,103 S1 W8 K( N6 N! @" H" x' f. M; B# T# i
44 Connect root@localhost as anonymous on pnet_blog t& w" s$ p2 S/ [5 E; 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, 10' E* D! m+ q: h2 W) O2 b: E
44 Quit9 A( ^' `! ~# `& R9 I" [+ d
43 Quit, ~0 j: O5 V' f
131021 17:44:28 45 Connect root@localhost as anonymous on pnet_blog
, X" b, p. R7 f8 H7 N45 Init DB pnet_blog" N# N9 {2 u `1 ~' k3 ?
45 Query SELECT * FROM posts WHERE url='how-and-when-to-enable-mysql-logs'
) r' |' O6 W D) Z* t" M/ y45 Query UPDATE posts SET impressions=impressions+1 WHERE id='41'
' O; a! q/ E! W$ a! z! i; t45 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
* j! c# q) }9 m/ R6 Z% h6 P45 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
3 i: I% ~9 }/ b45 Query SELECT * FROM posts WHERE date_published is not null AND date_published <= '20131021144428' AND date_published >= '20130421144428' ORDER BY impressions DESC LIMIT 0,103 A% p# x8 s8 }( y( o9 Q
46 Connect root@localhost as anonymous on pnet_blog$ n t! `; ^$ x* x2 c1 d! g
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
3 a' s I. P, w* Y. p6 D3 v2 ^, V46 Quit0 K! |" V& Y& n; y* |
45 Quit |
|