Series Article Directory
Chapter 1: sql_mode Chapter
2: optimize table, analyze table, alter table, gh-ost Chapter 3
: InnoDB MVCC principle
Chapter 4: SQL statement execution process
Chapter 5: Introduction to Percona Toolkit
Chapter 6: MySQL Index
Chapter 7: MySQL Lock
Chapter 8: MySQL Lock Analysis
Chapter 9: MySQL Deadlock Troubleshooting Steps
Article directory
foreword
MySQL deadlock occasionally occurs online or in the development environment. You can troubleshoot the deadlock problem through the following methods.
1. Online environment
1. View the last deadlock situation
show engine innodb status\G
The locked statement can be reversed based on the executed sql.
2. error log
Modify or increase the configuration in the /etc/my.cnf file, pay attention to the added position under [mysqld], otherwise it will be invalid;
innodb_print_all_deadlocks = ON
The path of the error log can be viewed through the following sql:
SHOW VARIABLES LIKE 'log_error';
In this way, you can troubleshoot mysql deadlock through error log analysis
2. Development environment
1. When a deadlock occurs
SELECT
a.trx_id,
d.SQL_TEXT,
a.trx_state,
a.trx_started,
a.trx_query,
b.ID,
b.USER,
b.DB,
b.COMMAND,
b.TIME,
b.STATE,
b.INFO,
c.PROCESSLIST_USER,
c.PROCESSLIST_HOST,
c.PROCESSLIST_DB
FROM
information_schema.INNODB_TRX a
LEFT JOIN information_schema.PROCESSLIST b ON a.trx_mysql_thread_id = b.id
LEFT JOIN PERFORMANCE_SCHEMA.threads c ON b.id = c.PROCESSLIST_ID
LEFT JOIN PERFORMANCE_SCHEMA.events_statements_current d ON d.THREAD_ID = c.THREAD_ID;
You can see the statements that are currently waiting for locks.
2. Analyze the locking range
SELECT ENGINE,ENGINE_TRANSACTION_ID,THREAD_ID,EVENT_ID,OBJECT_SCHEMA,OBJECT_NAME,INDEX_NAME,LOCK_TYPE, LOCK_MODE,LOCK_STATUS,LOCK_DATA FROM performance_schema.data_locks;