Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell script for kill deadlock process in MySQL

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly talks about "the shell script of kill deadlock process in MySQL". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the shell script of the kill zombie process in MySQL.

SHELL script: mysqld_kill_sleep.sh

#! / bin/sh

Mysql_pwd= "root password"

Mysqladmin_exec= "/ usr/local/bin/mysqladmin"

Mysql_exec= "/ usr/local/bin/mysql"

Mysql_timeout_dir= "/ tmp"

Mysql_timeout_log= "$mysql_timeout_dir/mysql_timeout.log"

Mysql_kill_timeout_sh= "$mysql_timeout_dir/mysql_kill_timeout.sh"

Mysql_kill_timeout_log= "$mysql_timeout_dir/mysql_kill_timeout.log"

$mysqladmin_exec-uroot-p "$mysql_pwd" processlist | awk'{print $12, $2, $4}'| grep-v Time | grep-v'|'| sort-rn > $mysql_timeout_log

Awk'{if ($1 > 30 & & $3 million = "root") print "'" $mysql_exec ""'- e "" / "kill", $2 "/"-uroot ""-p "" / "$mysql_pwd" / ""; "}'$mysql_timeout_log > $mysql_kill_timeout_sh"

Echo "check start...." > > $mysql_kill_timeout_log

Echo `date` > $mysql_kill_timeout_log

Cat $mysql_kill_timeout_sh

Write this to mysqld_kill_sleep.sh. Then chmod 0 mysqld_kill_sleep.sh,chmod u+rx mysqld_kill_sleep.sh, and then use the root account to run in cron, and adjust the time by yourself.

Displays after execution:

Www#. / mysqld_kill_sleep.sh

/ usr/local/bin/mysql-e "kill 27549"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27750"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27840"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27867"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27899"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27901"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27758"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27875"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27697"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27888"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27861"-uroot-p "mysql root password"

If you confirm that there is no problem, change the last cat to sh.

I rewrote the script above:

#! / bin/bash

Mysql_pwd= "password"

Mysql_exec= "/ usr/local/mysql/bin/mysql"

Mysql_timeout_dir= "/ tmp"

Mysql_kill_timeout_sh= "$mysql_timeout_dir/mysql_kill_timeout.sh"

Mysql_kill_timeout_log= "$mysql_timeout_dir/mysql_kill_timeout.log"

$mysql_exec-uroot-p$mysql_pwd-e "show processlist" | grep-I "Locked" > > $mysql_kill_timeout_log

Chmod 777 $mysql_kill_timeout_log

For line in `$ mysql_kill_timeout_log | awk'{print $1}'`

Do

Echo "$mysql_exec-uroot-p$mysql_pwd-e /" kill $line/ "> > $mysql_kill_timeout_sh

Done

Chmod 777 $mysql_kill_timeout_sh

Cat $mysql_kill_timeout_sh

=

A lot of times! Lock the watch if you are not careful! Here is the ultimate way to solve the problem of table lock!

Case one

Mysql > show processlist

See sql statement

If there are few words

Mysql > kill thread_id

It can be solved.

The process of kill losing the first table lock is still not improved. Since there is no improvement, let's find a way to kill all the locking processes. The simple script is as follows.

#! / bin/bash

Mysql-u root-e "show processlist" | grep-I "Locked" > > locked_log. Txt

For line in `cat locked_log.txt | awk'{print $1}'`

Do

Echo "kill $line;" > > kill_thread_id. Sql

Done

Now the content of kill_thread_id.sql looks like this.

Kill 66402982

Kill 66402983

Kill 66402986

Kill 66402991

.

Well, if we execute it in the shell of mysql, we can kill all the processes that lock the table.

Mysql > source kill_thread_id. Sql

Of course, it can also be done one by one.

For id in `mysqladmin processlist | grep-I locked | awk'{print $1}'`

Do

Mysqladmin kill ${id}

Done

Case two

If a large number of operations can be generated through a series of select statements, then these results can theoretically be processed in batches.

However, mysql does not provide the ability to analyze the result set like eval. So you can only save the select results to a temporary file now, and then execute the instructions in the temporary file.

The specific process is as follows:

Mysql > SELECT concat ('KILL', id,';') FROM information_schema.processlist WHERE user='root'

+-- +

| | concat ('KILL', id,';') |

+-- +

| | KILL 3101 |

| | KILL 2946 |

+-- +

2 rows IN SET (0.00 sec)

Mysql > SELECT concat ('KILL', id,';') FROM information_schema.processlist WHERE user='root' INTO OUTFILE'/ tmp/a.txt'

Query OK, 2 rows affected (0.00 sec)

Mysql > source / tmp/a.txt

Query OK, 0 rows affected (0.00 sec)

Case three

The mode of MySQL + PHP often leads to a large number of dead processes in MySQL under high concurrency pressure, resulting in service hanging. In order to kill these processes automatically, a script is created and automatically executed by crontab in the background of the server. It is found that after doing so, it really alleviates the problem very well. Send out this script and Share.

According to their own actual needs, some changes have been made:

SHELL script: mysqld_kill_sleep.sh

#! / bin/sh

Mysql_pwd= "root password"

Mysqladmin_exec= "/ usr/local/bin/mysqladmin"

Mysql_exec= "/ usr/local/bin/mysql"

Mysql_timeout_dir= "/ tmp"

Mysql_timeout_log= "$mysql_timeout_dir/mysql_timeout.log"

Mysql_kill_timeout_sh= "$mysql_timeout_dir/mysql_kill_timeout.sh"

Mysql_kill_timeout_log= "$mysql_timeout_dir/mysql_kill_timeout.log"

$mysqladmin_exec-uroot-p "$mysql_pwd" processlist | awk'{print $12, $2, $4}'| grep-v Time | grep-v'|'| sort-rn > $mysql_timeout_log

Awk'{if ($1 > 30 & & $3 million = "root") print "'" $mysql_exec ""'- e ""\ "kill", $2 "\"- uroot"- p"\" $mysql_pwd "\";"}'$mysql_timeout_log > $mysql_kill_timeout_sh "

Echo "check start...." > > $mysql_kill_timeout_log

Echo `date` > $mysql_kill_timeout_log

Cat $mysql_kill_timeout_sh

Write this to mysqld_kill_sleep.sh. Then chmod 0 mysqld_kill_sleep.sh,chmod u+rx mysqld_kill_sleep.sh, and then use the root account to run in cron, and adjust the time by yourself.

Displays after execution:

Www#. / mysqld_kill_sleep.sh

/ usr/local/bin/mysql-e "kill 27549"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27750"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27840"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27867"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27899"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27901"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27758"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27875"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27697"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27888"-uroot-p "mysql root password"

/ usr/local/bin/mysql-e "kill 27861"-uroot-p "mysql root password"

If you confirm that there is no problem, change the last cat to sh.

I rewrote the script above:

#! / bin/bash

Mysql_pwd= "password"

Mysql_exec= "/ usr/local/mysql/bin/mysql"

Mysql_timeout_dir= "/ tmp"

Mysql_kill_timeout_sh= "$mysql_timeout_dir/mysql_kill_timeout.sh"

Mysql_kill_timeout_log= "$mysql_timeout_dir/mysql_kill_timeout.log"

$mysql_exec-uroot-p$mysql_pwd-e "show processlist" | grep-I "Locked" > > $mysql_kill_timeout_log

Chmod 777 $mysql_kill_timeout_log

For line in `$ mysql_kill_timeout_log | awk'{print $1}'`

Do

Echo "$mysql_exec-uroot-p$mysql_pwd-e\" kill $line\ "> $mysql_kill_timeout_sh

Done

Chmod 777 $mysql_kill_timeout_sh

Cat $mysql_kill_timeout_sh

Isn't it convenient!

=

The output of the processlist command shows which threads are running and can help identify problematic query statements. Use this command in two ways.

1. Enter mysqladmin processlist under the mysql/bin directory

two。 Start mysql and enter show processlist

If you have SUPER permission, you can see all the threads; otherwise, you can only see the threads that you initiated (that is, the threads running under the corresponding MySQL account).

The form of the data is as follows (only three pieces have been intercepted):

Mysql > show processlist

+-

| | Id | User | Host | db | Command | Time | State | Info |

+-

| | 207 | root | 192.168.0.20 Sleep 51718 | mytest | Sleep | 5 | | NULL |

| | 208 | root | 192.168.0.20 Sleep 51719 | mytest | Sleep | 5 | | NULL |

| | 220 | root | 192.168.0.20 Query 51731 | mytest | Query | 84 | Locked |

Select bookname,culture,value,type from book where id=001

First briefly talk about the meaning and purpose of each column, the first column, id, needless to say, a logo, you want to kill a statement is very useful. The user column shows the pre-order user. If it is not root, this command only shows the sql statements within your permissions. The host column, which shows which port of the ip this statement is issued from. Hehe, it can be used to track the user who has the problem statement. The db column, which shows which one the process is currently connected to. The command column, which shows the commands executed by the current connection, which are generally sleep, query, connect. Time column, the duration of this state, in seconds. State column, shows the status of the sql statement using the current connection, very important column, there will be a description of all the states later, please note that state is only a certain state in the execution of the statement, a sql statement, which has been queried as an example, may need to be completed through states such as copying to tmp table,Sorting result,Sending data, info column, display this sql statement, because the length is limited, so the long sql statement is not fully displayed But it is an important basis to judge the problem sentence.

The most important thing in this command is the state column, which lists the following states:

Checking table

Checking the datasheet (this is automatic).

Closing tables

The modified data in the table is being flushed to disk and the table that has been used up is being closed. This is a quick operation, and if not, you should make sure that the disk space is full or that the disk is under a heavy load.

Connect Out

The replication slave server is connecting to the master server.

Copying to tmp table on disk

Because the temporary result set is larger than tmp_table_size, temporary tables are being converted from memory storage to disk storage to save memory.

Creating tmp table

Creating a temporary table to hold some of the query results.

Deleting from main table

The server is performing the first part of the multi-table deletion, and the first table has just been deleted.

Deleting from reference tables

The server is performing the second part of the multi-table deletion and is deleting records for other tables.

Flushing tables

Executing FLUSH TABLES, waiting for another thread to close the data table.

Killed

If a kill request is sent to a thread, the thread will check the kill flag bit and abandon the next kill request. MySQL checks the kill flag bit in each main loop, but in some cases the thread may take a short time to die. If the thread is locked by another thread, the kill request takes effect as soon as the lock is released.

Locked

Locked by other queries.

Sending data

The record of the Select query is being processed and the results are being sent to the client.

Sorting for group

Sorting for GROUP BY.

Sorting for order

Sorting for ORDER BY.

Opening tables

This process should be very fast unless disturbed by other factors. For example, a data table cannot be opened by another thread until the execution of an Alter TABLE or LOCK TABLE statement is complete. Trying to open a table.

Removing duplicates

An Select DISTINCT query is being executed, but MySQL cannot optimize those duplicate records in the previous phase. Therefore, MySQL needs to remove the duplicate records again and then send the results to the client.

Reopen table

A lock on a table is acquired, but the lock cannot be acquired until the table structure has been modified. The lock has been released, the datasheet has been closed, and an attempt is being made to reopen the datasheet.

Repair by sorting

The repair directive is sorting to create an index.

Repair with keycache

The repair instruction is using the index cache to create new indexes one by one. It will be slower than Repair by sorting.

Searching rows for update

We are talking about finding qualified records for updating. It must be done before Update modifies the relevant records.

Sleeping

Waiting for the client to send a new request.

System lock

Waiting to acquire an external system lock. If you are not currently running multiple mysqld servers requesting the same table at the same time, you can disable external system locks by adding the-- skip-external-locking parameter.

Upgrading lock

Insert DELAYED is trying to get a lock table to insert a new record.

Updating

Searching for matching records and modifying them.

User Lock

Waiting for GET_LOCK ().

Waiting for tables

The thread is informed that the data table structure has been modified and needs to be reopened to get the new structure. Then, in order to reopen the table, you must wait until all other threads close the table. This notification occurs in the following situations: FLUSH TABLES tbl_name, Alter TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE, or OPTIMIZE TABLE.

Waiting for handler insert

Insert DELAYED has processed all pending insert operations and is waiting for a new request.

Most states correspond to fast operations, and as long as one thread stays in the same state for a few seconds, there may be a problem that needs to be checked.

There are other states not listed above, but most of them are only useful to see if there are any errors on the server.

At this point, I believe that you have a deeper understanding of the shell script of the kill zombie process in MySQL, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report