假如你是一名 web 开发者。假设你想调试你的应用或提升其性能的话,那你须要去參考各种日志文件。日志是開始故障排除最好的选择。就著名的 MySql 数据库server而言,你须要參考下面日志文件:
  • 错误日志:它包括了server执行时(当然也包括服务启动和停止时)所发生的错误信息
  • 普通查询日志:这是一个记录 mysqld 在做什么(连接。断开,查询)的通用日志
  • 慢查询日志:正如其名,它记录了 "慢" 的查询 SQL 语句

本文未涉及到二进制日志。二进制日志要求非常高的server硬件配置。并且仅仅是在特定场景下(比方,主从复制,主从安装。某些数据的恢复操作)实用。

否则的话,它就是一名实实在在的 "性能杀手"。

关于 MySql 日志的官方文档參考 http://dev.mysql.com/doc/refman/5.7/en/server-logs.html

通过 MySql 配置启用日志

日志相关參数位于 [mysqld] 部分。
编辑 MySql 配置文件:
nano /etc/mysql/my.cnf
以上是 Debian 下的默认安装文件夹,其它 Linux 公布版可能不太一样,这个文件里 MySql server的參数例如以下:
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
#
# Here you can see queries with especially long duration
#log_slow_queries       = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id              = 1
#log_bin                        = /var/log/mysql/mysql-bin.log
expire_logs_days        = 10
max_binlog_size         = 100M
#binlog_do_db           = include_database_name
#binlog_ignore_db       = include_database_name
MySql 安装默认是不启用全部日志文件的(Windows 平台下的 error 日志除外)。Debian 安装 MySql 默认是将 error 日志发送给 syslog。

error 日志

依据 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 配置,error 日志推送给 syslog:
[mysqld_safe]
syslog
这是推荐的做法
。假设你因为某种原因,不想讲 error 日志推给 syslog。将 /etc/mysql/conf.d/mysqld_safe_syslog.cnf 文件里的上述行注掉,或者直接删除掉这个文件。然后在 /etc/mysql/my.cnf 中加入下面行:
[mysqld_safe]
log_error=/var/log/mysql/mysql_error.log
 
[mysqld]
log_error=/var/log/mysql/mysql_error.log

一般查询日志

要启用一般查询日志,将相关行取消凝视(或者加入)就可以:
general_log_file        = /var/log/mysql/mysql.log
general_log             = 1

慢查询日志

要启用慢查询日志,将相关行取消凝视(或者加入)就可以:
log_slow_queries       = /var/log/mysql/mysql-slow.log
long_query_time = 2
log-queries-not-using-indexes

配置修改后重新启动 MySql server

以上方法要求服务重新启动才干生效:
service mysql restart
或者使用 systemd

systemctl restart mysql.service

执行时启用日志

MySql 5.1 之后我们能够在执行时启用或者禁用日志。
执行时启用日志。登录 MySql client(mysql -u root -p)然后执行:
SET GLOBAL general_log = 'ON';
SET GLOBAL slow_query_log = 'ON';

执行时禁用日志,登录 Mysql client(mysql -u root -p)后执行:
SET GLOBAL general_log = 'OFF';
SET GLOBAL slow_query_log = 'OFF';

这样的方式适用于全部平台并且不须要重新启动服务。

显示日志结果

error 日志

按以上办法设置以后,你能够通过下面命令显示 error 日志:
tail -f /var/log/syslog
备注:假设你没有配置 error 日志文件。MySql 将把 error 日志保存在数据文件夹(一般是 /var/lib/mysql)下的一个名为 {host_name}.err 的文件里。

普通查询日志

按以上办法设置以后。你能够通过使用下面命令来显示普通日志:
tail -f /var/log/mysql/mysql.log
备注:假设你没有配置普通日志文件。MySql 将把普通日志保存在数据文件夹(一般是 /var/lib/mysql)下的一个名为 {host_name}.log 的文件里。

慢查询日志

按以上办法设置以后。你能够通过使用下面命令来显示慢查询日志:
tail -f /var/log/mysql/mysql-slow.log

备注:假设你没有配置慢查询日志文件,MySql 将把普通日志保存在数据文件夹(一般是 /var/lib/mysql)下的一个名为 {host_name}-slow.log 的文件里。

循环日志

别忘了滚动日志。否则的话日志文件可能会变得非常庞大。
Debian(以及 Debian 派生系列诸如 Ubuntu 等)系统,MySql 初始安装之后,循环日志就已经使用了 logrotate:
nano /etc/logrotate.d/mysql-server
对于其它 Linux 发行版。可能须要做一些修改:

# - I put everything in one block and added sharedscripts, so that mysql gets
# flush-logs'd only once.
# Else the binary logs would automatically increase by n times every day.
# - The error log is obsolete, messages go to syslog now.
/var/log/mysql.log /var/log/mysql/mysql.log /var/log/mysql/mysql-slow.log {
daily
rotate 7
missingok
create 640 mysql adm
compress
sharedscripts
postrotate
test -x /usr/bin/mysqladmin || exit 0
# If this fails, check debian.conf!
MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then
# Really no mysqld or rather a missing debian-sys-maint user?
# If this occurs and is not a error please report a bug.
#if ps cax | grep -q mysqld; then
if killall -q -s0 -umysql mysqld; then
exit 1
fi
else
$MYADMIN flush-logs
fi
endscript
}

检验server配置

使用 show variables like '%log%'; 来检查server和日志文件相关的变量:
root@cosmos ~ # mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 144332
Server version: 5.5.31-0+wheezy1 (Debian)
 
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> show variables like '%log%';
+-----------------------------------------+--------------------------------+
| Variable_name                           | Value                          |
+-----------------------------------------+--------------------------------+
| back_log                                | 50                             |
| binlog_cache_size                       | 32768                          |
| binlog_direct_non_transactional_updates | OFF                            |
| binlog_format                           | STATEMENT                      |
| binlog_stmt_cache_size                  | 32768                          |
| expire_logs_days                        | 10                             |
| general_log                             | OFF                            |
| general_log_file                        | /var/lib/mysql/cosmos.log      |
| innodb_flush_log_at_trx_commit          | 1                              |
| innodb_locks_unsafe_for_binlog          | OFF                            |
| innodb_log_buffer_size                  | 8388608                        |
| innodb_log_file_size                    | 5242880                        |
| innodb_log_files_in_group               | 2                              |
| innodb_log_group_home_dir               | ./                             |
| innodb_mirrored_log_groups              | 1                              |
| log                                     | OFF                            |
| log_bin                                 | OFF                            |
| log_bin_trust_function_creators         | OFF                            |
| log_error                               |                                |
| log_output                              | FILE                           |
| log_queries_not_using_indexes           | OFF                            |
| log_slave_updates                       | OFF                            |
| log_slow_queries                        | OFF                            |
| log_warnings                            | 1                              |
| max_binlog_cache_size                   | 18446744073709547520           |
| max_binlog_size                         | 104857600                      |
| max_binlog_stmt_cache_size              | 18446744073709547520           |
| max_relay_log_size                      | 0                              |
| relay_log                               |                                |
| relay_log_index                         |                                |
| relay_log_info_file                     | relay-log.info                 |
| relay_log_purge                         | ON                             |
| relay_log_recovery                      | OFF                            |
| relay_log_space_limit                   | 0                              |
| slow_query_log                          | OFF                            |
| slow_query_log_file                     | /var/lib/mysql/cosmos-slow.log |
| sql_log_bin                             | ON                             |
| sql_log_off                             | OFF                            |
| sync_binlog                             | 0                              |
| sync_relay_log                          | 0                              |
| sync_relay_log_info                     | 0                              |
+-----------------------------------------+--------------------------------+
41 rows in set (0.00 sec)

server变量相关官方文档參考 http://dev.mysql.com/doc/refman/5.7/en/server-options.html

何时启用日志

MySql 默认安装的话,全部的日志文件都不会被启用的(除了 Windows 平台上的 error 日志)。Debian 上安装默认将 error 日志发给 syslog。
实际上,在非常多情况下日志文件都能够提供关键问题的解决的方法:

  • 总是启用 error 日志
  • 在这些情况下开启普通查询日志(最好在执行时):检查你的应用是否正确处理了 MySql 数据库连接(一个常见的错误就是从一个单一脚本多次连接到 MySql);监控来自你的应用的查询的执行情况;測试 memcached(或者相似的软件),检查某查询是被 db 执行还是被 memcached 处理
  • 当你的应用因为某些原因造成性能下降而你想找到这些慢查询时。启用慢查询日志(MySql 最好是在短期内这样配置,比方 2-3 天)

演示样例

下面是一个 MySql 普通日志的演示样例:
131021 17:43:50    43 Connect root@localhost as anonymous on pnet_blog
       43 Init DB pnet_blog
       43 Query SELECT count(id) as total_posts FROM posts WHERE date_published is not null AND date_published <= '20131021144350'
       43 Query SELECT * FROM posts WHERE date_published is not null AND date_published <= '20131021144350' ORDER BY date_published DESC LIMIT 0,10
       44 Connect root@localhost as anonymous on pnet_blog
       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
       44 Quit
       43 Quit
131021 17:44:28    45 Connect root@localhost as anonymous on pnet_blog
       45 Init DB pnet_blog
       45 Query SELECT * FROM posts WHERE url='how-and-when-to-enable-mysql-logs'
       45 Query UPDATE posts SET impressions=impressions+1 WHERE id='41'
       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
       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
       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
       46 Connect root@localhost as anonymous on pnet_blog
       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
       46 Quit
       45 Quit       
原文链接:http://www.pontikis.net/blog/how-and-when-to-enable-mysql-logs

何时、怎样开启 MySql 日志?的更多相关文章

  1. 转载Linux下开启MySQL日志

    转载https://blog.csdn.net/weixin_38187469/article/details/79273962 开启mysql日志   1.查看日志是否启用 mysql> sh ...

  2. mac 开启mysql日志

    step1: 进入终端进入mysql: step2 : 开启mysql日志 step3 : 查看mysql的日志文件所在位置 step4 : 在终端中用tail -f 命令打开该日志文件:

  3. 开启mysql日志及若干问题

    今天学习了mysql日志功能,以前也有所了解,只不过没有深入的学习,所以趁着“余热”,把我从网上找到的资料与实践 结合起来,总结一下其基本用法.学习从来都不是无趣的,就看你怎么看待学习. 1.查看查询 ...

  4. 开启MySQL日志

    找到my.ini(Linux下是my.cnf)文件,在文件里加入下面两行: log="F:/mysqllog/mysql.log" log-bin="F:/mysqllo ...

  5. Mysql是否开启binlog日志&开启方法

    运行sql   show variables like 'log_bin'; 如果Value 为 OFF 则为开启日志文件 如何开启mysql日志? 找到my,cnf 中 [mysqld]  添加如下 ...

  6. Mysql日志解析

    修改Mysql配置 Mysql配置地址为: C:\Program Files (x86)\MySQL\MySQL Server 5.5 如果无法修改可以把my.ini拷贝出来,修改完后,再拷贝回去! ...

  7. ubuntu开启慢日志

    ubuntu 开启mysql日志记录 1.找到mysql的配置文件sudo vim /etc/mysql/my.cnf将下面两行的#去掉#general_log_file = /var/log/mys ...

  8. MySQL日志设置及查看方法

    MySQL有以下几种日志: 错误日志: -log-err 查询日志: -log 慢查询日志: -log-slow-queries 更新日志: -log-update 二进制日志: -log-bin 默 ...

  9. 查看mysql日志文件

    开启mysql日志 /etc/mysql/mysql.conf.d/mysqld.cnf sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 找到第68,69行 前面 ...

随机推荐

  1. loadrunner 响应时间和TPS

    例子:一个高速路有10个入口,每个入口每秒钟只能进1辆车 1.请问1秒钟最多能进几辆车?    TPS=10 2.每辆车需要多长时间进行响应?    reponse time = 1 3.改成20辆车 ...

  2. org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [********] will not be managed by Spring

    如下图,查看层次是否正确.

  3. pycharm激活2018

    因为我的是Windows,所以这篇文章只针对Windows系统. 1.将“0.0.0.0 account.jetbrains.com”中的内容添加到hosts文件中,hosts路径为:C:\Windo ...

  4. 第2节 hive基本操作:12、hive当中的hql语法

    3.2. hive查询语法 3.2.1.SELECT https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select 基本 ...

  5. VMware12 download

    下载地址:http://filehippo.com/zh/download_vmware-workstation-pro/ 许可证地址:http://www.zdfans.com/5928.html

  6. vue开发调试工具vue-devtools安装

    vue开发调试工具别人总结的非常好,所以直接把链接拿过来了,就当做个笔记了,也希望能帮到有需要的人,感谢“沉着前进”,来源(https://www.cnblogs.com/fighxp/p/78150 ...

  7. C++暂停黑窗口

    C++中采用system("pause");来暂停黑窗口,那么操纵系统就会将窗口暂停,显示“请按任意键继续. . .” 我们用VS执行代码是,若直接按键盘的F5(开始调试),那么窗 ...

  8. [Luogu] P4460 [CQOI2018]解锁屏幕

    题目背景 使用过Android 手机的同学一定对手势解锁屏幕不陌生.Android 的解锁屏幕由3X3 个点组成,手指在屏幕上画一条线,将其中一些点连接起来,即可构成一个解锁图案.如下面三个例子所示: ...

  9. 初识 Bootstrap

    Bootstrap 概述 Bootstrap 是一个前端框架,使用它可以快速开发响应式页面,还能专门针对 PC 端或移动端快速开发,大大提高了开发效率. Bootstrap 是最受欢迎的 HTML.C ...

  10. 自动清除日期目录shell脚本

    很多时候备份通常会使用到基于日期来创建文件夹,对于这些日期文件夹下面又有很多子文件夹,对于这些日期文件整个移除,通过find结合rm或者delete显得有些力不从心.本文提供一个简单的小脚本,可以嵌入 ...