Here at Bobcares, we provide Server Administration and Maintenance services to website owners and web solution providers.
An error we sometimes see in MySQL servers while updating, restoring or replicating databases is: “Error No: 1062” or “Error Code: 1062” or “ERROR 1062 (23000)“
A full error log that we recently saw in a MySQL cluster is:
could not execute Write_rows event on table mydatabasename.atable; Duplicate entry ’174465′ for key ‘PRIMARY’, Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event’s master log mysql-bin.000004, end_log_pos 60121977
What is MySQL Error No: 1062?
Simply put, error 1062 is displayed when MySQL finds a DUPLICATE of a row you are trying to insert.
We’ve seen primarily 4 reasons for this error:
- The web application has a bug that adds primary key by large increments, and exhausts the field limit.
- MySQL cluster replication tries to re-insert a field.
- A database dump file contains duplicate rows because of coding error.
- MySQL index table has duplicate rows.
In rare cases, this error is shown when the table becomes too big, but let’s not worry about that for now.
How to fix Error No 1062 when your web appilcation is broken
Every database driven application like WordPress, Drupal or OpenCart distinguishes one user or data set from another using something called a “primary field”.
This primary field should be unique for each user, post, etc.
Web apps use a code like this to insert data:
INSERT INTO table ('id','field1','field2','field3') VALUES ('NULL','data1','data2','data3');
Where “id” is the unique primar key, and is set to auto-increment (that is a number inserted will always be greater than the previous one so as to avoid duplicates).
This will work right if the value inserted is “NULL” and database table is set to “auto-increment”.
Some web apps make the mistake of passing the value as
VALUES ('','data1','data2','data3');
where the first field is omitted. This will insert random numbers into the primary field, rapidly increasing the number to the maximum field limit (usually 2147483647 for numbers).
All subsequent queries will again try to over-write the field with “2147483647”, which MySQL interprets as a Duplicate.
Web app error solution
When we see a possible web application code error, the developers at our Website Support Services create a patch to the app file that fixes the database query.
Now, we have the non-sequential primary key table to be fixed.
For that, we create a new column (aka field), set it as auto-increment, and then make it the primary key.
The code looks approximately like this:
alter table table1 drop primary key;
alter table table1 add field2 int not null auto_increment primary key;
Once the primary key fields are filled with sequential values, the name of the new field can be changed to the old one, so that all web app queries will remain the same.
Warning : These commands can get very complex, very fast. So, if you are not sure how these commads work, it’s best to get expert assistance.
How to fix MySQL replication Error Code : 1062
Due to quirks in network or synching MySQL is sometimes known to try and write a row when it is already present in the slave.
So, when we see this error in a slave, we try either one of the following depending on many factors such as DB write traffic, time of day etc.
- Delete the row – This is the faster and safer way to continue if you know that the row being written is exactly the same as what’s already present.
- Skip the row – If you are not sure there’d be a data loss, you can try skipping the row.
How to delete the row
First delete the row using the primary key.
delete from table1 where field1 is key1;
Then stop and start the slave:
stop slave;
start slave;
select sleep(5);
Once it is done, check the slave status to see if replication is continuing.
show slave status;
If all is well, you’ll see “Seconds_Behind_Master” as a number. If not, your replication is broken and it needs to be fixed.
How to skip the row
For this, you can set the Skip counter to 1.
Here’s how it could look like:
stop slave;
set global SQL_SLAVE_SKIP_COUNTER = 1;
start slave;
select sleep(5);
Then check the slave status to see if replication is continuing.
show slave status;
Again, if all is well, you’ll see “Seconds_Behind_Master” as a number. If not, your replication is broken and it needs to be fixed.
Proceed with caution
Stopping and starting the slave cannot cause any issue unless you havea very busy database. But, the delete statement, skipping and following up with a broken replication requires expert knowledge about MySQL organization and functioning.
If you are not sure how these commands will affect your database, we recommend you talk to a DB administrator.
How to fix MySQL restore errors
Restore errors usually take the form of:
ERROR 1062 (23000) at line XXXX: Duplicate entry ‘XXXXXX’ for key X”
When restoring database dumps, this error can happen due to 2 reasons:
- The SQL dump file has dulpicate entries.
- The index file is duplicate rows.
To find out what is exactly going wrong, we look at the conflicting rows and see if they have the same or different data.
If it’s the same data, then the issue could be due to duplicate index rows. If it is different data, the SQL dump file needs to be fixed.
How to fix duplicate entries in database dumps
This situation can happen when two or more tables are dumped into a single file without checking for duplicates.
To resolve this, one way we’ve used is to create a new primary key field with auto-increment and then change the queries to insert NULL value into it.
Then go ahead with the dump.
Once the new primary field table is fully populated, the name of the field is changed to the old primary table name to preserve the old queries.
The alter table command will look like this:
alter table table1 change column 'newprimary' 'oldprimary' varchar(255) not null;
If your index file is corrupted
There’s no easy way to fix an index file if there are duplicate entries in it.
You’ll have to delete the index file, and restore that file either from backups or from another server where your database dump is restored to a fresh DB server.
The steps involved are quite complex to list out here. We recommend that you consult a DB expert if you suspect the index file is corrupted.
Summary
MySQL error no 1062 can occur due to buggy web applications, corrupted dump files or replication issues. Today we’ve seen the various ways in which the cause of this error can be detected, and how it can be resolved.
i’ve pasted the script for skipping the error, but pluses make sure that you contact the stake holder of the database about htis.
###
#!/bin/bash
while true; do
duplicate_count=$(mysql –defaults-file=~/.my.cnf -e”show slave status \G;” | grep “Duplicate entry” | wc -l)
if [ “$duplicate_count” -ge 1 ]; then
echo “Duplicate entry found. Skipping the entry…”
mysql –defaults-file=~/.my.cnf -e”stop slave; set global sql_slave_skip_counter=1; start slave;”
fi
sleep 5 # Adjust sleep duration as needed
mysql –defaults-file=~/.my.cnf -e”show slave status\G”
done
####
Note: pleasereview the status of the salve
Hello,
Our experts are available to assist you with your concerns. We would be delighted to discuss this with you via our live chat feature. Simply click on the icon located in the bottom right corner to get started.