Export and Import Database in CentOS/Ubuntu Server
We can easily export and import MySQL/MariaDB database in CentOS or Ubuntu server.
Prerequisites
- SSH access to Linux sever.
- The database credentials.
Export Database
Login to your server via SSH. You need the database name
, username
of the database and the user password
. Now run this command to export the database:
sudo mysqldump -u USERNAME -pPASSWORD DATABASE_NAME > database_name.sql
USERNAME
– The username of the database.PASSWORD
– The password of the user.DATABASE_NAME
– The database name you want to export.database_name.sql
– This will be the exported .sql file which will be stored in the current directory (the SSH server’s current path).
At the time of exporting, if there is any problem, mysqldump will print the error.
Example:
sudo mysqldump -u obydul -pTest1234 test_db_1 < dump.sql
Import Database
Login to the database as root (or root access username).
mysql -u root -p
We are going to create a new database called 'new_database'.
CREATE DATABASE new_database;
Exit the MySQL by typing CTRL+D
.
Now go to the directory where the .sql file is located. Then hit this command to import the database:
sudo mysql -u USERNAME -pPASSWORD new_database < database_name.sql
USERNAME
- The username of the new database.PASSWORD
- The password of the user.new_database
- The database name where you want to import.database_name.sql
- This database will be imported.
Example:
sudo mysql -u obydul -pTest1234 test_db_2 < dump.sql
You can learn more from official mysqldump documentation page. Thank you.
Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.