Table of Contents
1、PHP慢脚本日志
既然找到了是php的问题,如何去定位php脚本呢?
2、Mysql的慢查询日志
Home Database Mysql Tutorial PHP慢脚本日志和Mysql的慢查询日志

PHP慢脚本日志和Mysql的慢查询日志

Jun 07, 2016 pm 04:41 PM
mysql php log Inquire Script

1、PHP慢脚本日志 间歇性的502,是后端 PHP-FPM 不可用造成的,间歇性的502一般认为是由于 PHP-FPM 进程重启造成的。 在 PHP-FPM 的子进程数目超过的配置中的数量时候,会出现间歇性的502错误,如果在配置中设置了 max_requests 的话,超过数量也会出现502错

1、PHP慢脚本日志

  • 间歇性的502,是后端 PHP-FPM 不可用造成的,间歇性的502一般认为是由于 PHP-FPM 进程重启造成的。
  • 在 PHP-FPM 的子进程数目超过的配置中的数量时候,会出现间歇性的502错误,如果在配置中设置了max_requests的话,超过数量也会出现502错误,而max_requests的设置,正是为了防止不安全的第三方library脚本的 内存泄露 ,当然你自己编写的脚本存在 死锁 的话,也会出现502现象。

  • 如果你发现mysql负载并不高,但是php-fpm的进程数和内存占用过高的话,恭喜你,大多数情况下是因为脚本存在死锁。

既然找到了是php的问题,如何去定位php脚本呢?

开启php慢查询日志:

$ sudo vi /usr/loal/php/etc/php-fpm.conf
; Default Value: 0
request_slowlog_timeout = 1s
; The log file for slow requests
; Default Value: /usr/local/php/log/php-fpm.log.slow
slowlog = /usr/local/php/log/php-fpm.log.slow
Copy after login

默认的 request_slowlog_timeout 是0,php的慢脚本日志是关闭的,因此设置为大于0的n,表示执行时间超过n的脚本将记录进入slowlog里。
然后监测到网站存在问题的时候查看下slowlog即可发现慢脚本,对脚本进行检查处理即可。

2、Mysql的慢查询日志

mysql慢查询日志对于跟踪有问题的查询非常有用,可以分析出当前程序里有很耗费资源的sql语句,那如何打开mysql的慢查询日志记录呢?

其实打开mysql的慢查询日志很简单,只需要在mysql的配置文件里(windows系统是my.ini,linux系统是my.cnf)的[mysqld]下面加上如下代码:

log-slow-queries=/var/lib/mysql/slowquery.log 
long_query_time=2 
Copy after login

long_query_time=2中的2表示查询超过两秒才记录.
如果日志内容很多,用眼睛一条一条去看会累死,mysql自带了分析的工具,使用方法如下:

$ cd /usr/local/mysql/bin
$ mysqldumpslow –help
-s,是order的顺序,主要有c,t,l,r和ac,at,al,ar,分别是按照query次数,时间,lock的时间和返回的记录数来排序,前面加了a的时倒叙 
-t,是top n的意思,即为返回前面多少条的数据 
-g,后边可以写一个正则匹配模式,大小写不敏感的 
$ mysqldumpslow -s c -t 20 host-slow.log   #访问次数最多的20个sql语句
$ mysqldumpslow -s r -t 20 host-slow.log  #返回记录集最多的20个sql
$ mysqldumpslow -t 10 -s t -g “left join” host-slow.log  #按照时间返回前10条里面含有左连接的sql语句
Copy after login

mysql慢查询日志查询手册

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1240
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How does MySQL differ from Oracle? How does MySQL differ from Oracle? Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

See all articles