我有以下表结构(SHOW CREATE
刚刚使用命令):
CREATE TABLE `ipstats` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`ip` VARCHAR(15) NOT NULL,
`online` ENUM('n','y') NOT NULL DEFAULT 'y',
`last_used` DATETIME NOT NULL DEFAULT '1981-09-30 00:00:00',
PRIMARY KEY (`id`),
UNIQUE INDEX `ip` (`ip`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
AUTO_INCREMENT=253691;
现在,我只是获取一些最近的值(我已经更改了 IP 以进行演示):
mysql> SELECT * FROM ipstats ORDER BY id DESC LIMIT 10;
+--------+----------------+--------+---------------------+
| id | ip | online | last_used |
+--------+----------------+--------+---------------------+
| 253690 | 10.204.102.38 | n | 2013-10-19 14:14:33 |
| 253689 | 10.188.124.196 | n | 2013-10-19 10:46:25 |
| 253688 | 10.166.124.194 | n | 2013-10-19 16:49:40 |
| 253687 | 10.250.137.166 | n | 2013-10-19 13:51:56 |
| 253686 | 10.221.102.39 | n | 2013-10-19 14:13:03 |
| 253685 | 10.129.102.57 | n | 2013-10-19 18:45:20 |
| 253684 | 10.214.102.39 | n | 2013-10-19 03:43:55 |
| 253683 | 10.31.142.41 | n | 2013-10-19 17:27:08 |
| 253682 | 10.41.142.154 | n | 2013-10-19 00:52:11 |
| 253681 | 10.41.124.84 | n | 2013-10-19 10:37:12 |
+--------+----------------+--------+---------------------+
在此之后,我将尝试执行一个简单的INSERT
语句:
INSERT INTO `ipstats` (`ip`, `last_used`)
VALUES ( '10.3.100.244', NOW() )
ON DUPLICATE KEY UPDATE
`online` = 'y',
`last_used` = NOW(),
`id` = LAST_INSERT_ID(`id`)
该值10.3.100.244
在表中尚不存在。结果是:
ERROR 1062 (23000): Duplicate entry '253691' for key 'PRIMARY'
选择LAST_INSERT_ID
给出:
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 0 |
+------------------+
如果那还不是奇怪的行为;我将尝试插入一个已经存在的 IP 条目:
mysql> INSERT INTO `ipstats` (`ip`, `last_used`) VALUES ( '10.204.102.38', NOW() ) ON DUPLICATE KEY UPDATE `online` = 'y', `last_used` = NOW(), `id` = LAST_INSERT_ID(`id`);
ERROR 1062 (23000): Duplicate entry '253691' for key 'PRIMARY'
我现在对 MySQL 的这种行为感到困惑。所有其他查询在所有其他正在运行的进程中工作正常。仅当我尝试到表时才会发生INSERT
错误ipstats
。我什至尝试了以下方法:
mysql> UPDATE ipstats
SET `online` = 'n',
`last_used` = NOW(),
`id` = LAST_INSERT_ID(`id`)
WHERE ip = '10.204.102.38';
Affected rows: 1 Found rows: 0 Warnings: 0 Duration for 1 query: 0.000 sec.
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 253690 |
+------------------+
Affected rows: 0 Found rows: 1 Warnings: 0 Duration for 1 query: 0.000 sec.
mysql> INSERT INTO `ipstats` (`ip`, `last_used`)
VALUES ( '10.204.102.38', NOW() )
ON DUPLICATE KEY UPDATE
`online` = 'y',
`last_used` = NOW(),
`id` = LAST_INSERT_ID(`id`);
Error 1062 (23000): Duplicate entry '253691' for key 'PRIMARY'
可能是什么问题呢?我该如何解决这个问题?该错误从今天下午开始出现,我以前从未遇到过这样的问题。
PS:我的 MySQL 安装中的所有用户帐户都会发生错误。有 4 个用户(包括 root)可以访问数据库。他们都无法执行INSERT
查询。