MySQL database backup code and method _ MySQL

Source: Internet
Author: User
It is important to back up your database when database tables are lost or damaged. In the event of a system crash, you must be able to restore as much data as possible from your table to the state at the time of the crash. Sometimes it is the MySQL administrator who causes damage. The administrator knows that the table has been damaged and tries to edit it directly in an editor such as vi or Emacs. It is important to back up your database when database tables are lost or damaged. In the event of a system crash, you must be able to restore as much data as possible from your table to the state at the time of the crash. Sometimes it is the MySQL administrator who causes damage. The administrator knows that the table has been damaged and tries to edit it directly in an editor such as vi or Emacs. this is definitely not a good thing for the table! The two main methods for backing up a database are to use the mysqldump program or directly copy database files (such as cp, cpio, or tar ). Each method has its own advantages and disadvantages: mysqldump is operated collaboratively with the MySQL server. The direct copy method is performed outside the server, and you must take measures to ensure that no customer is modifying the table you will copy. If you want to use file system backup to back up the database, the same problem also occurs: If the database table is modified during File system backup, when you enter the inconsistent subject status of the backup table file, it will be meaningless for future table recovery. The difference between file system backup and direct file copy is that you have full control over the backup process for the latter, so that you can take measures to ensure that the server does not interfere with the table. Mysqldump is slower than direct copy. Mysqldump generates text files that can be transplanted to other machines, even those machines with different hardware structures. Directly copying files cannot be transplanted to other machines, unless the table you are copying uses the MyISAM storage format. ISAM tables can only be copied on machines with similar hardware structures. The MyISAM table storage format introduced in MySQL 3.23 solves this problem. because the format is machine-independent, you can directly copy files to machines with different hardware structures. Only two conditions are met: the other machine must also run MySQL 3.23 or a later version, and the file must be in MyISAM format, not ISAM format.

No matter which backup method you use, if you need to recover the database, there are several principles that should be followed to ensure the best results:

Implement regular backup, establish a plan and strictly abide

Let the server execute the update log. When you need to recover data after a crash, the update log will help you. After you use the backup file to restore the data to the backup state, you can use the modifications following the backup again by running the query in the update log, this restores the tables in the database to the state when a crash occurs. In terms of file system backup, database backup files represent full dumping, while update logs represent progressive dumping ).

Use a unified and easy-to-understand backup file naming mechanism

Such as backup1 and buckup2 are not particularly meaningful. When you implement your recovery, you will waste time figuring out what is in the file. You may find it useful to create a backup file name using the database name and date. For example:

% Mysqldump samp_db>/usr/archives/mysql/samp_db.1999-10-02

% Mysqldump menagerie>/usr/archives/mysql/menagerie.1999-10-02

You may want to compress the backups after they are generated. The backups are huge! You also need to set an expiration date for your backup files to prevent them from filling up your disk, just as you have set your log files to expire. Back up your backup file with the file system. If a complete crash not only clears your data directory, but also clears the disk drive containing your database backup, you will be in real trouble. Back up your update logs. Place your backup file on a file system different from the one used for your database. This will reduce the possibility of filling up the file system that contains the data directory due to the generation of backups.

The technology used to create backups is also useful for copying databases to another machine. Most often, a database is transferred to a server running on another host, but you can also transfer data to another server on the same host.

1. use mysqldump to back up and copy databases

When you use the mysqldumo program to generate a database backup file, by default, the file content includes the CREATE statement for creating the table being dumped and the INSERT statement containing the row data in the table. In other words, the output produced by mysqldump can be used as mysql input to recreate the database. You can dump the entire database into a separate text file, as shown below:

% Mysqldump samp_db>/usr/archives/mysql/samp_db.1999-10-02

The beginning of the output file looks like this:

# MySQL Dump 6.0 #

# Host: localhost Database: samp_db

#---------------------------------------

# Server version 3.23.2-alpha-log

# Table structure for table 'absence '#

Create table absence (student_id int (10)

Unsigned DEFAULT '0' not null, date DEFAULT '2017-00-00 'not null, primary key (student_id, date ));

# Dumping data for table 'absence '#

Insert into absence VALUES (3, '2017-09-03 ');

Insert into absence VALUES (5, '2017-09-03 ');

Insert into absence VALUES (10, '2017-09-08 ');

......

The rest of the file consists of more INSERT and create table statements.

If you want to compress the backup, use a command similar to the following:



% Mysqldump samp_db │ gzip>/usr/archives/mysql/samp_db.1999-10-02.gz

If you want a large database, the output file will also be huge and may be difficult to manage. If you want to, you can list individual table names after the database names in the mysqldump command line to lean them to the contents, which divides the dumped files into smaller and easier-to-manage files. The following example shows how to roll some tables in the samp_db database to a separate file:

% Mysqldump samp_db student score event absence> grapbook. SQL

% Mysqldump samp_db member president> hist-league. SQL

If you generate a backup file that is used to regularly refresh the content of another database, you may want to use the -- add-drop-table option. This tells the server to write the drop table if exists statement to the backup file. Then, when you extract the backup file and load it into the second database, IF the TABLE already EXISTS, you will not get an error. If you pour out a database so that the database can be transferred to another server, you don't even have to create a backup file. Ensure that the database exists on another host and then dumped the database using pipelines, so that mysql can directly read the output of mysqldump. For example, if you want to copy the database samp_db from the host pit-viper.snake.net to boa.snke.net, you can easily do this:



% Mysqladmin-h boa.snke.net create samp_db

% Mysqldump samp_db │ mysql-h boa.snke.net samp_db

In the future, if you want to refresh the database on boa.snke.net again, skip the mysqladmin command, but add -- add-drop-table to mysqldump to avoid table errors:

% Mysqldump -- add-drop-table samp_db │ mysql-h boa.snke.net samp_db

Other useful options for mysqldump include:

-- Flush-logsAnd -- Lock-tablesThe combination will be helpful for your database checkpoints.

-- Lock-tablesLock All tables you are dumping

While -- Flush-logsClose and re-open the update log file. The new update log will only include queries for modifying the database from the backup point. This will set the backup Time of Your update log check point. (However, if you have a customer who needs to perform an update, it is not a good thing to lock all tables for customer access during Backup .) If you use -- Flush-logsWhen setting check point to backup, it is best to dumped the entire database. If you dumped individual files, it is difficult to synchronize the update log checkpoint with the backup file. During the recovery period, you usually extract the updates based on the database, but you have no choice to extract updates for a single table. Therefore, you must extract them by yourself. By default, mysqldump reads the entire content of a table into the memory before writing. This is usually unnecessary, and in fact, if you have a large table, it is almost a failure. You are available -- QuickOption tells mysqldump to write each row as long as it retrieves a row. To further optimize the dumping process, use -- OptInstead -- Quick. -- OptOption to open other options, accelerate data dumping and read them back. Use -- OptBackup may be the most commonly used method, because of the advantages of backup speed. However, to warn you, -- OptThe option does have a price, -- OptThe optimization is your backup process, not the access of other customers to the database. -- OptYou can lock all tables at a time to prevent anyone from updating any tables you are dumping. You can easily see the effect of common database access. When your database is usually used very frequently, it only adjusts the backup once a day. A -- OptThe opposite effect options are: -- Dedayed. This option causes mysqldump to write the insert delayed statement instead of the INSERT statement. If you load data files into another database and want to minimize the impact of this operation on queries that may appear in the database, -- DelayedThis is very helpful. -- CompressThis option is helpful when you copy the database to another machine because it reduces the number of bytes transmitted over the network. The following is an example. -- CompressThe program that communicates with the server on the remote host is provided, rather than the program connected to the local host:

% Mysqldump -- opt samp_db │ mysql -- compress-h boa.snke.net samp_db

Mysqldump has many options. for details, see MySQL Reference Manual.
2. use the backup and copy methods for directly copying databases

Another method that does not involve mysqldump to back up databases and tables is to directly copy database table files. Typically, this uses utilities such as cp, tar, or cpio. The example in this article uses cp. When using a direct backup method, you must ensure that the table is not used. If the server changes a table while you are copying it, the copy will be meaningless. The best way to ensure your copy integrity is to close the server, copy files, and restart the server. If you do not want to shut down the server, you must lock the server while performing the table check. If the server is running, the same constraints apply to copying files, and you should use the same locking protocol to make the server "quiet ". If the server is closed or you have locked the table you want to copy, the following shows how to back up the entire samp_db database to a backup directory (DATADIR indicates the server's data directory ):

% Cd DATADIR % cp-r samp_db/usr/archive/mysql

A single table can be backed up as follows:

% Cd DATADIR/samp_db % cp member. */usr/archive/mysql/samp_db % cp score. */usr/archive/mysql/samp_db ....

When you have completed the backup, you can restart the server (if it is disabled) or release the lock added to the table (if you want the server to run ). To copy a database from one machine to another, just copy the file to the appropriate data directory of another server host. Make sure the file is in MyIASM format or the two machines have the same hardware structure. Otherwise, your database has strange content on another host. You should also ensure that the servers on the other machine do not access them when you are installing database tables.

3. Replicating Database)

Replication is similar to copying a database to another server, but its exact meaning is to ensure full synchronization of the two databases in real time. This feature will be available in version 3.23 and is not very mature. Therefore, this article will not detail it.

4. use backup to restore data

There are many causes for database corruption, and their severity varies. If you are lucky, you may only damage one or two tables (such as power loss). If you are unlucky, you may have to replace the entire data directory (such as disk damage ). Recovery is also required in some cases. for example, the user mistakenly deleted the database or table. Regardless of the cause of these unfortunate events, you will need to implement a recovery. If the tables are corrupted but not lost, try to use myisamchk or isamchk to fix them. if such a corruption occurs, you may not need to use backup files at all. For the table repair process, see database maintenance and repair. The recovery process involves two sources of information: your backup file and an update log. The backup file restores the table to the status when the backup is implemented. however, generally, the table has been modified in the time between the backup and the occurrence of the problem. The update log contains queries for these changes. You can use the log file as the mysql input to repeat the query. This is why Log update is enabled. The recovery process varies depending on how much information you must restore. In fact, it is easier to restore the entire database than a single table because

It is easier for the database to use Update logs than to use a single table.

4.1 restore the entire database

First, if the database you want to restore is a mysql database containing an authorization table, you need to use -- Skip-grant-tableOption to run the server. Otherwise, it will complain that the authorization table cannot be found. After you have restored the table, execute mysqladmin flush-privileges to tell the server to load the authorization labels and use them. Copy the contents of the database directory to another place if you need them later. Use the latest backup file to reinstall the database. If you use the file generated by mysqldump, use it as the mysql input. If you copy files directly from the database, copy them directly back to the Database Directory. However, you need to close the database before copying the files, and then restart it. Use the update log to repeat the backup and then modify the database table. Use any applicable update logs as mysql input. Specify -- One-databaseThis option enables mysql to only query databases that you are interested in restoring. If you know that you need to use all the update log files, you can use this command in the directory containing logs:

% Ls-t-r-1 update. [0-9] * │ xargs cat │ mysql -- one-database db_name

The ls command generates a single column list of update log files and sorts them by the server's order (idea: if you modify any file, you will change the sorting order, this leads to the use of the wrong order of updated logs .) You may use several updates. For example, the update logs generated since your backup are named update.392 and update.393. you can run them again like this:

% Mysql -- one-database db_name <update.392

% Mysql -- one-database db_name <update.393

.....

If you are performing recovery and use the update log to recover lost information due to an error suggested drop database, drop table, or DELETE statement, before using the update log, make sure to delete these statements from it.

4.2 restore a single table

Restoring a single table is complicated. If you use a backup file generated by mysqldump that does not contain the data of the tables you are interested in, you need to extract them from the relevant rows and use them as mysql input. This is an easy part. The hard part is to pull the fragments from the update logs that only apply to the table. You will find that the mysql_find_rows utility is very helpful for this, it extracts multi-row queries from the update log. Another possibility is to use another server to restore the entire database and then copy the expected table file to the original database. This may be really easy! When you copy files back to the Database Directory, make sure that the server of the original database is disabled.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.