MySQL | MySQL Database System (4)-Database Backup and Recovery

MySQL | MySQL Database System (4)-Database Backup and Recovery

Foreword
In your daily work, backing up data is actually one of the important tasks of information security management. So, we will introduce database backup and recovery in this article. There are multiple ways to backup MySQL database at the same time. First: directly package the database folder /etc/local/mysql/data, or we can use tools for backup.

1. The backup database
can export the specified library, the specified table or the entire library as SQL scripts through the mysqldump command. When you need to upgrade the MySQL server, you can export the original library information and then directly import it into the upgraded MySQL server.

Perform the export
when using the command mysqldump export data, the default will be displayed directly in the terminal. If you want to save to a file, you also need to combine> redirect output operations.

Basic syntax format
1) Export part of the table in the specified library.

mysqldump [options] library name [table name 1] [table name 2] ···> /backup path/backup file name
2) Export one or more complete libraries, including all tables.

mysqldump [Options] --databases library name 1 [library name 2] ···> /backup path/backup file name
3) Back up all the libraries in the MySQL server

mysqldump [Options] --all-databases> /backup path/backup file name
Commonly used options include -u, -p, which are used to specify the database user name and password respectively.

The above basic syntax format 1 will be used to export the user table in the MySQL library as a mysql-user.sql file, and the basic syntax format 2 will be used to export the entire auth library as an auth.sql file, all operations in the two basic syntax formats Both require root user authentication.


[root@localhost ~]# mysqldump -u root -p mysql user > mysql-user.sql
Enter password: 
[root@localhost ~]# mysqldump -u root -p --databases auth > auth.sql
Enter password: 

When you want to back up all the libraries in the entire MySQL server, you can use the basic syntax format 3 above. When the amount of exported data is large, you can add the option --opt to optimize the execution speed.

The backup file all-data.sql will be created, and it will include all the libraries in the MySQL server.


[root@localhost ~]# mysqldump -u root -p --opt --all-databases > all-data.sql
Enter password: 

View the contents of the backup file.
The SQL script exported through the tool mysqldump is a text file, and the part at the beginning of / ··· / or - indicates comment information.

Use grep, less, cat and other tools to view the detailed script content.

The database operation statements in the auth.sql script will be filtered out.


[root@localhost ~]# grep -v "^--" auth.sql | grep -v "^/" | grep -v "^$"
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `auth` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `auth`;
DROP TABLE IF EXISTS `servers`;
CREATE TABLE `servers` (
  `Server_name` char(64) NOT NULL,
  `Host` char(64) NOT NULL,
  `Db` char(64) NOT NULL,
  `Username` char(64) NOT NULL,
  `Password` char(64) NOT NULL,
  `Port` int(4) DEFAULT NULL,
  `Socket` char(64) DEFAULT NULL,
  `Wrapper` char(64) NOT NULL,
  `Owner` char(64) NOT NULL,
  PRIMARY KEY (`Server_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='MySQL Foreign Servers table';
LOCK TABLES `servers` WRITE;
UNLOCK TABLES;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `user_name` char(16) NOT NULL,
  `user_passwd` char(48) DEFAULT '',
  PRIMARY KEY (`user_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
LOCK TABLES `users` WRITE;
INSERT INTO `users` VALUES ('jacktian','*B2B366CA5C4697F31D4C55D61F0B17E70E5664EC');
UNLOCK TABLES;

2. When recovering the database
, you can import it through the command mysql.

Basic syntax


mysql [选项] [库名] [表名] < /备份路径/备份文件名

When the backup file only contains the backup of the table and does not contain the statement of the library, the library name must be specified when importing, and the target library must exist.

You can import tables into the test library from the backup file mysql-user.sql.


[root@localhost ~]# mysql -u root -p test < mysql-user.sql
Enter password: 
[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.22-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test;                                   ## 验证导入结果
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user           |
+----------------+
1 row in set (0.00 sec)

mysql> 

If the complete library information is already included in the backup file, you do not need to specify the library name when performing the import operation, and you can restore the auth library from the backup file auth.sql.


[root@localhost ~]# cd /usr/local/mysql/data/
[root@localhost data]# ls
auth     ib_logfile0  localhost.err  mongodb  mysql-bin.000001  mysql-bin.000003  mysql-bin.index  performance_schema
ibdata1  ib_logfile1  localhost.pid  mysql    mysql-bin.000002  mysql-bin.000004  mysql.error.log  test
[root@localhost data]# mv auth /tmp/                           ## 移动 auth 库,进行模拟故障操作。
[root@localhost data]# ls -ld auth
ls: 无法访问auth: 没有那个文件或目录
[root@localhost data]# mysql -u root -p < ~/auth.sql           ## 执行导入恢复操作。
Enter password: 
ERROR 1050 (42S01) at line 63: Table '`auth`.`users`' already exists
[root@localhost data]# ls -ld auth                             ## 确认恢复后的结果。
drwx------. 2 mysql mysql 4096 8月   1 05:17 auth
[root@localhost data]# 

Guess you like

Origin blog.51cto.com/15067236/2606202