Exporting a MySQL database: Difference between revisions

From ThinkServer
>Samthecrazyman
Created, from Dokuwiki
 
m Changed date code in cron command to correct order
 
(6 intermediate revisions by 2 users not shown)
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!)


== What you need to know ==
== Manually dumping a database ==
=== What you need to know ===
You will need the following information before you start:
You will need the following information before you start:
* Root access to the MySQL server
* Root access to the MySQL server
Line 9: Line 10:
* A place to save it that you have write access to (a good place to be would be your home folder)
* A place to save it that you have write access to (a good place to be would be your home folder)


== Exporting your database ==
=== Exporting your database ===
# If not at the console shell, open a console shell (Konsole in KDE)
# If not at the console shell, open a console shell (Konsole in KDE)
# We are using a program called 'mysqldump'. This does what it says - dumps the database to a file.
# We are using a program called 'mysqldump'. This does what it says - dumps the database to a file.
Line 19: Line 20:
# You database is now dumped in a file!
# You database is now dumped in a file!


== Troubleshooting ==
=== Troubleshooting ===
* Make sure you include a file path. mysqldump will by default dump to your screen if run without an input. If you see lots of scrolling with lots of random writing on the screen, you haven't included an output. The output comes after the >.
* Make sure you include a file path. mysqldump will by default dump to your screen if run without an input. If you see lots of scrolling with lots of random writing on the screen, you haven't included an output. The output comes after the >.
* 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 pages ==
== Creating a cron job to back up MySQL automatically ==
[[Importing a MySQL database]]
=== 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. Starting with the year as opposed to the day makes sorting the files in lists a lot easier.
* 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 or press 'Run now' to test the command out now (This will also make sure you have an initial backup).
 
=== 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 privileges
* 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]]
* [[MySQL]]

Latest revision as of 22:51, 9 November 2013

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!)

Manually dumping a database

What you need to know

You will need the following information before you start:

  • Root access to the MySQL server
  • The name of the database to be exported (it might be an idea to write this down, you will need this name to import it later)
  • A place to save it that you have write access to (a good place to be would be your home folder)

Exporting your database

  1. If not at the console shell, open a console shell (Konsole in KDE)
  2. We are using a program called 'mysqldump'. This does what it says - dumps the database to a file.
  3. Type the following into the shell:
    # mysqldump -u [username] -p [database name] -c > [path to file/file.sql]
    For example, assuming the MySQL user is root, the database name is wikidb and we are writing to the Linux root home directory, we will type the following:
    # mysqldump -u root -p wikidb -c > /root/Documents/wikidb.sql
  4. MySQL will prompt you for the password for the user
  5. Once you have typed and entered the password, it may look like nothing is happening, be patient, it can take a while to dump the database, especially if it is a large database or the computer is slow.
  6. When all is finished you will be returned to the command prompt.
  7. Browse to the path and check the database dump file has been created and contains data (check the size, make sure it is more than a few kilobytes)
  8. You database is now dumped in a file!

Troubleshooting

  • Make sure you include a file path. mysqldump will by default dump to your screen if run without an input. If you see lots of scrolling with lots of random writing on the screen, you haven't included an output. The output comes after the >.
  • 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.

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 root 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:
    mysqldump -A -u USER -pPASSWORD > /path/to/backup/backup-name-$(date +%d-%m-%Y).sql
    • -A switch makes mysqldump backup all databases.
    • USER should be changed to the user.
    • PASSWORD should be changed to the password used for the user. Notice, no space between the switch -p and PASSWORD or the job will fail.
    • /path/to/backup should be the full path to the location you want the backup to take place.
    • backup-name should be the name of the backup file, $(date +%d-%m-%Y) adds the date to the place you place it in the file name, especially handy if you want to keep your backups every day. Starting with the year as opposed to the day makes sorting the files in lists a lot easier.
  • 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 or press 'Run now' to test the command out now (This will also make sure you have an initial backup).

Troubleshooting

  • If the job fails with access denied a few points to check are:
    • Make sure there is no space between the -p switch and your password.
    • Make sure MySQL is running
    • Make sure the user has the correct privileges
  • 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