您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

何时、如何开启MySql日志?_MySQL

2024/3/3 22:39:22发布28次查看
假如你是一名 web 开发人员,如果你想调试你的应用或提升其性能的话,那你需要去参考各种日志文件。日志是开始故障排除最好的选择。就著名的 mysql 数据库服务器而言,你需要参考以下日志文件:
错误日志:它包含了服务器运行时(当然也包括服务启动和停止时)所发生的错误信息普通查询日志:这是一个记录 mysqld 在做什么(连接,断开,查询)的通用日志慢查询日志:正如其名,它记录了 慢 的查询 sql 语句本文未涉及到二进制日志。二进制日志要求非常高的服务器硬件配置,而且只是在特定场景下(比如,主从复制,主从安装,某些数据的恢复操作)有用。否则的话,它就是一名实实在在的 性能杀手。
通过 mysql 配置启用日志日志相关参数位于 [mysqld] 部分。
编辑 mysql 配置文件:
nano /etc/mysql/my.cnf
以上是 debian 下的默认安装目录,其他 linux 发布版可能不太一样,这个文件中 mysql 服务器的参数如下:
# * 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 服务器以上方法要求服务重启才能生效:
service mysql restart
或者使用 systemd:
systemctl restart mysql.service
运行时启用日志mysql 5.1 之后我们可以在运行时启用或者禁用日志。
运行时启用日志,登录 mysql 客户端(mysql -u root -p)然后执行:
set global general_log = 'on';
set global slow_query_log = 'on';
运行时禁用日志,登录 mysql 客户端(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}
检验服务器配置使用 show variables like '%log%'; 来检查服务器和日志文件相关的变量:
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)
服务器变量相关官方文档参考 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 43 query select * from posts where date_published is not null and date_published 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 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 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 = '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 46 quit
45 quit
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product