有时候,我们想要查看,访问某个页面,mysql执行的所有的sql,在mysql层面做这个事情就容易的多
下面是mysql版本 >=5.6 的设置:
1.编辑my.cnf
vim /usr/local/mysql/my.cnf
[mysqld]
general_log_file = /var/log/mysql/mysql.log
general_log = 1
2.连接mysql,通过命令 mysql -u <username> -p
, 进入后执行:
SET GLOBAL general_log = 1;
3.重启mysql
/etc/init.d/mysql restart
4.如果文件 /var/log/mysql/mysql.log 不存在,则创建文件
touch /var/log/mysql/mysql.log
设置文件权限
chmod 777 /var/log/mysql/mysql.log
查看log文件的输出:
tail -f /var/log/mysql/mysql.log
访问页面,然后查看日志输出,譬如我的输出如下:
180411 12:38:28 282 Connect root@localhost on fecshop
282 Query SET NAMES 'utf8'
282 Query SELECT * FROM `customer` WHERE (`id`=432) AND (`status`=1)
282 Query SHOW FULL COLUMNS FROM `customer`
282 Query SELECT
kcu.constraint_name,
kcu.column_name,
kcu.referenced_table_name,
kcu.referenced_column_name
FROM information_schema.referential_constraints AS rc
JOIN information_schema.key_column_usage AS kcu ON
(
kcu.constraint_catalog = rc.constraint_catalog OR
(kcu.constraint_catalog IS NULL AND rc.constraint_catalog IS NULL)
) AND
kcu.constraint_schema = rc.constraint_schema AND
kcu.constraint_name = rc.constraint_name
WHERE rc.constraint_schema = database() AND kcu.table_schema = database()
AND rc.table_name = 'customer' AND kcu.table_name = 'customer'
282 Quit
5.输出慢查询日志
在上面的my.cnf中加入下面的配置
# Set Slow Query Log
long_query_time = 1
slow_query_log = 1
slow_query_log_file = /usr/log/slowquery.log
log_queries_not_using_indexes = 1
创建文件/usr/log/slowquery.log
, 设置777 权限,慢查询就会写入到该文件中