MySQL database backup and recovery Xiangjie (on)

  This article discusses the MySQL backup and recovery mechanisms, and how to maintain the data table, including the two main types of tables: MyISAM and Innodb, MySQL version 5.0.22 is designed in this paper.

  Currently free backup tool MySQL support are: mysqldump, mysqlhotcopy, can also be backed up with SQL syntax: BACKUP TABLE or SELECT INTO OUTFILE, or if the backup binary logs (binlog), also can be directly copy the data files and associated configuration files. MyISAM table is stored in the form of a file, thus relatively easy backup, several methods mentioned above can be used. Innodb all the tables are stored in the same data file ibdata1 in (possibly more than one file, or file a separate table space), is relatively good backup copy of the free programs can be data files, backup binlog, or use mysqldump.

  1、mysqldump

  Backup 1.1

  mysqldump is the use of SQL-level backup mechanism, which will lead into a data table SQL script file, relatively appropriate upgrades between different versions of MySQL, which is the most commonly used backup method. Some of the main parameters are now speaking about the mysqldump:

  -compatible = name It tells mysqldump, export the data are compatible and which kind of database or an old version of the MySQL server. Value can be ansi, mysql323, mysql40, postgresql, oracle, mssql, db2, maxdb, no_key_options, no_tables_options, no_field_options the like, several values ​​to be used, they are separated by commas. Of course, it does not guarantee full compatibility, but as far as possible compatible.

  -complete-insert, -c export data using the full field name contains the INSERT mode, that is, all the values ​​are written in one line. Doing so can improve the insertion efficiency, but may be affected max_allowed_packet parameters which led to the insertion fails. Therefore, prudent use of this parameter, at least I do not recommend.

  What character set when using -default-character-set = charset Specify export data, if the data table is not using the default character set latin1, then this option must be specified when exporting, or importing data again after the garbled question.

  -disable-keys tell mysqldump increase at the beginning and end of the INSERT statement; and; statements, which can greatly increase the speed of insert statement, because it is completely inserted before re-indexing of all data. This option is only for MyISAM tables.

  -extended-insert = true | false case by default, mysqldump open -complete-insert mode, so do not want to use it, then you use this option, set its value to false.

  -hex-blob Export hexadecimal format using the binary string field. If there is binary data must use this option. The field types are affected BINARY, VARBINARY, BLOB.

  -lock-all-tables, -x before you start exporting, submit a request to lock all tables in all databases to ensure data consistency. This is a global read lock, and automatically turn off -single-transaction and -lock-tables option.

  -lock-tables it and -lock-all-tables similar, but the current export data table lock instead of locking all of a sudden all the tables in the library. This option applies only to MyISAM tables, if the table is Innodb can -single-transaction option.

  -no-create-info, -t export data only, without adding CREATE TABLE statement.

  -no-data, -d does not export any data, only export the database table structure.

  -opt This is just a shortcut option, equivalent to the same time add -add-drop-tables -add-locking -create-option -disable-keys -extended-insert -lock-tables -quick -set-charset option. This option allows mysqldump to export data quickly, and the exported data can quickly lead back. This option is enabled by default, but can be disabled with -skip-opt. Note that if you run mysqldump not specified -quick or -opt option, the entire result set in memory. If you are exporting a large database, then problems may arise.

  -quick, -q This option is useful when exporting a large table, it is mandatory to obtain records mysqldump query from the server rather than directly to the output of all records that they will get cached in memory.

  -routines, -R export stored procedures and custom functions.

  -single-transaction The option to submit a BEGIN SQL statement before exporting the data, BEGIN does not block any applications and can ensure the consistency of the database status when exported. It only applies to the transaction table, such as InnoDB and BDB. This option and -lock-tables option are mutually exclusive, because LOCK TABLES causes any pending transaction implicitly committed. To export a large table, use -quick option should be combined.

  -triggers while export trigger. This option is enabled by default, disable it with -skip-triggers.

  Other parameters details, please reference manual, I usually come back up MyISAM table using the following SQL:

  /usr/local/mysql/bin/mysqldump -uyejr -pyejr

  –default-character-set=utf8 –opt –extended

  -insert=false –triggers -R –hex-blob -x db_name

  > db_name.sql

  Use the following SQL to back up Innodb table:

  /usr/local/mysql/bin/mysqldump -uyejr -pyejr –default

  -character-set=utf8 –opt –extended-insert=

  false –triggers -R –hex-blob –single-transaction db_name

  > db_name.sql

  Restore 1.2

  Mysqldump backup file out with a can directly into the SQL script, there are two ways to import data into.

  Mysql directly with clients such as:

  /usr/local/mysql/bin/mysql -uyejr -pyejr db_name < db_name.sql

  In fact, this is not the syntax with the SOURCE standard SQL syntax, but the mysql client provides features such as: SOURCE /tmp/db_name.sql;

  It should be the absolute path of the specified file, and must be run mysqld user (such as nobody) has permission to read the file.

  2、mysqlhotcopy

  Backup 2.1

  mysqlhotcopy is a PERL program, originally written by Tim Bunce. It uses LOCK TABLES, FLUSH TABLES and cp or scp to quickly back up the database. It is the fastest way to back up the database or single tables, but it can only run on the machine database files (including data table definition files, data files, index files) is located. mysqlhotcopy only for backup MyISAM, and can only run on a Unix system and NetWare.

  mysqlhotcopy supports one copy multiple databases, and also supports regular expression. Here are a few examples:

  root#/usr/local/mysql/bin/mysqlhotcopy -h=localhost -u=yejr -p=yejr db_name /tmp

  (Db_name copy the database directory under / tmp)

  root#/usr/local/mysql/bin/mysqlhotcopy -h=localhost -u=yejr -p=yejr db_name_1 …

  db_name_n /tmproot#/usr/local/mysql/bin/mysqlhotcopy -h

=localhost -u=yejr -p=yejr db_namehttps://searchdatabase.techtarget.com.cn/7-17818/regex/ /tmp

  Please use a more detailed view the manual, or call the following command to view mysqlhotcopy help of:

  perldoc /usr/local/mysql/bin/mysqlhotcopy

  Note that you want to use mysqlhotcopy,

  You must have SELECT, RELOAD privilege (to execute FLUSH TABLES) rights, and also must be able to have read datadir / db_name directory.

  Restore 2.2

  mysqlhotcopy back out of the entire database directory, can be copied directly to the use of specified mysqld DATADIR (here is / usr / local / mysql / data /) directory can also pay attention to the question of powers, the following example:

  root#cp -rf db_name /usr/local/mysql/data/root#chown -R nobody:nobody /usr/local/mysql/data/

  (Owner will change db_name mysqld run user directory)

Guess you like

Origin www.cnblogs.com/sqlservertongbu/p/11013440.html