Import MySQL dumpfile, SQL datafile into my database

Q. How can I import a MySQL dumpfile into my database? My old hosting provider gave me data.sql file. I do have access via ssh to server. I'm using CentOS Linux 5 server.

A. You can easily restore or import MySQL data with mysql command itself. First you need to login to your system using ssh or putty (from Windows based system). For example:

Type the following command at the shell prompt:

$ ssh loginname@server.hosting.com

Now type following command to import sql data file:
$ mysql -u username -p -h localhost data-base-name < data.sql

If you have dedicated database server, replace localhost name with actual server name or IP address:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
OR use hostname such as mysql.hosting.com
$ mysql -u username -p -h mysql.hosting.com database-name < data.sql

If you do not know the database name or database name is included in sql dump you can try out something as follows:
$ mysql -u username -p -h 202.54.1.10 < data.sql

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 10 comments… read them below or add one }

1 Cameron James 10.13.06 at 4:31 pm

If you’re having troubles try removing the space between -u and the username (eg: -uusername), -p and the password (eg: -ppassword), -h and the host (eg: -h11.11.11.11)

2 Steven Richards 07.04.08 at 1:59 am

Worked like a charm! I did have to remove the spaces as Cameron suggested (thanks). I’m running Fedora Core 7.

3 mosh 08.12.08 at 5:10 pm

I’m so stupid ;)
I have decicated server and I was using all the time phpMyAdmin … that was terrible….

now I import DB in 2 sek :D

THX a lot for this very important for me command :)

Greetings, mosh

4 Cephalex 11.04.08 at 10:45 pm

if you want to import an UTF8 database you can use this command

$mysql -u databaseuser -p -h localhost –default-character-set=utf8 databasename < sqlfile.sql

5 Maple Syrup 01.16.09 at 8:55 am

Thanks to Cameron James. Removing the spaces did it for me as well. Fought with that sucker for almost 4 hours…

6 kiran and niraj 02.05.09 at 10:17 am

thanx a lot for this script. and thanks cephalex for utf8 option.

7 lady 02.13.09 at 1:24 am

can anyone help me how to start using php in linux?i already installed it but i do not know how to run it using command line.
and one more thing how to convert data from pcap to mysql?
please give me a hint on how to start the program

8 fmuteam 03.24.09 at 5:27 pm

Very nice tut ! I just moving from another host to a new one. This commands works perfectly :)

9 Nordes 06.17.09 at 2:33 am

Thx a lot ;) Very usefull tips

10 Brandon Broga 06.29.09 at 5:18 pm

If Your Using Red Hat Linux,
take the $ out.

and try this,

mysql -u username -p -h localhost data-base-name < data.sql

Thanks

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , ,

Previous post: Apache 403 Forbidden Error and Solution

Next post: Fedora core Linux sound card configuration