逻辑备份:备份的是建表,建库,插入数据等操作所执行SQL语句,适用于中小型数据库,效率相对较低,提供三种级别的备份,表级,库级和全库级。
本质:导出的是SQL语句
优点:不论是什么存储引擎,都可以用mysqldump备份成SQL语句
缺点:速度较慢,导出时可能会出现格式不兼容的突发状况,无法做增量备份和累计增量备份
数据一致,服务可用:如何保证数据一致,在备份的时候进行锁表会自动锁表。锁住之后在备份。
语法:mysqldump -u指定用户 -p指定密码 -A > 文件名
mysqldump -u指定用户 -p指定密码 --all-databases > 文件名
- [root@localhost ~]# mysqldump -uroot -p123 -A > all.mysql
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
-
- [root@localhost ~]# mysqldump -uroot -p123 --all-database >all1.txt
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
语法:mysqldump -u指定用户 -p密码 -B 数据库名1 数据库名2> 文件名
mysqldump -u指定用户 -p密码 --databases 数据库名1 数据库名2 > 文件名
- [root@localhost ~]# mysqldump -uroot -p123 -B db1 >db1.txt
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
-
- [root@localhost ~]# mysqldump -uroot -p123 --databases db1 school > db2.txt
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
语法:mysqldump -u指定用户 -p指定密码 数据库名 表名 > 文件名
- [root@localhost ~]# mysqldump -uroot -p123 db1 employee > employee.txt
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
语法:mysqldump -u指定用户 -p指定密码 -d 数据库名 表名
- [root@localhost ~]# mysqldump -uroot -p123 -d db1 employee > jiegou.txt
- mysqldump: [Warning] Using a password on the command line interface can be insecure.
语法:select * from 表.库 into outfile'/var/lib/mysql-files/文件名';
- mysql> show variables like 'secure%'; #查看默认导出路径
- +------------------+-----------------------+
- | Variable_name | Value |
- +------------------+-----------------------+
- | secure_auth | ON |
- | secure_file_priv | /var/lib/mysql-files/ |
- +------------------+-----------------------+
- 2 rows in set (0.00 sec)
-
- mysql> select * from mysql.user into outfile '/var/lib/mysql-files/b.xfs';
- Query OK, 6 rows affected (0.00 sec)
- [root@localhost opt]# mkdir backup #创建默认目录
- [root@localhost opt]# chown -R mysql.mysql /opt/backup#修改目录的属主和属组
- [root@localhost opt]# vim /etc/my.cnf #修改配置文件
- secure_file_priv=/opt/backup
- [root@localhost opt]# systemctl restart mysqld #重启mysql
-
- [root@localhost opt]# mysql -p123 #进入mysql
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.7.43 MySQL Community Server (GPL)
-
- Copyright (c) 2000, 2023, Oracle and/or its affiliates.
-
- 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> show variables like 'secure%'; #再次查看文件的默认路径
- +------------------+--------------+
- | Variable_name | Value |
- +------------------+--------------+
- | secure_auth | ON |
- | secure_file_priv | /opt/backup/ |
- +------------------+--------------+
- 2 rows in set (0.01 sec)
语法:mysql -u用户名 -p密码 < 之前备份的文件
- mysql> drop database db1; #先删除数据库db1
- Query OK, 15 rows affected (0.07 sec)
- mysql> show databases; #查看数据库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | db3 |
- | mysql |
- | performance_schema |
- | school |
- | sys |
- +--------------------+
- 6 rows in set (0.00 sec)
-
- [root@localhost ~]# mysql -uroot -p123 < db1.txt #恢复数据库
- mysql: [Warning] Using a password on the command line interface can be insecure.
- mysql> show databases; #再次查看数据库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | db1 |
- | db3 |
- | mysql |
- | performance_schema |
- | school |
- | sys |
- +--------------------+
- 7 rows in set (0.00 sec)
语法:source +备份数据库的路径
- mysql> drop database db1; #删除数据库db1
- Query OK, 15 rows affected (0.05 sec)
-
- mysql> source /root/db1.txt #恢复数据库db1
- Query OK, 0 rows affected (0.00 sec)
-
- .
- .
-
- Query OK, 0 rows affected (0.00 sec)
-
-
- mysql> show databases; #查看数据库
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | db1 |
- | db3 |
- | mysql |
- | performance_schema |
- | school |
- | sys |
- +--------------------+
- 7 rows in set (0.00 sec)
语法:mysql -u用户 -p密码 表所在的数据库< 备份的文件
- [root@localhost ~]# mysql -uroot -p123 db1< employee.txt
- mysql: [Warning] Using a password on the command line interface can be insecure.
语法:source +备份的路径
- mysql> source /root/employee.txt
- Query OK, 0 rows affected (0.00 sec)
-
- Query OK, 0 rows affected (0.00 sec)
语法:mysql -u用户 -p密码 -D 数据库名 < 备份的文件
- [root@localhost ~]# mysql -uroot -p123 -D db1 <jiegou.txt
- mysql: [Warning] Using a password on the command line interface can be insecure.
-
- mysql> desc employee;
- +-----------------+---------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-----------------+---------------------+------+-----+---------+----------------+
- | id | int(11) | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | NULL | |
- | sex | enum('man','woman') | YES | | man | |
- | hire_date | date | YES | | NULL | |
- | post | varchar(20) | YES | | NULL | |
- | job_description | varchar(100) | YES | | NULL | |
- | salary | double(15,2) | NO | | NULL | |
- | office | int(11) | YES | | NULL | |
- | dep_id | int(11) | YES | | NULL | |
- +-----------------+---------------------+------+-----+---------+----------------+
- 9 rows in set (0.00 sec)
-
- mysql> select * from employee;
- Empty set (0.00 sec)
- mysql> truncate employee; #清空表中数据
- Query OK, 0 rows affected (0.02 sec)
-
- mysql> select * from employee; #查看表中数据
- Empty set (0.00 sec)
-
- mysql> load data infile'/opt/backup/a.txt'into table employee;
- Query OK, 15 rows affected (0.01 sec)
- Records: 15 Deleted: 0 Skipped: 0 Warnings: 0
-
- mysql> select * from employee;
- +----+-----------+-------+------------+------------+-----------------+----------+--------+--------+
- | id | name | sex | hire_date | post | job_description | salary | office | dep_id |
- +----+-----------+-------+------------+------------+-----------------+----------+--------+--------+
- | 1 | qiancheng | man | 2018-03-14 | hr | talk | 7000.00 | 501 | 102 |
- | 20 | tom | man | 2017-09-15 | instructor | teach | 8000.00 | 501 | 100 |
- | 21 | alince | woman | 2013-04-28 | instructor | teach | 5500.00 | 501 | 100 |
- | 22 | robin | man | 2020-09-18 | instructor | teach | 7200.00 | 501 | 100 |
- | 23 | zhuzhu | man | 2016-12-09 | hr | hrcc | 6000.00 | 502 | 101 |
- | 24 | gougou | woman | 2015-04-27 | hr | NULL | 6000.00 | 502 | 101 |
- | 30 | maomao | man | 2019-08-12 | sale | talk | 20000.00 | 503 | 102 |
- | 31 | yiyi | man | 2015-06-17 | talk | NULL | 8000.00 | NULL | NULL |
- | 40 | harry | woman | 2018-02-05 | hr | hrcc | 6900.00 | 502 | 102 |
- | 41 | tianyuan | man | 2018-02-05 | null | salecc | 9700.00 | 501 | 102 |
- | 42 | xiaoyi | man | 2018-02-05 | null | salecc | 5700.00 | 501 | 102 |
- | 50 | zxvb | man | 2019-04-23 | hr | NULL | 8000.00 | NULL | NULL |
- | 51 | ab | man | NULL | NULL | NULL | 6500.00 | NULL | NULL |
- | 52 | cd | man | NULL | NULL | NULL | 7600.00 | NULL | NULL |
- | 53 | ef | man | NULL | NULL | NULL | 8900.00 | NULL | NULL |
- +----+-----------+-------+------------+------------+-----------------+----------+--------+--------+
- 15 rows in set (0.00 sec)

- [root@localhost ~]# vim /etc/my.cnf
- server-id=1 #添加server-id
- log-bin = /opt/log/mysql-bin.log #指定binlog日志文件的存放位置和名称,位置和名称都可以自定义
[root@localhost ~]# chown -R mysql.mysql /opt/log
- [root@localhost ~]# systemctl restart mysqld
- [root@localhost ~]# cd /opt/log
- [root@localhost log]# ls
- mysql-bin.000001 mysql-bin.index
mysql> flush logs; #刷新binlog日志,使下面的语句存放到下一个binlog日志中
- mysql> show master status;
- +------------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +------------------+----------+--------------+------------------+-------------------+
- | mysql-bin.000002 | 154 | | | |
- +------------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- [root@localhost ~]# cd /opt/log
- [root@localhost log]# ls
- mysql-bin.000001 mysql-bin.000002 mysql-bin.index
- [root@localhost log]# mysqlbinlog mysql-bin.000002 --base64-output=decode-rows -vv
- /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
- /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
- DELIMITER /*!*/;
- # at 4
- #231007 19:40:11 server id 1 end_log_pos 123 CRC32 0xa788ae39 Start: binlog v 4, server v 5.7.43-log created 231007 19:40:11
- # Warning: this binlog is either in use or was not closed properly.
- # at 123
- #231007 19:40:11 server id 1 end_log_pos 154 CRC32 0xd228507b Previous-GTIDs
- # [empty]
- # at 154
- #231007 19:45:19 server id 1 end_log_pos 219 CRC32 0xdf49c909 Anonymous_GTID last_committed=0 sequence_number=1 rbr_only=no
- SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
- # at 219
- #231007 19:45:19 server id 1 end_log_pos 329 CRC32 0x7f94b39e Query thread_id=3 exec_time=0 error_code=0
- SET TIMESTAMP=1696679119/*!*/;
- SET @@session.pseudo_thread_id=3/*!*/;
- SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
- SET @@session.sql_mode=1436549152/*!*/;
- SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
- /*!\C utf8 *//*!*/;
- SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
- SET @@session.lc_time_names=0/*!*/;
- SET @@session.collation_database=DEFAULT/*!*/;
- create database db default charset'utf8'
- /*!*/;
- # at 329
- #231007 19:46:20 server id 1 end_log_pos 394 CRC32 0xc79d4673 Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=no
- SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
- # at 394
- #231007 19:46:20 server id 1 end_log_pos 513 CRC32 0xeca5dcea Query thread_id=3 exec_time=0 error_code=0
- use `db`/*!*/;
- SET TIMESTAMP=1696679180/*!*/;
- create table db1(id int,name varchar(30),age int)
- /*!*/;
- # at 513
- #231007 19:47:58 server id 1 end_log_pos 578 CRC32 0x502f26e0 Anonymous_GTID last_committed=2 sequence_number=3 rbr_only=yes
- /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
- SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
- # at 578
- #231007 19:47:58 server id 1 end_log_pos 648 CRC32 0x1c085c1f Query thread_id=3 exec_time=0 error_code=0
- SET TIMESTAMP=1696679278/*!*/;
- BEGIN
- /*!*/;
- # at 648
- #231007 19:47:58 server id 1 end_log_pos 696 CRC32 0x836eb6d7 Table_map: `db`.`db1` mapped to number 109
- # at 696
- #231007 19:47:58 server id 1 end_log_pos 784 CRC32 0x9580d081 Write_rows: table id 109 flags: STMT_END_F
- ### INSERT INTO `db`.`db1`
- ### SET
- ### @1=1 /* INT meta=0 nullable=1 is_null=0 */
- ### @2='xiaoli' /* VARSTRING(90) meta=90 nullable=1 is_null=0 */
- ### @3=23 /* INT meta=0 nullable=1 is_null=0 */
- ### INSERT INTO `db`.`db1`
- ### SET
- ### @1=2 /* INT meta=0 nullable=1 is_null=0 */
- ### @2='xiaozhang' /* VARSTRING(90) meta=90 nullable=1 is_null=0 */
- ### @3=34 /* INT meta=0 nullable=1 is_null=0 */
- ### INSERT INTO `db`.`db1`
- ### SET
- ### @1=3 /* INT meta=0 nullable=1 is_null=0 */
- ### @2='zhangsan' /* VARSTRING(90) meta=90 nullable=1 is_null=0 */
- ### @3=42 /* INT meta=0 nullable=1 is_null=0 */
- # at 784
- #231007 19:47:58 server id 1 end_log_pos 815 CRC32 0xe927fd1d Xid = 12
- COMMIT/*!*/;
- # at 815
- #231007 19:48:10 server id 1 end_log_pos 880 CRC32 0xcdc9d73a Anonymous_GTID last_committed=3 sequence_number=4 rbr_only=yes
- /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
- SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
- # at 880
- #231007 19:48:10 server id 1 end_log_pos 950 CRC32 0x0a8ade69 Query thread_id=3 exec_time=0 error_code=0
- SET TIMESTAMP=1696679290/*!*/;
- BEGIN
- /*!*/;
- # at 950
- #231007 19:48:10 server id 1 end_log_pos 998 CRC32 0x68b7b908 Table_map: `db`.`db1` mapped to number 109
- # at 998
- #231007 19:48:10 server id 1 end_log_pos 1086 CRC32 0xc1c77e9d Delete_rows: table id 109 flags: STMT_END_F
- ### DELETE FROM `db`.`db1`
- ### WHERE
- ### @1=1 /* INT meta=0 nullable=1 is_null=0 */
- ### @2='xiaoli' /* VARSTRING(90) meta=90 nullable=1 is_null=0 */
- ### @3=23 /* INT meta=0 nullable=1 is_null=0 */
- ### DELETE FROM `db`.`db1`
- ### WHERE
- ### @1=2 /* INT meta=0 nullable=1 is_null=0 */
- ### @2='xiaozhang' /* VARSTRING(90) meta=90 nullable=1 is_null=0 */
- ### @3=34 /* INT meta=0 nullable=1 is_null=0 */
- ### DELETE FROM `db`.`db1`
- ### WHERE
- ### @1=3 /* INT meta=0 nullable=1 is_null=0 */
- ### @2='zhangsan' /* VARSTRING(90) meta=90 nullable=1 is_null=0 */
- ### @3=42 /* INT meta=0 nullable=1 is_null=0 */
- # at 1086
- #231007 19:48:10 server id 1 end_log_pos 1117 CRC32 0x6cca4c23 Xid = 13
- COMMIT/*!*/;
- SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
- DELIMITER ;
- # End of log file
- /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
- /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
由题可知要恢复的起始位置和结束位置分别为648,950
- [root@localhost log]# mysqlbinlog --start-position 648 --stop-position 950 mysql-bin.000002 |mysql -uroot -p123
- mysql: [Warning] Using a password on the command line interface can be insecure.
- mysql> select * from db.db1;
- +------+-----------+------+
- | id | name | age |
- +------+-----------+------+
- | 1 | xiaoli | 23 |
- | 2 | xiaozhang | 34 |
- | 3 | zhangsan | 42 |
- +------+-----------+------+
- 3 rows in set (0.00 sec)
- [root@localhost log]# mysqlbinlog --start-datetime='2023-10-07 19:47:58' --stop-datetime='2023-10-07 19:48:10' mysql-bin.000002 |mysql -uroot -p123
- mysql: [Warning] Using a password on the command line interface can be insecure.
- mysql> select * from db.db1;
- +------+-----------+------+
- | id | name | age |
- +------+-----------+------+
- | 1 | xiaoli | 23 |
- | 2 | xiaozhang | 34 |
- | 3 | zhangsan | 42 |
- +------+-----------+------+
- 3 rows in set (0.00 sec)