Several ways database backup MySql database backup

Several ways MySql database backup

 

mysqldump backup tool

Back up the entire database

$> mysqldump -u root -h host -p dbname > backdb.sql

A backup of the database table

$> mysqldump -u root -h host -p dbname tbname1, tbname2 > backdb.sql

Backup multiple databases

$> mysqldump -u root -h host -p --databases dbname1, dbname2 > backdb.sql

All database backup system

$> mysqldump -u root -h host -p --all-databases > backdb.sql

Directly copy the entire database directory (for the InnoDB storage engine NA) backup

windowns: installpath/mysql/data

linux: /var/lib/mysql

Before copying the need to execute the following command:

MYSQL> LOCK TABLES;
# 在复制过程中允许客户继续查询表,
MYSQL> FLUSH TABLES; # 将激活的索引页写入硬盘。

mysqlhotcopy backup tool

Backup of the database table or fastest route, can only run on the machine where the database directory, and can only back up MyISAM table type.

To use this backup method must be able to access the table file backup.

$> mysqlhotcopy -u root -p dbname /path/to/new_directory;
#将数据库复制到new_directory目录。

mysql import sql file restore order

$> mysql -u root -p [dbname] < backup.sql
# 执行前需要先创建dbname数据库,如果backup.sql是mysqldump创建的备份文件则执行是不需要dbname。
MYSQL> source backup.sql; # 执行source命令前需要先选择数据库。

Direct copy to restore the database directory

Note: The way to be sure the original database and consistent database major version number to be reduced, and applies only to table MyISAM engine.

  1. Shut down mysql service.
  2. The backup of the file or directory covering mysql data directory.
  3. Start mysql service.
  4. For linux systems, the user needs to change user and group files and group mysql run after the files are copied.

mysqlhotcopy fast recovery

Stop mysql service, copy the backup files to a location database (mysql data folder) to store data, the first re-start mysql service can (may need to specify the owner of the database file).

$> cp -R /usr/backup/test /usr/local/mysql/data
# 如果恢复的数据库已经存在,则使用DROP语句删除已经存在的数据库之后,恢复才能成功,还需要保证数据库版本兼容。

Migrating between versions of the same database

$> mysqldump -h www.abc.com -uroot -p password dbname | 
$> mysqldump -h www.bcd.com -uroot -p password
# 将服务器www.abc.com的数据库dbname迁移到服务器www.bcd.com的相同版本数据库上。

Migration between different versions of the mysql database

Back up the original database.

Uninstall the original database.

Install the new database.

Restore a backup of the database data in the new database.

Database users access the information you need to back up mysql database.

The default character set issues, MySQL4.x use latin1 as the default character set, mysql5.x use utf8 as the default character set. If there is Chinese data needed to make changes to the default character set.

Migration between different databases

MyODBC tool to achieve migration between MySQL and SQL Server.

MySQL Migration Toolkit tools.

Export and import table

SELECT ...... INTO OUTFILE to export a text file, which can only be exported to a database server and export the file can not already exist.

MYSQL> SELECT ...... INTO OUTFILE filename [OPTIONS]
MYSQL> SELECT * FROM test.person INTO OUTFILE "C:\person0.txt"; # 将表person里的数据导入为文本文件person0.txt。

mysqldump file export text files (and INTO OUTFILE is not the same as the method of all options need to add quotes)

$> mysqldump -T path -u root -p dbname [tables] [OPTIONS]
# -T参数表明导出文本文件。path导出数据的目录。
$> mysqldump -T C:\test person -u root -p #test表中的person表导出到文本文件。执行成功后test目录下会有两个文件,person.sql和person.txt

mysql command to export a text file

MYSQL> mysql -u root -p --execute="SELECT * FROM person;" test > C:\person3.txt;
#test数据库中的person表数据导出到person3.txt文本文件中。--vartical参数可以将一行分为多行显示。 MYSQL> mysql -u root -p --vartical --execute="SELECT * FROM person;" test > C:\person3.txt; # --html将表导出为html文件,--xml文件将表导出为xml文件

LOAD DATA INFILE import a text file

MYSQL> LOAD DATA INFILE 'filename.txt' INTO TABLE tablename [OPTIONS] [IGNORE number LINES];
# [IGNORE number LINES]表示忽略行数 MYSQL> LOAD DATA INFILE 'C:\person0.txt' INTO TABLE test.person;

mysqlimport import a text file

$> mysqlimport -u root -p dbname filename.txt [OPSTONS]
# 导入的表名有文件名决定,导入数据之前表必须存在
$> mysqlimport -uroot -p test C:\backup\person.txt # 将数据导入到test数据库的person表中。

Use mysqlbinlog to recover data

$> mysqlbinlog [option] filename | mysql -u user -p password
# filename为二进制日志文件,
$> mysqlbinlog --stop-date="2013-03-30 15:27:47" D:\MySQL\log\binlog\binlog.000008 | mysql -u root -p password # 根据日志文件binlog.000008将数据恢复到2013-03-30 15:27:47以前的操作。

Start binary log

log-bin = path/filename #日志文件存储目录和文件名
expire_log_days = 10    #日志自动删除时间
max_binlog_size = 100M  # 日志文件最大大小

View binary log

MYSQL> SHOW VARIABLES LIKE 'log_%';
MYSQL> SHOW BINARY LOGS; $> mysqlbinlog filename # filename为二进制日志文件名。

Delete the binary log

MYSQL> RESET MASTER; #删除所有二进制日志
MYSQL> PURGE {MASTER | BINARY} LOGS TO 'log_name'; #删除文件编号小于log_name编号的文件 MYSQL> PURGE {MASTER | BINARY} LOGS BEFORE 'date'; #删除指定日期以前的文件

Temporarily stop binary log (no need to restart mysql service)

MYSQL> SET sql_log_bin = {0|1}  #暂停或启动二进制日志。
 
 

mysqldump backup tool

Back up the entire database

$> mysqldump -u root -h host -p dbname > backdb.sql

A backup of the database table

$> mysqldump -u root -h host -p dbname tbname1, tbname2 > backdb.sql

Backup multiple databases

$> mysqldump -u root -h host -p --databases dbname1, dbname2 > backdb.sql

All database backup system

$> mysqldump -u root -h host -p --all-databases > backdb.sql

Directly copy the entire database directory (for the InnoDB storage engine NA) backup

windowns: installpath/mysql/data

linux: /var/lib/mysql

Before copying the need to execute the following command:

MYSQL> LOCK TABLES;
# 在复制过程中允许客户继续查询表,
MYSQL> FLUSH TABLES; # 将激活的索引页写入硬盘。

mysqlhotcopy backup tool

Backup of the database table or fastest route, can only run on the machine where the database directory, and can only back up MyISAM table type.

To use this backup method must be able to access the table file backup.

$> mysqlhotcopy -u root -p dbname /path/to/new_directory;
#将数据库复制到new_directory目录。

mysql import sql file restore order

$> mysql -u root -p [dbname] < backup.sql
# 执行前需要先创建dbname数据库,如果backup.sql是mysqldump创建的备份文件则执行是不需要dbname。
MYSQL> source backup.sql; # 执行source命令前需要先选择数据库。

Direct copy to restore the database directory

Note: The way to be sure the original database and consistent database major version number to be reduced, and applies only to table MyISAM engine.

  1. Shut down mysql service.
  2. The backup of the file or directory covering mysql data directory.
  3. Start mysql service.
  4. For linux systems, the user needs to change user and group files and group mysql run after the files are copied.

mysqlhotcopy fast recovery

Stop mysql service, copy the backup files to a location database (mysql data folder) to store data, the first re-start mysql service can (may need to specify the owner of the database file).

$> cp -R /usr/backup/test /usr/local/mysql/data
# 如果恢复的数据库已经存在,则使用DROP语句删除已经存在的数据库之后,恢复才能成功,还需要保证数据库版本兼容。

Migrating between versions of the same database

$> mysqldump -h www.abc.com -uroot -p password dbname | 
$> mysqldump -h www.bcd.com -uroot -p password
# 将服务器www.abc.com的数据库dbname迁移到服务器www.bcd.com的相同版本数据库上。

Migration between different versions of the mysql database

Back up the original database.

Uninstall the original database.

Install the new database.

Restore a backup of the database data in the new database.

Database users access the information you need to back up mysql database.

The default character set issues, MySQL4.x use latin1 as the default character set, mysql5.x use utf8 as the default character set. If there is Chinese data needed to make changes to the default character set.

Migration between different databases

MyODBC tool to achieve migration between MySQL and SQL Server.

MySQL Migration Toolkit tools.

Export and import table

SELECT ...... INTO OUTFILE to export a text file, which can only be exported to a database server and export the file can not already exist.

MYSQL> SELECT ...... INTO OUTFILE filename [OPTIONS]
MYSQL> SELECT * FROM test.person INTO OUTFILE "C:\person0.txt"; # 将表person里的数据导入为文本文件person0.txt。

mysqldump file export text files (and INTO OUTFILE is not the same as the method of all options need to add quotes)

$> mysqldump -T path -u root -p dbname [tables] [OPTIONS]
# -T参数表明导出文本文件。path导出数据的目录。
$> mysqldump -T C:\test person -u root -p #test表中的person表导出到文本文件。执行成功后test目录下会有两个文件,person.sql和person.txt

mysql command to export a text file

MYSQL> mysql -u root -p --execute="SELECT * FROM person;" test > C:\person3.txt;
#test数据库中的person表数据导出到person3.txt文本文件中。--vartical参数可以将一行分为多行显示。 MYSQL> mysql -u root -p --vartical --execute="SELECT * FROM person;" test > C:\person3.txt; # --html将表导出为html文件,--xml文件将表导出为xml文件

LOAD DATA INFILE import a text file

MYSQL> LOAD DATA INFILE 'filename.txt' INTO TABLE tablename [OPTIONS] [IGNORE number LINES];
# [IGNORE number LINES]表示忽略行数 MYSQL> LOAD DATA INFILE 'C:\person0.txt' INTO TABLE test.person;

mysqlimport import a text file

$> mysqlimport -u root -p dbname filename.txt [OPSTONS]
# 导入的表名有文件名决定,导入数据之前表必须存在
$> mysqlimport -uroot -p test C:\backup\person.txt # 将数据导入到test数据库的person表中。

Use mysqlbinlog to recover data

$> mysqlbinlog [option] filename | mysql -u user -p password
# filename为二进制日志文件,
$> mysqlbinlog --stop-date="2013-03-30 15:27:47" D:\MySQL\log\binlog\binlog.000008 | mysql -u root -p password # 根据日志文件binlog.000008将数据恢复到2013-03-30 15:27:47以前的操作。

Start binary log

log-bin = path/filename #日志文件存储目录和文件名
expire_log_days = 10    #日志自动删除时间
max_binlog_size = 100M  # 日志文件最大大小

View binary log

MYSQL> SHOW VARIABLES LIKE 'log_%';
MYSQL> SHOW BINARY LOGS; $> mysqlbinlog filename # filename为二进制日志文件名。

Delete the binary log

MYSQL> RESET MASTER; #删除所有二进制日志
MYSQL> PURGE {MASTER | BINARY} LOGS TO 'log_name'; #删除文件编号小于log_name编号的文件 MYSQL> PURGE {MASTER | BINARY} LOGS BEFORE 'date'; #删除指定日期以前的文件

Temporarily stop binary log (no need to restart mysql service)

MYSQL> SET sql_log_bin = {0|1}  #暂停或启动二进制日志。

Guess you like

Origin www.cnblogs.com/byne-mn/p/11301108.html