发现问题 top命令 查看服务器负载,发现 mysql竟然 百分之两百的cpu ,引起Mysql 负载这么高的原因,估计是 索引问题和某些变态SQL语句. 排查思路 1. 确定高负载的类型,top命令看 负载高是CPU还是IO 。 2. mysql 下执行 查看当前的连接数与执行的sql 语句 。 3.
发现问题
top命令 查看服务器负载,发现 mysql竟然百分之两百的cpu,引起Mysql 负载这么高的原因,估计是索引问题和某些变态SQL语句.
排查思路
1. 确定高负载的类型,top命令看负载高是CPU还是IO。
2. mysql 下执行查看当前的连接数与执行的sql 语句。
3. 检查慢查询日志,可能是慢查询引起负载高。
4. 检查硬件问题,是否磁盘故障问题造成的。
5. 检查监控平台,对比此机器不同时间的负载。
确定负载类型(top)
<ol> <li><span><span>top - 10:14:18 up 23 days, 11:01, 1 user, </span><span>load average: 124.17, 55.88, 24.70 </span></span></li> <li><span>Tasks: 138 total, 1 running, 137 sleeping, 0 stopped, 0 zombie </span></li> <li><span>Cpu(s): 2.4%us, 1.0%sy, 0.0%ni, 95.2%id, 2.0%wa, 0.1%hi, 0.2%si, 0.0%st </span></li> <li><span>Mem: 3090528k total, 2965772k used, 124756k free, 93332k buffers </span></li> <li><span>Swap: 4192956k total, 2425132k used, 1767824k free, 756524k cached </span></li> <li><span> </span></li> <li><span>PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND </span></li> <li><span>30833 mysql 15 0 6250m 2.5g 4076 S <span>257.1</span> 49.9 529:34.45 mysqld </span></li> </ol>
查看当前的连接数与执行的sql 语句
<ol> <li><span>show processlist; </span></li> <li><span>Id User Host db Command Time State Info </span></li> <li><span>192 slave 8.8.8.142:39820 NULL Binlog Dump 58982 Has sent all binlog to slave; waiting for binlog to be updated NULL </span></li> <li><span>194 slave 8.8.8.120:41075 NULL Binlog Dump 58982 Has sent all binlog to slave; waiting for binlog to be updated NULL </span></li> <li><span>424891 biotherm 8.8.8.46:57861 biotherm Query 493 Sending data SELECT * FROM xxx_list WHERE <span>tid</span><span> = </span><span>'1112'</span><span> AND </span><span>del</span><span> = </span><span>0</span><span> ORDER BY id </span></span></li> <li><span>DESC LIMIT 0, 4 </span></li> <li><span>424917 biotherm 8.8.8.49:50984 biotherm Query 488 Sending data SELECT * FROM xxx_list WHERE <span>tid</span><span> = </span><span>'1112'</span><span> AND </span><span>del</span><span> = </span><span>0</span><span> ORDER BY id </span></span></li> <li><span>DESC LIMIT 0, 4 </span></li> <li><span>.............................................. </span></li> <li><span>430330 biotherm 8.8.8.42:35982 biotherm Query 487 Sending data SELECT * FROM xxx_list WHERE <span>tid</span><span> = </span><span>'1112'</span><span> AND </span><span>del</span><span> = </span><span>0</span><span> </span></span></li> </ol>
记录慢查询
编辑Mysql 配置文件(my.cnf),在[mysqld]字段添加以下几行:
<ol> <li><span><span>log_slow_queries</span><span> = /usr/local/mysql/var/slow_queries.log #慢查询日志路径 </span></span></li> <li><span><span>long_query_time</span><span> = </span><span>10</span><span> #记录SQL查询超过10s的语句 </span></span></li> <li><span><span>log-queries-not-using-indexes</span><span> = </span><span>1</span><span> #记录没有使用索引的sql </span></span></li> </ol>
查看慢查询日志
<ol> <li><span>tail /usr/local/mysql/var/slow_queries.log </span></li> <li><span># Time: 130305 9:48:13 </span></li> <li><span># User@Host: biotherm[biotherm] @ [8.8.8.45] </span></li> <li><span># Query_time: 1294.881407 Lock_time: 0.000179 Rows_sent: 4 Rows_examined: 1318033 </span></li> <li><span>SET <span>timestamp</span><span>=</span><span>1363916893</span><span>; </span></span></li> <li><span>SELECT * FROM xxx_list WHERE <span>tid</span><span> = </span><span>'11xx'</span><span> AND </span><span>del</span><span> = </span><span>0</span><span> ORDER BY id DESC LIMIT 0, 4; </span></span></li> </ol>
4个参数
Query_time: 0 Lock_time: 0 Rows_sent: 1 Rows_examined: 54
分别意思为:查询时间 锁定时间 查询结果行数 扫描行数,主要看扫描行数多的语句,然后去数据库加上对应的索引,再优化下变态的sql 语句。
极端情况kill sql进程
<ol> <li><span>找出占用cpu时间过长的sql,在mysql 下执行如下命令: </span></li> <li><span>show processlist; </span></li> <li><span>确定后一条sql处于Query状态,且Time时间过长,锁定它的ID,执行如下命令: </span></li> <li><span>kill QUERY 269815764; </span></li> </ol>
注意:杀死 sql进程,可能导致数据丢失,所以执行前要衡量数据的重要性。

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

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