mysql 的慢日志是排查问题sql语句,<br />**检查是否开启慢查询功能**<br />show variables like 'slow_query%';<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22438777/1637734380026-1be70c3c-c647-41f3-8ee9-3c231e89eab8.png#clientId=u117990f2-a606-4&crop=0.0317&crop=0&crop=0.9049&crop=0.8469&from=paste&height=154&id=u7ad372a7&margin=%5Bobject%20Object%5D&name=image.png&originHeight=191&originWidth=444&originalType=binary&ratio=1&rotation=0&showTitle=false&size=8403&status=done&style=shadow&taskId=u3a1e31d2-4196-45dd-90de-7bd04baedaa&title=&width=358)<br />slow_query_log :是否开启慢日志功能<br />slow_query_log_file:文件日志地址<br />**查询慢日志查询时间记录**<br /> show variables like 'long_query_time';<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22438777/1637734458694-c7210d1f-4722-49c6-b126-8fa3bbf0fd74.png#clientId=u117990f2-a606-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=164&id=u6d7e485a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=164&originWidth=376&originalType=binary&ratio=1&rotation=0&showTitle=false&size=6410&status=done&style=shadow&taskId=ua8209479-1476-440c-a03d-d0dca9aec40&title=&width=376)<br />此时说明已经开启慢日志记录,语句超过2秒就会记录,<br />如果没有开启,临时配置,可以通过临时命令开启<br />**临时命令开启**<br />set global slow_query_log='ON';<br />set global slow_query_log_file='D:/phpstudy_pro/Extensions/MySQL5.7.26/data/80-999-000312-slow.log';<br />set global long_query_time=2;<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22438777/1637734690745-a648245e-7069-4c13-81ab-b503636d537c.png#clientId=u117990f2-a606-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=202&id=u90ec8023&margin=%5Bobject%20Object%5D&name=image.png&originHeight=283&originWidth=781&originalType=binary&ratio=1&rotation=0&showTitle=false&size=18958&status=done&style=shadow&taskId=u565155c1-ccfa-46a7-a404-0f36c090946&title=&width=558)<br />这样就开启了日志,文件也会出现文件目录中,不过需要测试的化还需要在开启一个窗口,才可以,因为时间范围没有起作用,全局的话需要去掉global<br />**永久配置,修改配置文件**<br />/etc/mysql/conf.d/mysql.cnf <br />[mysqld]<br /> slow_query_log = ON <br />slow_query_log_file = /var/lib/mysql/instance-1-slow.log <br />long_query_time = 2<br />配置完成之后,重启mysql即可<br />**测试**<br />select sleep(3);<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22438777/1637734804360-f293bd63-0ccd-4125-99e6-4f1e9fc1f7de.png#clientId=u117990f2-a606-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=168&id=uf2e86a6f&margin=%5Bobject%20Object%5D&name=image.png&originHeight=168&originWidth=414&originalType=binary&ratio=1&rotation=0&showTitle=false&size=4735&status=done&style=shadow&taskId=ubbbe6255-74a2-40fd-8818-c75f035c3cd&title=&width=414)<br />此时慢日志显示这条语句,以及一些信息<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/22438777/1637734949692-0e835282-73cf-4a24-a31c-0b319fa87d43.png#clientId=u117990f2-a606-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=214&id=u8d30596d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=346&originWidth=1039&originalType=binary&ratio=1&rotation=0&showTitle=false&size=34756&status=done&style=shadow&taskId=uebe72b1a-fb5b-486f-a25e-aedf95c6940&title=&width=644)