MySQL is the most popular open source SQL database management system and is used by many popular website applications, such as WordPress, Joomla, and Drupal.
Whether you need to restore your site from a backup, migrate the site between servers, or clone the site from one domain to another, there will inevitably be occasions where you need to import a MySQL database.
There are many ways to go about importing a database. One such method, which we will cover here, is via command line.
Related Articles
SSH into Shared Server
SSH into VPS or Dedicated Server
Upload File in File Manager
Export Database Using mysqldump
Import MySQL Database Using phpMyAdmin
Create Database Using MySQL Database Wizard
Import Database
- Access your server or account via SSH or using the Terminal in cPanel
- Navigate to the location of the .sql file you intend to import using the cd command
cd /home/userna5/path/to/sql/file/
NOTE: The file will need to be on the server in order to import it. Upload it using the File Manager or FTP if you have not already done so. - If importing to an existing database, create a backup using the mysqldump command, entering the database password when prompted
mysqldump -p -u db_user db_name > file_name.sql
REPLACE: db_user with the database username, db_name with the database name, and file_name with the desired name of the exported file.
CAUTION: Be sure to name this something different than the name of the file you are importing to avoid overwriting it.
TIP: If running as the root user on a VPS or Dedicated server, the command can be run without the user and password, so it could just be
mysqldump db_name > file_name.sql
- Run the following command to import the .sql file, entering the database password when prompted
mysql -p -u db_user db_name < file_name.sql
REPLACE: db_user with the database username, db_name with the database name, and file_name with the name of the file being imported.
CAUTION: All existing data in the database will be overwritten.
NOTE: If the import completed successfully, no output will be displayed; you will simply be returned to a new command prompt
TIP: If running as the root user on a VPS or Dedicated server, the command can be run without the user and password, so it could just be
mysql db_name < database_file.sql
Comments
0 comments
Article is closed for comments.