Big Data/Analytics Zone is brought to you in partnership with:

Paulund is a website dedicated to writing tutorials and code snippets about Web Development, the main subjects are PHP, Wordpress, jQuery, CSS3 and HTML5. Paul is a DZone MVB and is not an employee of DZone and has posted 124 posts at DZone. You can read more from them at their website. View Full User Profile

Import and Export a Database with SSH

07.17.2013
| 5848 views |
  • submit to reddit

The following code snippets will allow you to import and export a large database using SSH commands.

To get SSH access to your hosts server you will need to contact your current web hosting company. If you are on a shared hosting package you may not be allowed to have SSH access to your hosts server.

To run both of these commands you will need to have MySQL installed on your server.

Export A Database

The following example will show how you how to export your database into a single SQL file. It is important to backup your database regularly or when you are making changes to the server such as releasing new code.

To export the database you need to run the following command.

mysqldump -p -u username -h hostname database_name > dbname.sql

This runs the mysqldump command with a number of parameters. The first parameter is -p which means password, when you run this command the script will ask you to enter your database password. If you want to do this in one line then you type in the password after the -p without any spaces.

mysqldump -pP@55w0rd -u username -h hostname database_name > dbname.sql

The next parameter is -u which means username, this will be the username you use to access your database. The next parameter is -h for host, you only need to use this if your database is on a different server, if you have your database on the current server then you do not need this parameter.

Next you type in the database name that you want to export, followed by a > for export and then the name and location of the file you are going to export this data into. In this example it just puts the file in dbname.sql which will place the file in the current location you are in, if you want to put these in a certain folder you need to provide the full folder path.

mysqldump -pP@55w0rd -u username -h hostname database_name > /var/www/vhosts/website/backup/dbname.sql

Import A Database

With the SQL file that you have just exported you can now easily import this into any database that you want by using the following code snippet.

First you need to make sure that they file you want to import is of a .sql format, then upload this file to your server so that you have access to it from an SSH command. Next make sure that the database you want to import this file into has been created, now you will able to import the database.

mysql -p -u username -h hostname database_name < /var/www/vhosts/website/backup/dbname.sql

This time you are running the mysql command with the same parameters, -p for password again if you want to put the password in this one command you can do or you will be asked for the password when you run this command. Next parameter is -u for the username of the database, -h for the host if the database is on a different server, next is the name of the database you want to import the SQL file. The next parameter is a < for import and then the location of the file you want to import.

Published at DZone with permission of Paul Underwood, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Daniel Kec replied on Wed, 2013/07/17 - 7:33am

Headline should be "Import and Export a MySQL Database with SSH"

Leandro Jordão replied on Wed, 2013/07/17 - 11:31am

Access your server and do your back up. Great, it can not worse.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.