How to export and import mysql database to ignore Duplicate entries for key 'PRIMARY'?

436 Views Asked by At

I'm attempting to write a bash script that will dump a database and then import it to a staging database. I would like the staging database to match the 'master' database.

I have the following code, however I recieve:

ERROR 1062 (23000) at line 23: Duplicate entry '1' for key 'PRIMARY'

# Dump production master database, excluding school_hosts table
mysqldump -h $MYSQL_HOST -u $MYSQL_USERNAME -p$MYSQL_PASSWORD --no-create-info --ignore-table=hcl_master.school_hosts hcl_master > hcl_master.sql

# Dump hcl staging database, for backup.
mysqldump -h $MYSQL_HOST -u $MYSQL_USERNAME -p$MYSQL_PASSWORD hclstaging_master > hclstaging_master_backup.sql

# Import dump file into staging master database
mysql -h $MYSQL_HOST -u $MYSQL_USERNAME -p$MYSQL_PASSWORD hclstaging_master < hcl_master.sql

After searching, I found that I could add --replace to the mysql command that is importing, however I recieve an error stating that:

mysql: unknown option '--replace'

Can anybody help with getting this script to work correctly? I'm unsure how I can drop the staging database before i import or how to get it to overwrite the primary key record?

Any help would be much appreciated. I am using MariaDB.

1

There are 1 best solutions below

0
ysth On

--replace is a mysqldump option that you specify when creating the dump, not something you can tell mysql when importing the dump.