Create a backup of a MySQL database using mysqldump.
Prerequisites
- MySQL installed
- User with database access
- Terminal access
Procedure
-
Open terminal
-
Run mysqldump Command:
mysqldump -u [username] -p [database_name] > backup.sql- Replace
[username]with your MySQL username. - Replace
[database_name]with the target database. - Enter password when prompted.
- Replace
-
Verify Backup: Check
backup.sqlexists in the current directory. -
Optionally, secure backup by moving it to a safe location and restrict file permissions (
chmod 600 backup.sql)
Optional Flags
--databases: Backup multiple databases.--all-databases: Backup all databases.--no-data: Backup only schema, no data.-h [hostname]: Set the MySQL server host. Defaults to localhost.-P [port]: Set the MySQL server port. Defaults to 3306.
Restore Backup
To restore: mysql -u [username] -p [database_name] < backup.sql