How to Backup and Restore Data in MySQL
Multiple backups should be taken. E.g.
- One copy on the same computer or locally networked computer that can be used to quickly replace a database that is in use.
- Another copy in a physically different location in case something happens at the current location (fire, flood). This could be another computer at a remote site or some portable medium like a tape or CD.
If using Windows, open a command prompt window.
If using Linux, execute the commands on the command line.
The examples below are for Linux - convert backslashes and add .exe to the command for the Windows equivalents.
Steps
- To make a backup:
- mysqldump --user=username --password=password databasename >path/backupfilename
- mysqldump --user=username --password=password databasename >path/backupfilename
- To repair a table:
- Stop the MySQL server:
- mysqladmin -u username -p shutdown
- mysqladmin -u username -p shutdown
- Start the repair:
- myisamchk -r databasepath/databasename/tablename.MYI
- databasepath - The complete path to your data directory.
- tablename can be "*" which means fix all tables.
- databasepath - The complete path to your data directory.
- myisamchk -r databasepath/databasename/tablename.MYI
- You should see output on the screen telling you which tables are being checked.
- Restart the MySQL server:
- mysqladmin -u username -p start
- mysqladmin -u username -p start
- Stop the MySQL server:
- If the repair doesn't work, you may need to delete the table and restore from the backup file:
- Delete the table - in MySQL, send the query:
- DROP TABLE tablename
- DROP TABLE tablename
- Back at the command prompt:
- mysql -u username -p databasename < path/backupfilename
- mysql -u username -p databasename < path/backupfilename
- Delete the table - in MySQL, send the query:
Warnings
- The backup file contains all INSERT commands to restore all tables you specified when you made the backup. If you only want to restore some of the tables, you'll need to edit the backup file before running the restore command.
Related wikiHows
- How to Install the MySQL Database Server on Your Windows PC
- How to Retrieve Data from Mysql
- How to Make Changes to the Database Structure in Mysql
- How to Create a Database in Mysql
Article provided by wikiHow, a collaborative writing project to build the world's largest, highest quality how-to manual. Please edit this article and find author credits at the original wikiHow article on How to Backup and Restore Data in MySQL. All content on wikiHow can be shared under a Creative Commons license.