Robbert Robbert - 1 month ago 8
MySQL Question

Importing database to Google Cloud SQL error: "mysql_query Duplicate entry '1' for key 'PRIMARY"

I am trying to import a (Wordpress) database, exported from a local XAMPP installation into a Google cloud SQL database.

I followed the steps from the Google documentation, so completed the following steps:


  1. I exported the .sql file with PHPmyadmin (tried only data and structure and data) from the local XAMPP
    installation.

  2. I created a Google Cloud storage bucket and uploaded my .sql file.

  3. I selected my instance and clicked on "import"

  4. Inserted the path to the file .sql file in the bucket

  5. And specified my database



Now I got the following error:

mysql_query Duplicate entry '1' for key 'PRIMARY' (INSERT INTO
`wp_comments` (`comment_ID`, `comment_post_ID`, `comment_author`,
`comment_author_email`, `comment_author_url`, `comment_author_IP`,
`comment_date`, `comment_date_gmt`, `comment_content`, `comment_karma`,
`comment_approved`, `comment_agent`, `comment_type`, `comment_parent`,
`user_id`) VALUES (1, 1, 'Mr WordPress', '', 'https://wordpress.org/', '',
'2015-07-11 09:58:54', '2015-07-11 09:58:54', 'Hi, this is a comment.\nTo
delete a comment, just log in and view the post's comments. There you will
have the option to edit or delete them.', 0, 'post-trashed', '', '', 0, 0))


I installed a wordpress database before the import, so maybe I need to remove the existing database, or leave it empty before uploading the new one? But it looks normally to me that you can upload an .sql file over an existing database, right?

Answer

You need to delete all tables (clear your database) and than import that .sql file. This error is occurring because you have already table of wp_comments and it contains an comment with that same ID.

Importing an SQL file, never replace data. It simply generate an error if duplicate record is going to happen as under primary key.

Comments