Import MySQL Dumpfile, SQL Datafile Into My Database

by Vivek Gite on August 23, 2006 · 53 comments

How can I import a MySQL dumpfile into my database? I'm using CentOS Linux 5 server. My old hosting provider gave me a data.sql file and I do have access to my Unix / Linux server via ssh. So How do I restore my data using command line over the ssh session?

You can easily restore or import MySQL data with the mysql command itself. First you need to login to your system using ssh or putty client.

Step #1: Upload File To MySQL Server

You can upload data.sql file using the sftp or scp command, enter:
$ scp data.sql vivek@example.cyberciti.biz:/home/vivek
The data.sql file will be uploaded to /home/vivek directory. Avoid using /tmp or Apache document directory such as /var/www/html as anyone can see your data on the remote server.

Step #2: Login To Remote Server

Type the following command at the shell prompt:
$ ssh loginname@example.cyberciti.biz
Replace example.cyberciti.biz with actual server name or an IP address.

Step#3: Import Datafile

Type the following command to import sql data file:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
In this example, import 'data.sql' file into 'blog' database using vivek as username:
$ mysql -u vivek -p -h localhost blog < data.sql

If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
OR use hostname such as mysql.cyberciti.biz
$ mysql -u username -p -h mysql.cyberciti.biz 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

A Note About Creating A New Database and Importing Data

In this example create a mysql database called foo and import data from bar.sql.gz as follows:

mysql -u root -p -h localhost

Sample outputs:

mysql> create database foo;
mysql> exit;

Import bar.sql.gz:

gunzip bar.sql.gz
ls -l
mysql -u root -p -h localhost foo <bar.sql

You can also create a username and password for foo database using the following syntax:

 
mysql -u root -p -h localhost
 

Sample outputs:

mysql> GRANT ALL ON foo.* TO NEW-USERNAME-HERE@localhost IDENTIFIED BY 'YOUR-PASSWORD-HERE';
### allow access from 192.168.1.5 too ##
mysql> GRANT ALL ON foo.* TO NEW-USERNAME-HERE@192.168.1.5 IDENTIFIED BY 'YOUR-PASSWORD-HERE';
mysql> quit;

Page last updated at 4:37 PM, January 6, 2012.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 53 comments… read them below or add one }

1 Cameron James October 13, 2006

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)

Reply

2 Steven Richards July 4, 2008

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

Reply

3 mosh August 12, 2008

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

Reply

4 John December 4, 2011

Same here. phpmyadmin needed 10 mins. And now…well below 3 seconds. So awesome.

Reply

5 Cephalex November 4, 2008

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

Reply

6 Maple Syrup January 16, 2009

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

Reply

7 kiran and niraj February 5, 2009

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

Reply

8 lady February 13, 2009

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

Reply

9 fmuteam March 24, 2009

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

Reply

10 Nordes June 17, 2009

Thx a lot ;) Very usefull tips

Reply

11 Brandon Broga June 29, 2009

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

and try this,

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

Thanks

Reply

12 Bhargav July 28, 2009

tnx … easy to understanc

Reply

13 Sandeep Tagore July 30, 2009

go to mysql folder and execute this command:
[root@localhost mysql]# bin/mysql [Database Name] < /home/sandeep/software/[file-name].sql

Reply

14 Prashant October 13, 2010

[root@localhost' is not recognized as an internal or external command,
perable program or batch file.

Reply

15 Luis Santana November 7, 2011

/bin/mysql [Database Name] < /home/sandeep/software/[file-name].sql

That is the proper command

Reply

16 hammad September 4, 2009

I installed oracle 11g, but I want to know who do I create database table in it and also describe me about how to type command to see the database table in Sql.

Reply

17 bhavesh October 23, 2009

I want to add a “login” feature to my website, that consists of an email address, password and a unique user ID. The feature should also be able to store the names, send an email verification reply, provide for emailing the user for forgotten passwords, etc.

plz replay me on my mail id.

I am totally new at mysql data base and php script.

bksondagar

Reply

18 hary December 30, 2009

thx a lot, it’s very helpfull for me.

Reply

19 Wild_Bill January 5, 2010

Thanks alot for these. Its helped me out of a big jam that I was in..

Reply

20 Aashish January 8, 2010

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

the above statement running but tables are not created in database

Reply

21 Aashish January 8, 2010

mysql -u username -p data-base-name < data.sql
gives error , can u help me
ERROR 1227 (42000) at line 500: Access denied; you need the SUPER privilege for this operation on win

Reply

22 sasikala January 27, 2010

How can I import a MySQL dumpfile into my database?
am using windows..

Reply

23 Trajcevski Filip September 12, 2011

You have to get the mySql dump file.
Option 1:
-Copy the dump file to MySql instalation directory\MySql Server x.x\bin
-Open command prompt, and go to MySql instalation directory\MySql Server x.x\bin directory.
-Run “mysql -uroot -ppassword databaseName < dumpFileName.sql"
IMPORTANT: dont leave spaces between -u and user or -p and password…
Option 2:
-add mysql from MySql instalation directory\MySql Server x.x\bin to system enviorment variables
-Open command prompt and go to the directory where your dump file is
-Run "mysql -uroot -ppassword databaseName < dumpFileName.sql"
IMPORTANT: dont leave spaces between -u and user or -p and password…

Reply

24 sascha February 7, 2010

yeah, everywhere instructions how to import sql file when it is stored in web server, but nowhere when it is stored in desktop. absolutly no one give us command how to upload stupid mysql file from desktop to web server without phpmyadmin.
I use Joomla 1.0.x CMS, and they have plugin to export but there is no import button in plugin and I don’t have command.

Reply

25 Vivek Gite February 8, 2010

Upload file to your server using ftp or sftp client. Once uploaded run above mysql command.

Reply

26 Don Carroll July 16, 2010

OK, nearly there, so thanks to the author and those who followed up in the comments. One question, though–how to add my sql dump (call it create.sql) to a specific table in the DB?

I have four existing tables in the database and don’t need/want to change them. I also have a newly created table (call it $xx_table) in which I wish to dump the data from create.sql. Any ideas?

Reply

27 Vivek Gite January 6, 2012

The commands are same. Let us say your table name is page, database name is wikidb and dump file is called page.sql. To import page.sql into wikidb.page table type:

mysql -u root -h localhost -p wikidb < page.sql

The above command will delete all data from page table and will import a new set of data from page.sql.

Hope this helps!

Reply

28 sascha February 18, 2011

here is detailed instruction:)
I had to upload rdesign.sql file to my root folder at server, then I had to find PATH for it, it is usually written in FTP client (I use WinSCP), path is this srv/www/virtual/blabla, instead of blabla write your domain name. instead of localhost rdesign write localhost your database name. so:

mysql -u yourdatabaseusername -p -h localhost rdesign < /srv/www/virtual/org.blabla.blabla/rdesign.sql

after that I had to type password for database, and job is finished.

my problem is that I don't have phpmyadmin and server upload is limited to 5MB (therefore I can't use Joomla mysql plugins) and my database file is 9MB, therefore I had to use Putty.

Reply

29 dibish July 22, 2010

you did a great job dear……..its worked ……..thanks a lot …………

Reply

30 sqldumb August 17, 2010

Thanks man, that tip saved me lots of frustrations.

Reply

31 Jayashree August 24, 2010

can anybody know how to import vhdl(x-vhdl) document which is mysql dump file i have downloaded from server into mysql database ?

Reply

32 DarkCoder November 10, 2010

For me (xampp on Xp) this worked:

mysql -uusername -ppassword -hlocalhost data-base-name < data.sql

Note: your *.sql file must be in /xampp/

Reply

33 glenbot December 9, 2010

You only really need to remove the space from the -p option.

mysql -u user -ppassword dabatase < database.sql

Reply

34 feylip December 10, 2010

or just omit the password altogether and let mysql prompt you for it ie
mysql> -u user -p <database.sql

then enter password at prompt

Reply

35 binu January 10, 2011

how can i import php db into linux.
i am using ubuntu.

Reply

36 Andy Konecny January 15, 2011

The only thing missing is how to tell mysql where to put the extract of the dump file.
I already have space assigned for my database, but it isn’t /var/lib/mysql where it keeps trying to extract it so I run out of room on root. The mount point with lots of room is in the same path as where the database was on the old box and the –datadir also points to that mount point.

Reply

37 Andy Konecny February 21, 2011

follow up
the command “mysql_install_db –datadir=”
and related command line instructions are what had it dumping the databases in the old home
properly defining the location in the my.conf file ‘broke’ mysql all together in this instance (couldn’t read .tmp, or create/write to just some files in the destination, though it did create a bunch)

Root cause: assuming that mysql could work in a proper security model of rights assignments. While it had full rights to the directory within the mount point, no rights of any sort (=000) had been assigned at the mount point. I haven’t tested what the minimum rights are, but clearly they are well beyond what should be needed as from a security point of view, the intermediate directories should not require any rights. Novell has had this proper model working in NetWare for decades, why can’t we do this in the Linux world?

Reply

38 vikas gautam February 23, 2011

thank for hte information but where is the script

Reply

39 Farshid March 30, 2011

Thanks. This was very helpful. A quick reminder.
Do keep up the good work.

Reply

40 Ejth May 19, 2011

DO NOT direct the contents of a large SQL dump to mysql with like in this article:
mysql -u username -p -h localhost data-base-name < data.sql

This will probably fail if your database is huge (like mine, its about 1.5gb)

instead use this approach:

mysql -h localhost -u [mysqlusername]
Enter password: [your pass]
use database_name
source dump.sql

This will handle huge files with no problems.

Reply

41 Sean P. O. MacCath-Moran October 8, 2011

Thanks Ejth – I was scouring the web for a large file solution and had completely forgot about this approach; your refresher was timely and HUGELY helpful. =o)

Reply

42 bunty May 26, 2011

i used “mysql -u username -p pass databasename < file_new.sql"

and found error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near..

please help

Reply

43 mtg169 May 31, 2011

use mysql -u username -p databasename <file_new.sql
That will prompt you for the password and is more secure as the password will not be stored in plain-text in your bash history. If you want to supply the password, simply remove the space after -p and the the password, so -pyourpassword.

Reply

44 mohsen September 3, 2011

Thank you for the guidance.
It seems what really works is
mysql -u user -ppassword
and not
mysql -u user -p password

I hope the author can change the initial document. It took me about 30 minutes to understand I should remove the space

Reply

45 joomlavui September 8, 2011

ERROR 1153 (08S01) at line 17005: Got a packet bigger than ‘max_allowed_packet’ bytes

Reply

46 EJTH September 8, 2011

use this approach instead, as i mentioned in my previous post the method mentioned in this article has problems with huge imports:

mysql -h localhost -u [mysqlusername]
Enter password: [your pass]
use database_name
source dump.sql

Reply

47 florin September 27, 2011

Just wanna thank EJTH – only his method worked for me.
Thanks,

Reply

48 magi September 13, 2011

How can I make a sql file, where is stored my database, using mysql command line?
Thanks.

Reply

49 Rajgopal H.G. September 22, 2011

Thanks a lot. I was missing the -h in the command
$ mysql -u username -p -h localhost data-base-name < data.sql

Reply

50 Justin DoCanto January 6, 2012

forgot how to do this off the top of my head. this post came up on google. just saved my ass. thanks guys

Reply

51 PHP Redstorm January 6, 2012

It was really helpful to import database using command line.

Thank you very much ;-)

Keep Posting!!!

Reply

52 waqas January 24, 2012

Tried everything, I am having the following error while importing a mysql db dump on command line. the file is around over 300 MB. I have imported the same file in centos a while ago. But now having this problem. Any help plz, I can’t change the db dump file.

“error 1064 (42000) you have an error in your sql syntax; check the manual that corresponds to your mysql server version.”

thanks

Reply

53 EJTH January 24, 2012

As i stated in my previous comment this method is more correct, and works better on large database dumps:

# mysql -h localhost -u [mysqlusername]
Enter password: [your pass]

Then type in:
use database_name
source dump.sql

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 9 + 8 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the simple math so we know that you are a human and not a script.




Previous post:

Next post: