Import and Export a Database Using Ubuntu CLI

Some time ago my Ubuntu server started misbehaving and uploading SQL files really slowly. Even the smallest of files take ten minutes to upload. And this was SO FRUSTRATING!! So I decided to use the Command Line Interface (CLI) instead and that got the job done.

Export Database

I use Putty as my SSH client but you can use whatever client you want. To export (or dump) a database using the CLI follow these steps…it’s just one step actually:

  • Login to your SSH client
  • Type the following command: mysqldump -u [username] -p [database name] > [database name].sql

*Note: “[database name].sql” will be the path to the exported file which, in its current state, is the current directory that you’re currently in (most likely your home directory). To change the export directory, add the path before

"[database name].sql" for example, ~/website_backups/myBackup.sql

Import Database

Importing a database file is just as simple except that the file should reside on the server. Here’s the code to import:

mysql -u [username] -p newdatabase < [database name].sql

*Note: “[database name].sql” will be the path to the exported file which, in its current state, is the current directory that you’re currently in (most likely your home directory). To change the export directory, add the path before

And that’s it. You’ve successfully imported and exported an SQL file using the Ubuntu CLI. Now to deal with my PHPMyAdmin slow upload issue.

Tags: , ,