如何在我的Linux服务器上跟踪MySQL查询?

例如,我想要设置某种侦听器,然后请求一个web页面并查看引擎执行的所有查询,或者只是查看在生产服务器上运行的所有查询。我该怎么做呢?


当前回答

我一直想做同样的事情,并从各种帖子中拼凑出了一个解决方案,再加上创建了一个小的控制台应用程序来输出实时查询文本,因为它被写入日志文件。这在我的情况下是重要的,因为我使用实体框架与MySQL,我需要能够检查生成的SQL。

创建日志文件的步骤(一些其他帖子的复制,这里都是为了简单起见):

Edit the file located at: C:\Program Files (x86)\MySQL\MySQL Server 5.5\my.ini Add "log=development.log" to the bottom of the file. (Note saving this file required me to run my text editor as an admin). Use MySql workbench to open a command line, enter the password. Run the following to turn on general logging which will record all queries ran: SET GLOBAL general_log = 'ON'; To turn off: SET GLOBAL general_log = 'OFF'; This will cause running queries to be written to a text file at the following location. C:\ProgramData\MySQL\MySQL Server 5.5\data\development.log Create / Run a console app that will output the log information in real time: Source available to download here Source: using System; using System.Configuration; using System.IO; using System.Threading; namespace LiveLogs.ConsoleApp { class Program { static void Main(string[] args) { // Console sizing can cause exceptions if you are using a // small monitor. Change as required. Console.SetWindowSize(152, 58); Console.BufferHeight = 1500; string filePath = ConfigurationManager.AppSettings["MonitoredTextFilePath"]; Console.Title = string.Format("Live Logs {0}", filePath); var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); // Move to the end of the stream so we do not read in existing // log text, only watch for new text. fileStream.Position = fileStream.Length; StreamReader streamReader; // Commented lines are for duplicating the log output as it's written to // allow verification via a diff that the contents are the same and all // is being output. // var fsWrite = new FileStream(@"C:\DuplicateFile.txt", FileMode.Create); // var sw = new StreamWriter(fsWrite); int rowNum = 0; while (true) { streamReader = new StreamReader(fileStream); string line; string rowStr; while (streamReader.Peek() != -1) { rowNum++; line = streamReader.ReadLine(); rowStr = rowNum.ToString(); string output = String.Format("{0} {1}:\t{2}", rowStr.PadLeft(6, '0'), DateTime.Now.ToLongTimeString(), line); Console.WriteLine(output); // sw.WriteLine(output); } // sw.Flush(); Thread.Sleep(500); } } } }

其他回答

我一直想做同样的事情,并从各种帖子中拼凑出了一个解决方案,再加上创建了一个小的控制台应用程序来输出实时查询文本,因为它被写入日志文件。这在我的情况下是重要的,因为我使用实体框架与MySQL,我需要能够检查生成的SQL。

创建日志文件的步骤(一些其他帖子的复制,这里都是为了简单起见):

Edit the file located at: C:\Program Files (x86)\MySQL\MySQL Server 5.5\my.ini Add "log=development.log" to the bottom of the file. (Note saving this file required me to run my text editor as an admin). Use MySql workbench to open a command line, enter the password. Run the following to turn on general logging which will record all queries ran: SET GLOBAL general_log = 'ON'; To turn off: SET GLOBAL general_log = 'OFF'; This will cause running queries to be written to a text file at the following location. C:\ProgramData\MySQL\MySQL Server 5.5\data\development.log Create / Run a console app that will output the log information in real time: Source available to download here Source: using System; using System.Configuration; using System.IO; using System.Threading; namespace LiveLogs.ConsoleApp { class Program { static void Main(string[] args) { // Console sizing can cause exceptions if you are using a // small monitor. Change as required. Console.SetWindowSize(152, 58); Console.BufferHeight = 1500; string filePath = ConfigurationManager.AppSettings["MonitoredTextFilePath"]; Console.Title = string.Format("Live Logs {0}", filePath); var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); // Move to the end of the stream so we do not read in existing // log text, only watch for new text. fileStream.Position = fileStream.Length; StreamReader streamReader; // Commented lines are for duplicating the log output as it's written to // allow verification via a diff that the contents are the same and all // is being output. // var fsWrite = new FileStream(@"C:\DuplicateFile.txt", FileMode.Create); // var sw = new StreamWriter(fsWrite); int rowNum = 0; while (true) { streamReader = new StreamReader(fileStream); string line; string rowStr; while (streamReader.Peek() != -1) { rowNum++; line = streamReader.ReadLine(); rowStr = rowNum.ToString(); string output = String.Format("{0} {1}:\t{2}", rowStr.PadLeft(6, '0'), DateTime.Now.ToLongTimeString(), line); Console.WriteLine(output); // sw.WriteLine(output); } // sw.Flush(); Thread.Sleep(500); } } } }

尽管答案已经被接受,但我想提出一个可能是最简单的选择:

$ mysqladmin -u bob -p -i 1 processlist

这将每秒钟在屏幕上打印当前查询。

-u执行命令的mysql用户名 -p提示输入你的密码(这样你就不必把它保存在一个文件中或让命令出现在你的命令历史中) i以秒为单位的间隔。 使用——verbose标志显示完整的进程列表,显示每个进程的整个查询。(谢谢,nmat)

这可能有一个缺点:如果快速查询在您设置的时间间隔之间运行,则可能不会显示。IE:我的间隔设置为1秒,如果有一个查询需要0.02秒才能运行,并且在间隔之间运行,您将看不到它。

当您希望快速检查正在运行的查询而无需设置侦听器或其他任何东西时,最好使用此选项。

斯特拉

查看实时MySQL/MariaDB查询的最快方法是使用调试器。在Linux上,你可以使用strace,例如:

sudo strace -e trace=read,write -s 2000 -fp $(pgrep -nf mysql) 2>&1

因为有很多转义字符,你可以通过管道将strace的输出格式化(只需在这两个一行程序之间添加|)到下面的命令中:

grep --line-buffered -o '".\+[^"]"' | grep --line-buffered -o '[^"]*[^"]' | while read -r line; do printf "%b" $line; done | tr "\r\n" "\275\276" | tr -d "[:cntrl:]" | tr "\275\276" "\r\n"

因此,您应该看到相当干净的SQL查询,没有时间,而不涉及配置文件。

显然,这不会取代下面描述的启用日志的标准方法(其中涉及到重新加载SQL服务器)。

目录

使用MySQL探针来查看实时的MySQL查询,而不需要接触服务器。示例脚本:

#!/usr/sbin/dtrace -q
pid$target::*mysql_parse*:entry /* This probe is fired when the execution enters mysql_parse */
{
     printf("Query: %s\n", copyinstr(arg1));
}

将上述脚本保存到一个文件(如watch.d),并运行:

pfexec dtrace -s watch.d -p $(pgrep -x mysqld)

了解更多信息:开始使用DTracing MySQL

Gibbs MySQL Spyglass

请看这个答案。

Logs

以下是对发展有用的步骤。

将这些行添加到~/.my.cnf或全局my.cnf中:

[mysqld]
general_log=1
general_log_file=/tmp/mysqld.log

路径:/var/log/mysqld.log或/usr/local/var/log/mysqld.log也可以工作,这取决于您的文件权限。

然后重新启动你的MySQL/MariaDB by(必要时加上sudo):

killall -HUP mysqld

然后检查你的日志:

tail -f /tmp/mysqld.log

完成后,将general_log更改为0(以便将来可以使用它),然后删除文件并重新启动SQL server: killall -HUP mysqld。

如果你想进行监控和统计,那么有一个很好的开源工具Percona monitoring and Management

但它是一个基于服务器的系统,并且它的启动不是很简单。

并有现场演示系统进行测试。

MySQL命令:SHOW FULL PROCESSLIST;查看在任何给定时间正在处理哪些查询,但这可能无法实现您所希望的。

获得历史记录而不必修改使用服务器的每个应用程序的最佳方法可能是通过触发器。您可以设置触发器,以便每次查询运行都会导致查询被插入到某种历史表中,然后创建一个单独的页面来访问此信息。

请注意,这可能会大大降低服务器上的所有内容,因为在每个查询上都添加了一个额外的INSERT。


编辑:另一种选择是通用查询日志,但将其写入平面文件将消除许多灵活显示的可能性,特别是实时显示。如果您只是想要一种简单、易于实现的方法来查看发生了什么,那么启用GQL,然后在日志文件上运行tail -f就可以了。