Exporting a MySQL database: Difference between revisions
>Samthecrazyman m Changed pages to articles, small format changes |
>Samthecrazyman Added automatic cron backup instructions. |
||
| Line 1: | Line 1: | ||
You may need to export a MySQL database if you are moving the database to another computer (for example, you are moving you Mediawiki server). You may also want to export the database to back it up as databases are not stored like files and must be recovered differently. This page will guide you through the process. | You may need to export a MySQL database if you are moving the database to another computer (for example, you are moving you Mediawiki server). You may also want to export the database to back it up as databases are not stored like files and must be recovered differently. This page will guide you through the process and also show you how to create an automatic job to create these dumps for you. | ||
The process of exporting a database to a file is generally refereed to as 'dumping' the database (don't confuse with dropping a database!) | The process of exporting a database to a file is generally refereed to as 'dumping' the database (don't confuse with dropping a database!) | ||
= Manually dumping a database = | |||
== What you need to know == | == What you need to know == | ||
You will need the following information before you start: | You will need the following information before you start: | ||
| Line 23: | Line 24: | ||
* Don't forget the '-c' option. This makes sure that mysqldump dumps the entire database exactly as it is. | * Don't forget the '-c' option. This makes sure that mysqldump dumps the entire database exactly as it is. | ||
* To aid with importing later, it is worth dumping the database to a file with the name of the database itself. You need to make the database before importing and it must have the same as the original database. | * To aid with importing later, it is worth dumping the database to a file with the name of the database itself. You need to make the database before importing and it must have the same as the original database. | ||
== Related Articles | = Creating a cron job to back up MySQL automatically = | ||
== What you need to know == | |||
* You will need KDE Task Scheduler installed. This will make creating a cron job easier. | |||
* Where you will be storing your dumps on an ongoing basis. | |||
* A user with read-only rights to access the database when backing up. | |||
== Creating the cron job == | |||
* Log on as <code>root</code> and start KDE Task Scheduler from the Kickstart menu (this can be found by typing 'cron'. | |||
* Select 'System Cron' from the radio buttons in the top task bar. | |||
* Select 'New entry'. | |||
* In the command box, we need to formulate a command. The following command gives most of the things you may need in the command and can be adapted to suit your needs:<br><pre>mysqldump -A -u USER -pPASSWORD > /path/to/backup/backup-name-$(date+\%d-\%m-\%Y).sql</pre> | |||
** <code>-A</code> switch makes <code>mysqldump</code> backup all databases. | |||
** <code>USER</code> should be changed to the user. | |||
** <code>PASSWORD</code> should be changed to the password used for the user. Notice, no space between the switch <code>-p</code> and <code>PASSWORD</code> or the job will fail. | |||
** <code>/path/to/backup</code> should be the full path to the location you want the backup to take place. | |||
** <code>backup-name</code> should be the name of the backup file, <code>$(date+\%d-\%m-\%Y)</code> adds the date to the place you place it in the file name, especially handy if you want to keep your backups every day. | |||
* Select the user you want this to run under (probably root) | |||
* Now choose the time and date you would like the backup to take place. If you would like it to happen every day, select the 'Run every day' tick box and this will select all the relevant options, leaving you just to choose a time. | |||
* Now select 'OK' and then 'OK' on the main window. | |||
* Wait until the time of the backup and make sure the file is created correctly. | |||
== Troubleshooting == | |||
* If the job fails with <code>access denied</code> a few points to check are: | |||
** Make sure there is no space between the <code>-p</code> switch and your password. | |||
** Make sure MySQL is running | |||
** Make sure the user has the correct privilidges | |||
* The % on the date part of the command must be escaped with a \ or the code will stop past the %. Cron interprets % as the end of the line and must be escaped to make it pass it. | |||
* Passwords with any special characters may make cron fail as it uses some characters for commands. You may have to change your MySQL password to an alphanumeric only password. | |||
* KDE task scheduler has a problem at the time of writing that when you edit a job, the user randomly changes. Make sure that when you edit a job, you change the user back to root or the job will more than likely fail. | |||
= Related Articles = | |||
* [[Importing a MySQL database]] | * [[Importing a MySQL database]] | ||