On Salve
MySQL Replication ERROR 1062: Duplicate Entry for Primary Key when starting Slave6 min read
October 3, 2020 Praseeb K Das
PRASEEB K DAS AUTHOR
Devops Engineer
Sorry! The Author has not filled his profile.
Webhttps://*Emails are not allowed*all9446237102
FOLLOW ME
In this blog,we will see how to resolve the MySQL Replication ERROR 1062 : Duplicate Entry for Primary Key when starting Slave.
Here we will skip the MySQL slave error.
ct 2 08:13:36 mysqld: 2020-10-02 8:13:36 608 [ERROR] Slave SQL: Error 'Duplicate entry '6352107' for key 'PRIMARY'' on query. Default database: 'databasename_db'. Query: 'insert into IA_INS_EVENT_ACT (CREATED_BY, CREATED_ON, LAST_MODIFIED_BY, LAST_MODIFIED_ON, TENANT_ID, ACT_STATE, BATCH_ID, IS_DELETED, DESCRIPTION, GROUP_MAIL_STATUS, INGHT_ID, LOG, SNAPSHOT_ID, STATUS, THREAD_ID, TRIGGER_TYPE, USER_ID) values ('SYS_ADMIN_INGHT', '2020-09-25 11:35:01.131', 'SYS_ADMIN_INGHT', '2020-09-25 11:35:01.131', 6, 'ENQUEUED', '1133_1601033701127', 0, '', 0, 1133, '', NULL, 1, NULL, 'PERCEPTION', 14767)', Gtid 0-2-73099, Internal MariaDB error code: 1062
Oct 2 08:13:36 mysqld: 2020-10-02 8:13:36 608 [Warning] Slave: Duplicate entry '6352107' for key 'PRIMARY' Error_code: 1062
Oct 2 08:13:36 mysqld: 2020-10-02 8:13:36 608 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'master2-bin.000056' position 14905
Oct 2 08:13:36 mysqld: 2020-10-02 8:13:36 605 [Note] Error reading relay log event: slave SQL thread was killed
Oct 2 08:13:36 mysqld: 2020-10-02 8:13:36 605 [Note] Slave SQL thread exiting, replication stopped in log 'master2-bin.000056' at position 15350
This error,will be raising after successfully establishing MySQL Master and Slave Connection and got error like “MySQL slave duplicate entry with MySQL error code 1062”
When we take dump from Master database,there will be possibility that our Master Database has duplicate entries,but it wont work in the slave.Hence we have to make one change in Configuration file of MySQL “my.cnf”
vim /etc/my.cnf
[mysqld]
slave-skip-errors=1062
skip-slave-start
Save and quit the file by using :wq and then restart the service by using the following command
#sudo service mysql restart / service mariadb restart
This means,if the slave host returns the error code 1062,it will skip that query and proceed further.
Once we restart restart mysql/mariadb service.Then login to mysql and run below command.
START SLAVE;
Now all the duplicate-key errors will get bypassed.
Now lets execute below command to view the Slave status.