Mysql Error:1205错误诊断_MySQL
bitsCN.com
前两天遇到一个1205(ER_LOCK_WAIT_TIMEOUT)的错误,弄了半天终于找到原因,掌握原理+细心才能找到罪归祸首。下面我给大家分享下这个问题的分析处理过程,希望对大家有所帮助。接到slave error告警后,看到现场是这样的:slave重做binlog因为锁超时中断,报HA_ERR_LOCK_WAIT_TIMEOUT错误。
超时,easy啊,心想估计是有大事务长期持有锁,导致其他事务超时等待。但是这个库是只读的备库,不可能有写事务,通过show processlist命令也确实没有发现写事务,倒是有一个大查询任务。当时觉得MVCC查询不上锁啊,直接无视。我尝试重新start slave,发现没过几秒钟,错误依然出现,并且Exec_Master_Log_Pos没有变化,这说明同样的事务尝试写错误,依然被堵住,导致锁超时等待了。这一定是事务持有锁导致锁超时,但机器上除了查询,啥也木有。隔离级别,确认下隔离级别,虽然生产环境中机器都是RC(读提交)模式,但也不排除这种可能。但结果再次让我失望,事务隔离级别是读提交。
会不会是存储引擎的问题,我又验证了一把,表是innodb存储引擎,读不存在说是上表锁的情况。无语了,难道innodb的MVCC,读在某些情况下也上锁?这岂不是与读不上锁上违背吗?继续排查问题,查看锁等待情况:
select * from information_schema.innodb_lock_waits;
这说明确实有事务堵住了更新。继续,
SELECT r.trx_id waiting_trx_id,
r.trx_query waiting_query,
b.trx_id blocking_trx_id,
b.trx_query blocking_query,
b.trx_mysql_thread_id blocking_thread,
b.trx_started,
b.trx_wait_started
FROM information_schema.innodb_lock_waits w
INNER JOIN information_schema.innodb_trx b
ON b.trx_id = w.blocking_trx_id
INNER JOIN information_schema.innodb_trx r
ON r.trx_id = w.requesting_trx_id
从图中可以看到,blocking_query确实是select语句,靠,难道真是它上锁了,上的什么锁呢?
select * from information_schema.innodb_locks;
可以看到一个读锁和一个写锁,这说明了,查询的确是上了记录的读锁,锁应该都是在innodb层面加的。到底为啥会上读锁呢?
select trx_id,trx_state,trx_isolation_level from information_schema.innodb_trx;
答案揭晓了,可以看到RUNNING的事务隔离级别是SERIALIZABLE,串行化隔离级别导致读上锁,进而阻塞复制无法进行下去。
这个例子其实很简单,通过这个例子可以看到,information_schema下面的几张表太重要了,暴露了很多信息,方便我们排查问题。同时排查问题时,一定要坚信原理,并且细心,问题总会水落石出。
bitsCN.com
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











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

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.

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.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.
