1

So I wanted to format my system and I had a lot of works that I have done on my localhost that involves databases. I followed the normal way of backing up the database by exporting it into an SQL file but I think I made a mess by making a mistake of backing up everything in one SQL file (I mean the whole localhost was exported to just one SQL file).

The problem now is: when I try to import the backed up file I mean the (localhost.sql), I get an error like

tables already exist.

information_schema performance_schema

an every other tables that comes with Xampp, which has been preventing me from importing the database.

These tables are the phpmyadmin tables that came with Xampp. I have been trying to get past this for days.

My question now is that can I extract different databases from the same compiled SQL database file?

8
  • 2
    PLEASE DO NOT YELL! Feb 28, 2017 at 22:29
  • You shouldn't be restoring the mysql database, only the one with your application data in it.
    – tadman
    Feb 28, 2017 at 22:36
  • @AaronBertrand it's MySQL Feb 28, 2017 at 22:44
  • @JayBlanchard what are you talking about? Feb 28, 2017 at 22:44
  • 1
    Then please don't tag with sql-server Feb 28, 2017 at 22:50

4 Answers 4

6

To import a database you can do following things:

mysql -u username -p database_name < /path/to/database.sql

From within mysql:

mysql> use database_name;
mysql> source database.sql;
3

The error is quite self-explanatory. The tables information_schema and performance_schema are already in the MySQL server instance that you are trying to import to.

Both of these databases are default in MySQL, so it is strange that you would be trying to import these into another MySQL installation. The basic syntax to create a .sql file to import from the command line is:

$ mysqldump -u [username] -p [database name] > sqlfile.sql

Or for multiple databases:

$ mysqldump --databases db1 db2 db3 > sqlfile.sql

Then to import them into another MySQL installation:

$ mysql -u [username] -p [database name] < sqlfile.sql

If the database already exists in MySQL then you need to do:

$ mysqlimport -u [username] -p [database name] sqlfile.sql

This seems to be the command you want to use, however I have never replaced the information_schema or performance_schema databases, so I'm unsure if this will cripple your MySQL installation or not.

So an example would be:

$ mysqldump -uDonglecow -p myDatabase > myDatabase.sql
$ mysql -uDonglecow -p myDatabase < myDatabase.sql

Remember not to provide a password on the command line, as this will be visible in plain text in the command history.

4
  • thanks for comment, but I really tried all that is listed above but it's not working. I think I need to explain myself better. I have databases on my localhost and I backed it up on my system by exporting all the localhost database including the ones that came with mysql and the the ones that came with xampp. The issue I am having now is when I tried to import that backed up databaes(localhost.sql) i get an error that tables already exist. Following your solution I get error 1146 i.e I still can't import the file that contains my databases. I am stuck! Mar 6, 2017 at 15:42
  • You could try spinning up a VM and installing MySQL on there. Try importing the database and manually drop those tables - though if you're on a fresh installation with nothing else on the MySQL installation, it can't hurt to try on your main one and reinstall it if it breaks!
    – Donglecow
    Mar 7, 2017 at 16:29
  • Please how do I do that? Mar 15, 2017 at 17:58
  • Depending on which Operating System you're using, and what your environment is. First (regardless of OS), install Virtual Box on your machine. Install Xubuntu (assuming this is part of your stack) on a virtual machine through Virtual Box. Some help can be found on youtube. Windows installation or Linux installation. That should cover most bases. If you're still struggling, let me know. The process for Xubuntu should be similar to regular Ubuntu.
    – Donglecow
    Mar 16, 2017 at 8:44
1

The point the previous responders seem to be missing is that the dump file localhost.sql when fed into mysql using

% mysql -u [username] -p [databasename] < localhost.sql

generates multiple databases so specifying a single databasename on the command line is illogical.

I had this problem and my solution was to not specify [databasename] on the command line and instead run:

% mysql -u [username] -p < localhost.sql

which works.

Actually it doesn't work right away because of previous attempts which did create some structure inside mysql, and those bits in localhost.sql make mysql complain because they already exist from the first time around, so now they can't be created on the second time around.

The solution to THAT is to manually edit localhost.sql with modifications like INSERT IGNORE for INSERT (so it doesn't re-insert the same stuff, nor complain), CREATE DATABASE IF NOT EXISTS for CREATE DATABASE, CREATE TABLE IF NOT EXISTS for CREATE TABLE, and to delete ALTER TABLE commands entirely if they generate errors because by then they've already been executed ((and INSERTs and CREATEs perhaps too for the same reasons). You can check the tables with DESCRIBE TABLE and SELECT commands to make sure that the ALTERations, etc. have taken hold, for confidence.

My own localhost.sql file was 300M which my favorite editor emacs complained about, so I had to pull out bits using

% head -n 20000 localhost.sql | tail -n 10000 > 2nd_10k_lines.sql

and go through it 10k lines at a time. It wasn't too hard because drupal was responsible for an enormous amount, the vast majority, of junk in there, and I didn't want to keep any of that, so I could carve away enormous chunks easily.

0
unzip -p /pathoffile/database_file.zip | mysql -uusername -p databsename;

Best way to import database in localhost has simple 5 steps:

  1. zip sql file first to compress databse size.
  2. go to termianl.
  3. create empty database.
  4. Run Command unzip databse With Import database: unzip -p /pathoffile/database_file.zip | mysql -uusername -p databsename;
  5. Enter Password

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.