Linux上MySQL日志的配置和开启

1.MySQL下有以下种日志。ps -ef|grep mysqld

错误日志:log-err
查询日志:log
慢查询日志:log-slow-queries
更新日志:log-update
二进制日志:log-bin

2.开启查询日志。

方式一:直接修改MySQL配置文件 vi /etc/my.cnf

在[mysqld]节点下添加:然后重启服务:service mysql restart

log-error=/usr/local/mysql/log/error.log
log=/usr/local/mysql/log/mysql.log
long_query_time=2
log-slow-queries= /usr/local/mysql/log/slowquery.log

启动报错:

创建多级目录,父目录自动创建:mkdir -p /usr/local/mysql/log

创建文件:touch /usr/local/mysql/log/error.log

最终配置,及错误展示:默认错误日志在/var/lib/mysql/你的主机名.err

general_log=1
general_log_file=/usr/local/mysql/mysql-5.6.42/log/general.log
slow_query_log=1
slow_query_log_file=/usr/local/mysql/mysql-5.6.42/log/slowqueries.log
long_query_time=2
log_output='FILE'
##可以同时使用
log_output='FILE,TABLE'

##MySQL不会自动创建,手动创建日志文件
[root@instance-zq6zwn1b ~]# mkdir -p /usr/local/mysql/mysql-5.6.42/log
[root@instance-zq6zwn1b ~]# touch /usr/local/mysql/mysql-5.6.42/log/general.log
[root@instance-zq6zwn1b ~]# ^C
[root@instance-zq6zwn1b ~]# touch /usr/local/mysql/mysql-5.6.42/log/slowqueries.log
[root@instance-zq6zwn1b ~]# service mysql restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS!

##重启报错(Errcode: 13 - Permission denied),赋值权限即可
2019-01-15 22:58:06 9911 [Note] InnoDB: Waiting for purge to start
2019-01-15 22:58:06 9911 [Note] InnoDB: 5.6.42 started; log sequence number 3162695
/usr/sbin/mysqld: File '/usr/local/mysql/mysql-5.6.42/log/slowqueries.log' not found (Errcode: 13 - Permission denied)
2019-01-15 22:58:06 9911 [ERROR] Could not open /usr/local/mysql/mysql-5.6.42/log/slowqueries.log for logging (error 13). Turning logging off for the whole duration of the MySQL server process. To turn it on again: fix the cause, shutdown the MySQL server and restart it.
/usr/sbin/mysqld: File '/usr/local/mysql/mysql-5.6.42/log/general.log' not found (Errcode: 13 - Permission denied)
2019-01-15 22:58:06 9911 [ERROR] Could not open /usr/local/mysql/mysql-5.6.42/log/general.log for logging (error 13). Turning logging off for the whole duration of the MySQL server process. To turn it on again: fix the cause, shutdown the MySQL server and restart it.
2019-01-15 22:58:06 9911 [Note] Server hostname (bind-address): '*'; port: 3306


##赋值权限
[root@instance-zq6zwn1b ~]# chmod 777 /usr/local/mysql/mysql-5.6.42/log/general.log
[root@instance-zq6zwn1b ~]# chmod 777 /usr/local/mysql/mysql-5.6.42/log/slowqueries.log
[root@instance-zq6zwn1b ~]# chmod 777 /usr/local/mysql/mysql-5.6.42/log/error.log

##原始日志目录位置,此处记录是为了,可以快速切换查看日志
/var/lib/mysql
/usr/local/mysql/mysql-5.6.42/log

参数详解:https://baijiahao.baidu.com/s?id=1608929505838938265&wfr=spider&for=pc

方式二:进入MySQL控制台,用命令设置

mysql -u root -p

##查看一下默认为慢查询的时间10秒
show variables like "%long%";
##设置成2秒
set global long_query_time=2;
##查看一下慢查询是不是已经开启
show variables like "%slow%";
##启用慢查询
set global slow_query_log='ON';

Linux上MySQL日志的配置和开启_第1张图片

Linux上MySQL日志的配置和开启_第2张图片

Linux上MySQL日志的配置和开启_第3张图片
 

show variables like 'general_log';  -- 查看普通日志是否开启
set global general_log=on; -- 开启普通日志功能
show variables like 'general_log_file';  -- 看看日志文件保存位置
set global general_log_file='tmp/general.lg'; -- 设置日志文件保存位置
show variables like 'log_output';  -- 看看日志输出类型  table或file
set global log_output='table'; -- 设置输出类型为 table
set global log_output='file';   -- 设置输出类型为file

##查看所有bin-log列表
show master logs;

##查看master状态,即最后(最新)一个binlog日志的编号名称,及其最后一个操作事件pos结束点(Position)值
show master status;

##清空所有bin-log
reset master;

引用地址:https://blog.csdn.net/intelrain/article/details/80451120

 

你可能感兴趣的:(Linux,MySQL)