监控MySQL中的所有SQL查询

Microsoft’s SQL Server has a tool called Profiler that you can use to monitor every SQL query that hits the database. This is extremely useful for programmers as well as database administrators to troubleshoot the exact queries generated by an application.

MicrosoftSQL Server有一个称为Profiler的工具,您可以使用该工具监视命中数据库的每个SQL查询。 这对于程序员和数据库管理员对应用程序生成的确切查询进行故障排除非常有用。

Having switched to using MySQL on a frequent basis, this was one of the first things I wanted to figure out how to do. How else can you see the actual SQL code generated by WordPress or phpBB?

频繁使用MySQL之后,这是我想知道如何做的第一件事。 您还怎么能看到WordPress或phpBB生成的实际SQL代码?

The first thing we’ll need to do is turn on logging of queries in MySQL. Be warned that this should only be done in development… it really slows things down to log every single query to a file.

我们需要做的第一件事是在MySQL中打开查询日志记录。 请注意,这仅应在开发中完成…确实减慢了将每个查询记录到文件的速度。

Find and open your MySQL configuration file, usually /etc/mysql/my.cnf on Ubuntu. Look for the section that says “Logging and Replication”

查找并打开您MySQL配置文件,通常是Ubuntu上的/etc/mysql/my.cnf。 查找显示“日志记录和复制”的部分

## * Logging and Replication## Both location gets rotated by the cronjob.# Be aware that this log type is a performance killer.

## *日志记录和复制## cronjob轮换了两个位置。#请注意,此日志类型是性能的杀手。

log = /var/log/mysql/mysql.log

日志= /var/log/mysql/mysql.log

Just uncomment the “log” variable to turn on logging. Restart MySQL with this command:

只需取消注释“ log”变量即可打开日志记录。 使用以下命令重新启动MySQL:

sudo /etc/init.d/mysql restart

sudo /etc/init.d/mysql重新启动

Now we’re ready to start monitoring the queries as they come in. Open up a new terminal and run this command to scroll the log file, adjusting the path if necessary.

现在,我们准备开始监视查询的出现。打开一个新终端,然后运行此命令滚动日志文件,并在必要时调整路径。

tail -f /var/log/mysql/mysql.log

尾-f /var/log/mysql/mysql.log

Now run your application. You’ll see the database queries start flying by in your terminal window. (make sure you have scrolling and history enabled on the terminal)

现在运行您的应用程序。 您会在终端窗口中看到数据库查询开始通过。 (确保在终端上启用了滚动和历史记录)

I’m impressed, phpbb3 has fairly tight, optimized SQL code. WordPress, on the other hand, is very inefficient.

令我印象深刻的是,phpbb3具有相当紧密,优化SQL代码。 另一方面,WordPress效率很低。

翻译自: https://www.howtogeek.com/howto/database/monitor-all-sql-queries-in-mysql/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值