Managing MySQL databases efficiently is crucial for web hosting, development, and server management. Whether you’re working with a VPS hosting plan or handling large-scale applications on a dedicated server, knowing how to import and export MySQL databases using the command line is essential for backups, migrations, and troubleshooting.
First, access your server via SSH:
ssh user@yourserver.com
The mysqldump command is used to create a backup of your MySQL database:
mysqldump -u root -p database_name > backup.sql
To save space, you can compress the backup file using gzip:
gzip backup.sql
This will create backup.sql.gz, reducing storage usage.
If the destination database does not exist, create one before importing:
mysql -u root -p -e "CREATE DATABASE new_database;"
To restore a database from a backup file:
mysql -u root -p new_database < backup.sql
If the backup file is compressed, use the following command:
gunzip < backup.sql.gz | mysql -u root -p new_database
Using the command line for MySQL import and export ensures efficiency, security, and flexibility in database management. Whether you’re running databases on a VPS hosting plan or a dedicated server, mastering these commands will streamline your workflow and improve overall database administration.