删除DELETE FROM上的连接,而不是SELECT

时间:2016-01-25 18:12:49

标签: mysql innodb mariadb mysql-error-2006 mysql-error-2013

我正在尝试记录特定成员,如果它不再在另一个表中有子记录。当我提交DELETE FROM时,我收到一个丢失连接或MySQL服务器已经消失。如果我从数据库中选择相同的记录,我会得到有效的回报。我已经在下面进行了互动:

SELECT m.* FROM Member m LEFT OUTER JOIN MemberAssociation ma ON m.memberID = ma.memberID WHERE m.scanCode = 12345 AND ma.associationCode IS NULL;
/* The expected Member records, which appear to be valid.  2 Rows */

DELETE m.* FROM Member m LEFT OUTER JOIN MemberAssociation ma ON m.memberID = ma.memberID WHERE m.scanCode = 12345 AND ma.associationCode IS NULL;
ERROR 2013 (HY000): Lost connection to MySQL server during query

DELETE m.* FROM Member m LEFT OUTER JOIN MemberAssociation ma ON m.memberID = ma.memberID WHERE m.scanCode = 12345 AND ma.associationCode IS NULL;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    2
Current database: my_database

ERROR 2013 (HY000): Lost connection to MySQL server during query

有可能我可以做些什么来解决这个问题,还是我偶然发现了软件错误?

我正在运行innodb_version:5.6.19-67.0和MariaDB版本:10.0.13-MariaDB。 Member表是InnoDB表。

2 个答案:

答案 0 :(得分:1)

请参阅MySQL:MariaDB了解发生这种情况的背景信息。

A related SO query也讨论了同一个问题。

基本上,看起来您的DELETE查询可能花费的时间太长而且MySQL服务器 - 客户端连接超时。

编辑基于OP的响应

根据this链接,损坏的Innodb表也会导致此问题。有关诊断和故障排除过程,请参阅以下2个链接:

答案 1 :(得分:0)

我自己已经解决了这个问题,并且对将来遇到类似事情的人提出了一些建议。

就我而言,由于表损坏,MySQL失败了。我使用CHECK TABLE工具找到了错误。

CHECK TABLE Member;
+-------------------------+-------+----------+----------+
| Table                   | Op    | Msg_type | Msg_text |
+-------------------------+-------+----------+----------+
| my_database.Member | check | status   | OK       |
+-------------------------+-------+----------+----------+

CHECK TABLE MemberAssociation;
ERROR 2013 (HY000): Lost connection to MySQL server during query

然后我尝试复制数据,但由于PRIMARY KEY问题而无法解决:

CREATE TABLE MemberAssociation_recover LIKE MemberAssociation

INSERT INTO MemberAssociation_recover SELECT * FROM MemberAssociation;
ERROR 1062 (23000): Duplicate entry 'XXXXX-YYYYY-ZZZZ' for key 'PRIMARY'
显然有些不对劲。

此时我做了一个mysqldump,发现MemberAssociation表中的大多数行都有重复。因为这是一个开发数据库,​​所以我通过以下过程恢复了数据:

CREATE TEMPORARY TABLE MemberAssociationIgnore LIKE MemberAssociation;
INSERT IGNORE INTO MemberAssociationIgnore SELECT * FROM MemberAssociation;
DELETE FROM MemberAssociation;
INSERT INTO MemberAssociation SELECT * FROM MemberAssociationIgnore;

注意:在生产系统上,我不会遵循上述程序。我建议将数据从损坏的表中复制到一个没有密钥的新表中,并在应用删除的密钥然后将数据复制回原始表之前手动清除任何重复项。