Kun*_*dra 5 mysql database deadlock mariadb galera
我检查了其他类似的问题,例如堆栈溢出中的“MySQL 死锁”,但没有任何解决方案。
REPLACE INTO db2.table2 (id, some_identifier_id, name, created_at, updated_at) (SELECT id, some_identifier_id, name, created_at, updated_at FROM db1.table1 WHERE some_identifier_id IS NOT NULL AND some_identifier_id NOT IN (SELECT some_identifier_id FROM db2.table1 WHERE some_other_identifier_id IS NOT NULL));
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
Run Code Online (Sandbox Code Playgroud)
情况:
尝试:
最可能的原因可能是由于galera 集群背后的多主复制为其乐观锁定(http://www.severalnines.com/blog/avoiding-deadlocks-galera-set-haproxy-single-node-writes-and -多节点读取)。但是在单个节点上执行查询时应该不会失败?尽管成功后我必须在多主复制中执行相同的操作,但我想如果基本问题得到解决,那么复制的服务器将不再产生问题。
笔记:
我需要在没有任何临时表或在代码中存储子查询结果的情况下执行此操作。到目前为止,还有一些其他依赖项对其执行单个查询是最有利的方式。
好吧,我找到了解决这个问题的方法。根据我的研究和测试,我认为这次失败背后有两个问题。
Do not rely on auto-increment values to be sequential. Galera uses a mechanism based on autoincrement increment to produce unique non-conflicting sequences, so on every single node the sequence will have gaps. https://mariadb.com/kb/en/mariadb/mariadb-galera-cluster-known-limitations/
Galera Cluster uses at the cluster-level optimistic concurrency control, which can result in transactions that issue a COMMIT aborting at that stage. http://galeracluster.com/documentation-webpages/limitations.html
In a gist- query was running successfully in an individual server but when it's galera then the failure comes. Removal of the auto-incremental primary key from that query and handling the same transaction to restart on deadlock solved the problem.
[Edit]
I've written a post to explain the schema, environment, issue and how I worked with it. May be useful to someone facing the same issue.
The issue is reported to community and open