How to Import Export MySQL database using SSH connection

This article shows how to Import Export Mysql database, Normally if you are dealing with a large databases over than 128MB it is better to control them by SSH.

In the same vein to manage the files and databases on your account you can use SSH connection . It is an efficient method to reach your goal. Just run a simple command  to import or export a MYSQL database into an existing database on your account.

Using SSH To Import Your MySQL Database

  • First Upload the SQL file to the server via the file manager or an FTP client.
  • If the database does not exist, please create a database, a user, and give the user full privileges to the database. Note the username and password.

  • After that log in to the server through SSH.

  • Navigate to the directory where your .sql file is.

  • After that run the below command:

				
					mysql -p -u user_name database_name < file.sql (replace 'user_name', 'database_name', and 'file.sql' with the actual name.)
				
			
  • You will be prompted for your database user password, and then your database will be imported.

Using SSH To Export Your MySQL Database

Use the mysqldump command to export a MySQL database.It is a good idea to export your data often as a backup.

  • First login to server via SSH and execute the below command:
				
					mysqldump -p -u username database_name > dbname.sql
				
			
  • Secondly you will be prompted for a password, type in the username password, and press Enter. Replace username, password, and database_name with your MySQL username, password, and database name.
  • The file dbname.sql now holds a backup of your database and is ready for download to your computer. In conclusion to export a single table from your database, you would use the following command:

				
					mysqldump -p --user=username database_name tableName > tableName.sql
				
			
  • Again you would need to replace the username, database, and tableName with the correct information. Once done, the table specified would then be saved to your account as tableName.sql.

Share Article

Related Articles