MySQL/migration/CLI/Import

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< MySQL‎ | migration‎ | CLI
Jump to navigation Jump to search
importing data into MySQL via command-line interface

These instructions assume you have a .sql file created by mysqldump, or something compatible with it.

Caveat: If you are restoring to a different database name (as might happen if you were moving the db to a new server), you will need to edit the .sql file to use the appropriate database name in the line which begins "CREATE DATABASE" and the "USE" line following it. If you know the database has already been created, you can just delete the CREATE DATABASE line.

You might also want to verify that the mysql utility connects successfully before you try the import:

mysql -h<server> -u<username> -p<password>

If that works, then the full command to create a database from the .sql file is:

mysql -h<server> -u<username> -p<password> < <path/to/backup/file.sql>

Note that the path is relative to the system on which you are running the mysql utility, not the system running the mysql server (mysqld). Note also that this technique doesn't tell you anything until it finishes processing the .sql file.

You can also explicitly specify the database in which the SQL commands should be run, presumably (I haven't tested this yet) overriding the USE line in the file:

mysql -h<server> -u<username> -p<password> -D<dbname> < <path/to/backup/file.sql>

Links

Official