MySQL Slave: Duplicate entry for key ‘PRIMARY’ Error_code: 1062

In this article, we will see the solution for MySQL Slave: Duplicate entry for key ‘PRIMARY’ Error_code: 1062. Also MySQL error code 1054.

The problem for MySQL Slave: Duplicate entry

So we are getting MySQL error code 1062 which is also known as “MySQL slave Duplicate entry”. We got connected with the MySQL Master server but due to duplication, there was an error.

Due to an error, our slave server stopped working. We can fix that duplicate entry from the master and then set up the replication again. But what if we have millions of records with duplicate entries.

mysql error code 1062, mysql slave duplicate entry, mysql slave skip errors.

MySQL ERROR: 2020-03-22 14:43:43 28065 [Warning] Slave: Duplicate entry ‘1234’ for key ‘PRIMARY’ Error_code: 1062
2020-03-22 14:43:43 28065 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with “SLAVE START”. We stopped at log ‘mysql-bin-changelog.000015’ position 124752018.

We had taken a dump from the Master database and restored it to the slave server. It is possible that Master Server has duplicate entries, but it won’t work in the slave server.


Solution for MySQL Slave: Duplicate entry

So we are going to make changes to the configuration file for the MySQL Slave server. You can find the file in the /etc directory, in my case I have a file name as “my.cnf”.

# vim /etc/my.cnf

[mysqld]

slave-skip-errors=1062
skip-slave-start

We have used vim editor to edit the /etc/my.cnf file, you can use the editor of your choice. Then save and Quit the file using “:wq”. Restart the MySQL service by using the below command.

# sudo service mysql restart

Now if we get an error code 1062 in the MySQL Slave server, it will skip the duplicate query and move to the next entry.

Suppose you want to skip the 2 error codes 1062 and 1054 then use a comma. Make the changes in the configuration file and restart the MySQL service and start the slave.

slave-skip-errors=1062, 1054

Save the changes you made in the configuration file and use “sudo service mysql restart”.


What is MySQL error code 1054?

Let’s suppose you want to insert the data into the table by using the below query.

>insert into example values(1,tastethelinux);

It gives me the error Unknown column ‘tastethelinux’ in the field list. The above query contains the string “tastethelinux” and we have not used the quotes.

So use the correct insert query in a single quote as below.

>insert into example values(1,'tastethelinux');

Conclusion

In this article, We have solved the MySQL error code 1062 Duplicate entry. Also, we have seen why error 1054 comes in MySQL. We have solved issues for the MySQL replication like 1298 Slave incorrect timezone, and 1594 Relay log read failure. What steps you should follow when you change the Master host. Any issues or feedback please let us know in the comment section.


Give your valuable time