背景
- 获取运行的sql语句,用来排错或者优化查询性能
解决思路
- 开启Mysql的Log功能,那么Mysql的执行语句都会写到Log文件里,然后通过BareTail这个专门查看Log文件的工具即可实时查看mysql生成的语句了
步骤
- 进入Mysql
- 启用Log功能(general_log=ON)
- SHOW VARIABLES LIKE "general_log%";
- SET GLOBAL general_log = 'ON';
- general_log_file 字段展示的是sql语句保存路径
- 假如你想设置Log文件地址(所有Sql语句都会在general_log_file里)
- SET GLOBAL general_log_file = 'c:\mysql.log';
- 假如想关闭sql语句打印
- SET GLOBAL general_log = 'OFF';
代码示范
mysql> SHOW VARIABLES LIKE "general_log%";
+------------------+----------------------------------+
| Variable_name | Value |
+------------------+----------------------------------+
| general_log | OFF |
| general_log_file | /var/lib/mysql/ubuntu-xenial.log |
+------------------+----------------------------------+
2 rows in set (0.01 sec)
mysql> SET GLOBAL general_log = 'ON';
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW VARIABLES LIKE "general_log%";
+------------------+----------------------------------+
| Variable_name | Value |
+------------------+----------------------------------+
| general_log | ON |
| general_log_file | /var/lib/mysql/ubuntu-xenial.log |
+------------------+----------------------------------+
2 rows in set (0.00 sec)
mysql>
可以下载BareTail专门查看Log文件的绿色软件(提供免费版本仅220k),看sql语句会更友好