- GreatSQL社區原創內容未經授權不得隨意使用,轉載請聯系小編并注明來源,
- GreatSQL是MySQL的國產分支版本,使用上與MySQL一致,
- 作者:飛魚過天
- 文章來源:GreatSQL社區原創
- 問題
- 原因
- 故障解決方案
- 復現步驟
- 參考文獻
一、問題:
MySQL5.7.38主從架構,主節點唯一索引上(唯一索引不是主鍵)有重復值,全部從節點報1062,SQL執行緒狀態例外,根據SQL執行緒報的binlog位置點,insert 資料時有重復值,插入失敗
二、原因:
unique_checks=0時導致,在bug(106121)串列中官方解釋的原因:該引數關閉,維護唯一索引時,不會進行物理讀,只會進行記憶體讀,來確保唯一索引的唯一性,即如果記憶體中有沖突資料就報1062,如果記憶體中沒有沖突資料插入成功,不會進行io來將唯一索引相關的資料頁拉取到記憶體,
官方的回復“IMHO this is not a bug”,我理解的意思“不要你覺得,我要我覺得,我就是這么玩的”,
三、故障解決方案:
一、臨時解決方案
- 恢復主從:
- 在從節點開啟會話
- set sql_log_bin=0
- 洗掉表的唯一索引
- 重新啟動復制執行緒
缺點是:不能夠解決資料重復的問題,切換主從后會面臨更多重復資料的問題,如果從節點接收查請求且使用到了原唯一索引的欄位,那sql效率會嚴重下降,但是可以解決主從復制停止的問題
二、永久解決方案
- 業務自己去重,不要插入重復資料
- 引數unique_checks保持為1
- 關于重復的業務資料:與業務交流,確定重復資料的處理方式
四、復現步驟:
1. 表結構:
mysql> create database wl;
mysql> show create table wl.lgf\G
*************************** 1. row ***************************
Table: lgf
Create Table: CREATE TABLE `lgf` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`k` int(11) NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `c` (`c`,`pad`)
) ENGINE=InnoDB AUTO_INCREMENT=2147483647 DEFAULT CHARSET=utf8
Python生成隨機資料,插入表,并另起會話觀察總資料量約10w條左右(保證聚簇索引中的前邊的資料與后邊的資料所在的葉子節點的頁相差很遠):
rand.py
import random
import os
while True:
i=str(random.randint(1000,8000000))
a=str(random.randint(1000000000000000,8000000000000000))
b=str(random.randint(1000000000000000,8000000000000000))
c=str(random.randint(100000,800000))
sql="insert ignore into lgf(id,k,c,pad) values(%s,%s,%s,%s) " % (i,c,a,b)
os.system('mysql -uroot -p123456 -h127.0.0.1 -P3306 -e "use wl;%s"' % (sql))
2. 查詢資料:
查詢前10條資料:
mysql> select * from wl.lgf order by id limit 10;
+------+--------+------------------+------------------+
| id | k | c | pad |
+------+--------+------------------+------------------+
| 1058 | 162327 | 1693367460515515 | 4503256156555111 |
| 1072 | 581438 | 7079984640802065 | 3180334749170868 |
| 1139 | 160022 | 5072986485096872 | 4163430310554381 |
| 1193 | 780611 | 4790797228737408 | 2940698105313885 |
| 1234 | 395757 | 4904177529354516 | 4353197763651083 |
| 1243 | 725513 | 5525166443023382 | 5731401212245669 |
| 1262 | 749163 | 1132694876665847 | 5159069792931202 |
| 1280 | 415220 | 2770815803363126 | 3979264947141008 |
| 1316 | 329253 | 6088415865037450 | 6035685143204331 |
| 1360 | 403078 | 3344825394389018 | 7962994492618902 |
+------+--------+------------------+------------------+
10 rows in set (0.00 sec)
id=1360 c=3344825394389018 pad=7962994492618902
3. 拼接SQL
c與pad的值與id=1360值相等,id=1000000000(表中無該id行)
insert into wl.lgf(id,c,pad) values(10000000,'3344825394389018','7962994492618902') ;
4. 重啟mysqld
目的是清除快取 為了清空MySQL快取容,還可結合以下幾個引數 修改my.cnf檔案,重啟MySQL實體
- innodb_buffer_pool_load_at_startup = 0
- innodb_buffer_pool_dump_at_shutdown = 0
5. 重新插入重復唯一索引資料:
mysql> set unique_checks=0;
mysql> use wl
mysql> insert into wl.lgf(id,c,pad) values(10000000,'3344825394389018','7962994492618902') ;
Query OK, 1 row affected (0.00 sec)
6. 查詢:force index指定主鍵查詢資料
mysql> select * from wl.lgf force index(primary) where c='3344825394389018' and pad='7962994492618902';
+----------+--------+------------------+------------------+
| id | k | c | pad |
+----------+--------+------------------+------------------+
| 1360 | 403078 | 3344825394389018 | 7962994492618902 |
| 10000000 | 0 | 3344825394389018 | 7962994492618902 |
+----------+--------+------------------+------------------+
2 rows in set (0.37 sec)
參考檔案
MySQL Bugs: #106121: Unique key constraint invalid(https://bugs.mysql.com/bug.php?id=106121)
MySQL :: MySQL 8.0 Reference Manual :: 5.1.8 Server System Variables(https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_unique_checks)
Enjoy GreatSQL ??
關于 GreatSQL
GreatSQL是由萬里資料庫維護的MySQL分支,專注于提升MGR可靠性及性能,支持InnoDB并行查詢特性,是適用于金融級應用的MySQL分支版本,
相關鏈接: GreatSQL社區 Gitee GitHub Bilibili
GreatSQL社區:
社區博客有獎征稿詳情:https://greatsql.cn/thread-100-1-1.html

技術交流群:
微信:掃碼添加
GreatSQL社區助手微信好友,發送驗證資訊加群,

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/544237.html
標籤:其他
上一篇:ETL的系統核心特征
下一篇:mysql主從復制及分表分庫
