使用tcpdump排查mysql数据库tps飙升的问题
现象 上线后习惯性的观察数据库的变化。发现数据库的tps有很大的飙升。不过幸好在双十一的时候在数据库方面做了一些完善,虽然主库的tps有飙升,但是总体load还不是很高。但是问题既然出现了,还是要解决的。 排查过程 确定是insert update 还是 delete操作
现象
上线后习惯性的观察数据库的变化。发现数据库的tps有很大的飙升。不过幸好在双十一的时候在数据库方面做了一些完善,虽然主库的tps有飙升,但是总体load还不是很高。但是问题既然出现了,还是要解决的。
排查过程
确定是insert update 还是 delete操作导致tps高?
既然是tps高,那就说明数据库修改的操作多了。到底是insert update操作多了, 还是 delete 操作多了?在天机平台可以很明显的可以看出来。如下图:
从上图,我们可以很清楚的看出来是update操作多了导致的。
到底是那些update语句导致tps高?
当时程序中并没有记录所有执行的sql语句。因此,没有一个现成的数据文件供分析。Sql语句是通过网络以文本方式传输到mysql服务器端的。因此我们完全可以通过tcpdump这个工具把所有的sql语句捕获到。
首先,为了便于比对,我先把一台服务器上的代码回滚到上线前的版本。
其次,我在两台服务器上同时执行tcpdump命令,以捕获所有执行的sql脚本。这两台服务器分别运行着上线前的旧版本程序和上线后的新版本程序。抓包命令如下:
$sudo tcpdump -i eth0 -A -s 3000 port 3306 > ~/sql.log
注意,我们在使用tcpdump的时候加了-A参数,这样就可以把sql语句都显示出来了。更多tcpdump使用,可以查看文章 调试利器之tcpdump详解
大约执行1分钟后,同时停止执行。这个时候,sql.log文件中已经包含了这段时间执行的所有sql语句。示例如下:
$grep 'update' ./sql.log | head ....5u.vD....update session_table set expire=’2014-12-12 20:01:23’ where sess_id = ‘demostring123’ limit 1
既然我们现在已经有了所有执行的sql语句,我们就可以很容易的通过使用grep, wc 等命令分析出是那些sql语句执行次数猛增了。
原文地址:使用tcpdump排查mysql数据库tps飙升的问题, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to connect to MySQL using phpMyAdmin? The URL to access phpMyAdmin is usually http://localhost/phpmyadmin or http://[your server IP address]/phpmyadmin. Enter your MySQL username and password. Select the database you want to connect to. Click the "Connection" button to establish a connection.

How to solve the MySQL "Access denied for user" error: 1. Check the user's permission to connect to the database; 2. Reset the password; 3. Allow remote connections; 4. Refresh permissions; 5. Check the database server configuration (bind-address, skip-grant-tables); 6. Check the firewall rules; 7. Restart the MySQL service. Tip: Make changes after backing up the database.
