mysql 逻辑备份 恢复数据

一、逻辑备份

逻辑备份:备份的是建表,建库,插入数据等操作所执行SQL语句,适用于中小型数据库,效率相对较低,提供三种级别的备份,表级,库级和全库级。

**本质:**导出的是SQL语句

**优点:**不论是什么存储引擎,都可以用mysqldump备份成SQL语句

**缺点:**速度较慢,导出时可能会出现格式不兼容的突发状况,无法做增量备份和累计增量备份

数据一致,服务可用:如何保证数据一致,在备份的时候进行锁表会自动锁表。锁住之后在备份。

二、逻辑备份:

1、备份全部数据库

语法: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.

2.备份部分数据库

语法: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.

3.备份表

语法: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.

4.备份表结构

语法: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.

5.备份表数据

语法: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)

6.恢复

(1)命令行恢复数据库

a)命令行恢复

语法: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)
b)数据库里面恢复

语法: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)

(2)恢复表

a)在命令行恢复

语法:mysql -u用户 -p密码 表所在的数据库< 备份的文件

复制代码
[root@localhost ~]# mysql -uroot -p123  db1< employee.txt
mysql: [Warning] Using a password on the command line interface can be insecure.
b)在数据库里面恢复

语法:source +备份的路径

复制代码
mysql> source /root/employee.txt
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

(3)恢复表结构

语法: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)

(4)恢复表中数据

复制代码
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)
相关推荐
xcLeigh2 小时前
Python 项目实战:用 Flask 实现 MySQL 数据库增删改查 API
数据库·python·mysql·flask·教程·python3
威迪斯特2 小时前
Flask:轻量级Web框架的技术本质与工程实践
前端·数据库·后端·python·flask·开发框架·核心架构
xu_yule2 小时前
Redis存储(15)Redis的应用_分布式锁_Lua脚本/Redlock算法
数据库·redis·分布式
一灰灰blog2 小时前
Spring AI中的多轮对话艺术:让大模型主动提问获取明确需求
数据库·人工智能·spring
Fleshy数模2 小时前
MySQL 表创建全攻略:Navicat 图形化与 Xshell 命令行双模式实践
linux·mysql
Nandeska2 小时前
15、基于MySQL的组复制
数据库·mysql
AllData公司负责人3 小时前
AllData数据中台-数据同步平台【Seatunnel-Web】整库同步MySQL同步Doris能力演示
大数据·数据库·mysql·开源
加油,小猿猿3 小时前
Java开发日志-双数据库事务问题
java·开发语言·数据库
山岚的运维笔记4 小时前
SQL Server笔记 -- 第20章:TRY/CATCH
java·数据库·笔记·sql·microsoft·sqlserver
Gain_chance4 小时前
33-学习笔记尚硅谷数仓搭建-DWS层交易域用户粒度订单表分析及设计代码
数据库·数据仓库·hive·笔记·学习·datagrip