Difference between revisions of "MySQL/porting/data"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search
m (backing up a MediaWiki moved to backing up MediaWiki)
(→‎Using phpmyadmin (web-based): original version written by Aero over a decade ago, but he wanted to disappear from the site and there have been lots of edits since then anyway...)
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''Topic''': How to back up and restore a [[MediaWiki]] database. MediaWiki databases use [[MySQL]].
+
==About==
 +
This subpage is about porting data into and out of [[MySQL]] databases.
  
 
There are several techniques for doing this.
 
There are several techniques for doing this.
 
==Using phpmyadmin (web-based)==
 
==Using phpmyadmin (web-based)==
<font size=-2>(by [[User:Aero|Aero]])</font>
 
 
===Backing up===
 
===Backing up===
 
#Make sure you've got [http://www.phpmyadmin.net PHPMyAdmin] running and that's all happy. I won't cover this part here as it varies with your set up.
 
#Make sure you've got [http://www.phpmyadmin.net PHPMyAdmin] running and that's all happy. I won't cover this part here as it varies with your set up.
Line 23: Line 23:
 
====Notes====
 
====Notes====
 
Some installations of phpmyadmin may have an upper size-limit on imports &ndash; e.g. at l48.net it's 2 MB, which basically means I can't use it to restore any of my wikis.
 
Some installations of phpmyadmin may have an upper size-limit on imports &ndash; e.g. at l48.net it's 2 MB, which basically means I can't use it to restore any of my wikis.
 +
 
==Using mysql-admin (GUI utility)==
 
==Using mysql-admin (GUI utility)==
 
===Backing up===
 
===Backing up===
Line 37: Line 38:
 
There's a "restore backup" button right under the "backup" button, and I presume the process is also fairly intuitive. Unfortunately, on my system (Ubuntu 4.x), pressing this button instantly causes mysql-admin to close, with no warning (presumably a crash/bug). This may be a problem with Ubuntu, as I have had similar problems with other applications that worked fine in other versions of Linux (I have not tried testing mysql-admin in other Linux distros). --[[User:Woozle|Woozle]] 14:04, 8 May 2006 (EDT)
 
There's a "restore backup" button right under the "backup" button, and I presume the process is also fairly intuitive. Unfortunately, on my system (Ubuntu 4.x), pressing this button instantly causes mysql-admin to close, with no warning (presumably a crash/bug). This may be a problem with Ubuntu, as I have had similar problems with other applications that worked fine in other versions of Linux (I have not tried testing mysql-admin in other Linux distros). --[[User:Woozle|Woozle]] 14:04, 8 May 2006 (EDT)
 
==Using mysql (command-line)==
 
==Using mysql (command-line)==
 +
===Backing Up===
 +
* Official MySQL documentation: [http://dev.mysql.com/doc/refman/5.5/en/backup-and-recovery.html Chapter 6. Backup and Recovery]
 +
 +
For all of these examples:
 +
* If you leave off the {{arg|password}}, it will prompt you for one.
 +
* To connect to a MySQL server on localhost, omit -h{{arg|hostname}}. '''not tested'''
 +
 +
From a shell command line:
 +
;mysqldump --tab={{arg|path}} -h{{arg|hostname}} -u{{arg|username}} -p{{arg|password}} --opt {{arg|dbname}}
 +
: This saves SQL code to recreate the entire database in .sql files compatible with the restoring method given below.
 +
:* This will create one .sql file per table, in the folder specified by {{arg|path}} (which must already exist).
 +
;mysqldump --tab={{arg|/output/path}} -h{{arg|hostname}} -u{{arg|username}} -p --opt {{arg|dbname}} {{arg|table_name_1}} {{arg|table_name_2}} {{arg|...}}
 +
: This only dumps tables.
 +
;mysqldump -h{{arg|hostname}} -u{{arg|username}} --opt {{arg|dbname}} &gt; {{arg|filespec}}
 +
: This dumps the entire database to a single file named {{arg|filespec}} (which will be created).
 +
;mysqldump --all-databases -hhost -uuser -ppassword &gt; name_of_dump_file.sql
 +
: This backs up all databases to a single file.
 +
 +
Questions:
 +
* Not sure what the <code>--opt</code> flag does.
 +
* I don't know if there's a way to dump all dbs to individual files.
 +
 +
To copy the dump file from one server to another, [[rsync]] generally works well.
 +
 +
For more specific backups, the following mysql client commands are relevant:
 +
;SHOW CREATE DATABASE {{arg|dbname}}; : Returns the SQL code which could be used to recreate the database, ''not'' including the contents.
 +
;SHOW CREATE TABLE {{arg|tablename}}; : Returns the SQL code which could be used to recreate{{arg|tablename}}, ''not'' including the data.
 +
 +
===Restoring===
 
If you are restoring to a different database name (as might happen if you were moving the db to a new server), you will need to edit the .sql file to use the appropriate database name in the line which begins "CREATE DATABASE" and the "USE" line following it. If you know the database has already been created, you can just delete the CREATE DATABASE line.
 
If you are restoring to a different database name (as might happen if you were moving the db to a new server), you will need to edit the .sql file to use the appropriate database name in the line which begins "CREATE DATABASE" and the "USE" line following it. If you know the database has already been created, you can just delete the CREATE DATABASE line.
  
Line 44: Line 74:
 
  mysql -h<u>server</u> -u<u>username</u> -p<u>password</u> < <u>path/to/backup/file.sql</u>
 
  mysql -h<u>server</u> -u<u>username</u> -p<u>password</u> < <u>path/to/backup/file.sql</u>
 
Note that the path is relative to the system on which you are running the mysql utility, ''not'' the system running the mysql server (mysqld). Note also that this technique doesn't tell you anything until it finishes processing the .sql file.
 
Note that the path is relative to the system on which you are running the mysql utility, ''not'' the system running the mysql server (mysqld). Note also that this technique doesn't tell you anything until it finishes processing the .sql file.
 +
You can also specify the database in which the SQL commands should be run:
 +
mysql -h<u>server</u> -u<u>username</u> -p<u>password</u> -D<u>dbname</u> < <u>path/to/backup/file.sql</u>
  
 +
==Notes==
 +
* '''Error message''': "The MySQL server is running with the --secure-file-priv option so it cannot execute this statement"
 +
** '''Meaning''': MySQL has been configured to only allow imports or exports from within a specified folder.
 +
** '''Solution''':  Check the secure_file_priv setting (can be found in MySQL Workbench under Server -> Status and System Variables -> System Variables tab; current Ubuntu default is "/var/lib/mysql-files/") to see where you should be putting your import/export files so as not to trigger this error. ...or you can stop the server, change the setting, restart, and try again.
 
==Related Resources==
 
==Related Resources==
 
*[http://members.lycos.co.uk/wipe_out/automysqlbackup/ AutoMySQLBackup]
 
*[http://members.lycos.co.uk/wipe_out/automysqlbackup/ AutoMySQLBackup]

Latest revision as of 15:40, 25 September 2023

About

This subpage is about porting data into and out of MySQL databases.

There are several techniques for doing this.

Using phpmyadmin (web-based)

Backing up

  1. Make sure you've got PHPMyAdmin running and that's all happy. I won't cover this part here as it varies with your set up.
  2. Login and choose Export from the main page
  3. Leave most of the default options. Specifically,
    • Format: SQL
    • Select the database(s) you want in the listbox
    • etc.
  4. Checkmark 'Add DROP TABLE'. This will make sure that when you restore from a backup, it will delete existing tables before restoring from the backed up ones.
  5. Checkmark 'Save to file' at the bottom, or it will just dump it to your browser window, and that's no fun.
    • You probably also want to use compression. These won't be huge files, but a 1.5mb database compresses to about 150kb. So, y'know, whatever. Wiggy recommends BZIP.
  6. Click Go.

Restoring from backup

I'm writing this from memory, so I might be wrong, but I think it's right.

  1. Click Databases from the PHPMyAdmin main page
  2. Click the name of your databse (it should already exist; have you [re]installed MediaWiki yet? Do that first.)
  3. Click the SQL tab
  4. Where it says Location of textfile:, browse to your latest backup, then click Go.

Notes

Some installations of phpmyadmin may have an upper size-limit on imports – e.g. at l48.net it's 2 MB, which basically means I can't use it to restore any of my wikis.

Using mysql-admin (GUI utility)

Backing up

  1. Choose a place on your hard drive for storing the file; create any folders needed.
  2. Run mysql-admin (this can be done from the command line)
  3. Connect to mysql. You'll need the server address, a username with read permissions, and the password for that user.
    • Note: mysql-admin seems to have trouble actually using passwords you've stored, so you may have to type in the password each time. Hopefully they will fix this soon.
    • Another note: the current version of mysql-admin doesn't like connecting to mysql versions prior to 4.0, but it did connect and backup my v3.23. It appears prone to sudden crashing on certain operations, so avoid poking around in the menu while backing up or restoring.
  4. Press the "backup" icon/button on the left.
  5. Check the 'Add DROP TABLE' box on the "Output File Options" box on the "Advanced Options" tab, as with the phpmyadmin backup.
  6. Steps from that point on are pretty intuitive, except for the scheduler (which still has me confused). Apparently mysql-admin can set itself up to do backups automatically, using the cron scheduler, but this hasn't been working; at first glance, it looks like it could be a permissions problem. (If you retrieve email from your localhost POP3 server, you will see error messages from the cron scheduler if it couldn't run something it was supposed to run.)
  7. The first time through, you will probably want to save the backup configuration to a Backup Project, for quick reuse later. I had trouble once, however, with the "Backup Now" button greying out after doing this; I'm not sure what caused it.

Restoring

There's a "restore backup" button right under the "backup" button, and I presume the process is also fairly intuitive. Unfortunately, on my system (Ubuntu 4.x), pressing this button instantly causes mysql-admin to close, with no warning (presumably a crash/bug). This may be a problem with Ubuntu, as I have had similar problems with other applications that worked fine in other versions of Linux (I have not tried testing mysql-admin in other Linux distros). --Woozle 14:04, 8 May 2006 (EDT)

Using mysql (command-line)

Backing Up

For all of these examples:

  • If you leave off the <password>, it will prompt you for one.
  • To connect to a MySQL server on localhost, omit -h<hostname>. not tested

From a shell command line:

mysqldump --tab=<path> -h<hostname> -u<username> -p<password> --opt <dbname>
This saves SQL code to recreate the entire database in .sql files compatible with the restoring method given below.
  • This will create one .sql file per table, in the folder specified by <path> (which must already exist).
mysqldump --tab=</output/path> -h<hostname> -u<username> -p --opt <dbname> <table_name_1> <table_name_2> <...>
This only dumps tables.
mysqldump -h<hostname> -u<username> --opt <dbname> > <filespec>
This dumps the entire database to a single file named <filespec> (which will be created).
mysqldump --all-databases -hhost -uuser -ppassword > name_of_dump_file.sql
This backs up all databases to a single file.

Questions:

  • Not sure what the --opt flag does.
  • I don't know if there's a way to dump all dbs to individual files.

To copy the dump file from one server to another, rsync generally works well.

For more specific backups, the following mysql client commands are relevant:

SHOW CREATE DATABASE <dbname>;
Returns the SQL code which could be used to recreate the database, not including the contents.
SHOW CREATE TABLE <tablename>;
Returns the SQL code which could be used to recreate<tablename>, not including the data.

Restoring

If you are restoring to a different database name (as might happen if you were moving the db to a new server), you will need to edit the .sql file to use the appropriate database name in the line which begins "CREATE DATABASE" and the "USE" line following it. If you know the database has already been created, you can just delete the CREATE DATABASE line.

You might also want to verify that the mysql utility connects successfully before you try the import:

mysql -hserver -uusername -ppassword

If that works, then the full command to create a database from a .sql file is:

mysql -hserver -uusername -ppassword < path/to/backup/file.sql

Note that the path is relative to the system on which you are running the mysql utility, not the system running the mysql server (mysqld). Note also that this technique doesn't tell you anything until it finishes processing the .sql file. You can also specify the database in which the SQL commands should be run:

mysql -hserver -uusername -ppassword -Ddbname < path/to/backup/file.sql

Notes

  • Error message: "The MySQL server is running with the --secure-file-priv option so it cannot execute this statement"
    • Meaning: MySQL has been configured to only allow imports or exports from within a specified folder.
    • Solution: Check the secure_file_priv setting (can be found in MySQL Workbench under Server -> Status and System Variables -> System Variables tab; current Ubuntu default is "/var/lib/mysql-files/") to see where you should be putting your import/export files so as not to trigger this error. ...or you can stop the server, change the setting, restart, and try again.

Related Resources