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:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 53 comments… read them below or add one }
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)
Worked like a charm! I did have to remove the spaces as Cameron suggested (thanks). I’m running Fedora Core 7.
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
Same here. phpmyadmin needed 10 mins. And now…well below 3 seconds. So awesome.
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
Thanks to Cameron James. Removing the spaces did it for me as well. Fought with that sucker for almost 4 hours…
thanx a lot for this script. and thanks cephalex for utf8 option.
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
Very nice tut ! I just moving from another host to a new one. This commands works perfectly :)
Thx a lot ;) Very usefull tips
If Your Using Red Hat Linux,
take the $ out.
and try this,
mysql -u username -p -h localhost data-base-name < data.sql
Thanks
tnx … easy to understanc
go to mysql folder and execute this command:
[root@localhost mysql]# bin/mysql [Database Name] < /home/sandeep/software/[file-name].sql
[root@localhost' is not recognized as an internal or external command,
perable program or batch file.
/bin/mysql [Database Name] < /home/sandeep/software/[file-name].sql
That is the proper command
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.
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
thx a lot, it’s very helpfull for me.
Thanks alot for these. Its helped me out of a big jam that I was in..
mysql -u username -p data-base-name < data.sql
the above statement running but tables are not created in database
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
How can I import a MySQL dumpfile into my database?
am using windows..
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…
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.
Upload file to your server using ftp or sftp client. Once uploaded run above mysql command.
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?
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:
The above command will delete all data from page table and will import a new set of data from page.sql.
Hope this helps!
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.
you did a great job dear……..its worked ……..thanks a lot …………
Thanks man, that tip saved me lots of frustrations.
can anybody know how to import vhdl(x-vhdl) document which is mysql dump file i have downloaded from server into mysql database ?
For me (xampp on Xp) this worked:
mysql -uusername -ppassword -hlocalhost data-base-name < data.sql
Note: your *.sql file must be in /xampp/
You only really need to remove the space from the -p option.
mysql -u user -ppassword dabatase < database.sql
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
how can i import php db into linux.
i am using ubuntu.
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.
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?
thank for hte information but where is the script
Thanks. This was very helpful. A quick reminder.
Do keep up the good work.
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.
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)
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
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.
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
ERROR 1153 (08S01) at line 17005: Got a packet bigger than ‘max_allowed_packet’ bytes
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
Just wanna thank EJTH – only his method worked for me.
Thanks,
How can I make a sql file, where is stored my database, using mysql command line?
Thanks.
Thanks a lot. I was missing the -h in the command
$ mysql -u username -p -h localhost data-base-name < data.sql
forgot how to do this off the top of my head. this post came up on google. just saved my ass. thanks guys
It was really helpful to import database using command line.
Thank you very much ;-)
Keep Posting!!!
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
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