MySQL使用mysqldump实现数据完全备份

 更新时间:2023年07月24日 11:24:45   作者:牵着我的猪去看海  
mysqldump是MySQL自带的备份工具,可方便实现对MySQL的备份,也可以将指定的库、表导出为SQL脚本,下面小编就来教大家如何使用mysqldump实现数据完全备份吧

mysqldump备份与恢复

MySQL自带的备份工具,可方便实现对MySQL的备份

可以将指定的库、表导出为SQL脚本

使用命令mysql导入备份的数据

mysqldump -u root -p --all-databses > all-data-$(date +%F).sql ###备份所有数据库
mysqldump -u root -p -databases auth mysql > auth-mysql.sql ###备份auth和mysql库
mysqldump -u root -p auth > auth-$(data +%F).sql ###备份auth数据库
mysqldump -u root -p mysql user > mysql-user-$(date +%F).sql ###备份mysql的user表
mysqldump -u root -p -d mysql user > /tmp/desc-mysql-user.sql ###备份mysql库user表的结构
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| myadm              |
| mysql              |
| performance_schema |
| student            |
| sys                |
| tom                |
+--------------------+
7 rows in set (0.00 sec)
[root@server3 opt]# mysqldump -u root -p tom > /opt/tom.sql
Enter password: 
[root@server3 opt]# ls
tom.sql   # 导出的备份文件

对所有库进行完全备份

[root@server3 opt]# mysqldump -uroot -pabc123 --all-databases > /backup/all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

mysqldump备份数据表

musqldump可针对库内特定的表进行备份

使用mysqldump备份表的操作

mysqldump -u 用户名 -p 【密码】【选项】选项库名 表名 > /备份路径/备份文件名

示例:

mysql> use tom;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------+
| Tables_in_tom |
+---------------+
| chengji       |
+---------------+
1 row in set (0.00 sec)
mysql> select * from chengji;
+----------+-------+
| name     | point |
+----------+-------+
| xiaowang |    77 |
| xiaoli   |    75 |
+----------+-------+
2 rows in set (0.00 sec)
#复制tom表 name字段 张三内容 生成一张新表pp
mysql> create table pp as select * from chengji where name='xiaowang';
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0
mysql> show tables;
+---------------+
| Tables_in_tom |
+---------------+
| chengji       |
| pp            |
+---------------+
2 rows in set (0.00 sec)
#新生成表
mysql> select *from pp;
+----------+-------+
| name     | point |
+----------+-------+
| xiaowang |    77 |
+----------+-------+
1 row in set (0.00 sec)

[root@server3 ~]# mysql -u root -p123123 chengji pp > /opt/pp.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@server3 ~]# ls /opt
pp.sql

恢复数据库

1、使用mysqldump导出的脚本,可使用导入的方法:

1)source命令【作用于mysql模式下】2)mysql命令【作用于于linux模式下】

2、使用source恢复数据库的步骤

登录到mysql数据库

执行source备份sql脚本的路径

source恢复的示例

MYSQL[(none)]> source /backup/all-data.sql

模拟删除表

mysql> use tom;
Database changed
#删除表
mysql> drop table chengji;
Query OK, 0 rows affected (0.02 sec)
mysql> drop table pp;
Query OK, 0 rows affected (0.01 sec)

进行恢复

mysql> use tom;
Database changed
mysql> use tom;
Database changed
mysql> show tables;
+---------------+
| Tables_in_tom |
+---------------+
| tom           |
+---------------+
1 row in set (0.00 sec)
mysql> select * from tom;
+----+----------+----------+
| id | name     | address  |
+----+----------+----------+
|  1 | zhangsan | hangzhou |
+----+----------+----------+
1 row in set (0.00 sec)
mysql> insert into tom (name,address) values('lisi','wuxi');
Query OK, 1 row affected (0.00 sec)
mysql> select * from tom;
+----+----------+----------+
| id | name     | address  |
+----+----------+----------+
|  1 | zhangsan | hangzhou |
|  2 | lisi     | wuxi     |
+----+----------+----------+
2 rows in set (0.00 sec)
mysql> create table pp as select * from tom where name='zhangsan';
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0
mysql> show tables;
+---------------+
| Tables_in_tom |
+---------------+
| pp            |
| tom           |
+---------------+
2 rows in set (0.00 sec)
mysql> select * from pp;
+----+----------+----------+
| id | name     | address  |
+----+----------+----------+
|  1 | zhangsan | hangzhou |
+----+----------+----------+
1 row in set (0.00 sec)
mysql> Ctrl-C -- exit!
Aborted
[root@server3 opt]# mysqldump -u root -p tom pp > /opt/pp.sql
Enter password: 
[root@server3 opt]# ls /opt/
all.sql  opt.sql  pp.sql  rh  tom.tom
[root@server3 opt]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.36-log Source distribution
Copyright (c) 2000, 2017, 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> drop table tom;
ERROR 1046 (3D000): No database selected
mysql> use tom;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> drop table tom;
Query OK, 0 rows affected (0.01 sec)
mysql> drop table pp;
Query OK, 0 rows affected (0.00 sec)
```java
mysql> show tables;
Empty set (0.00 sec)
#恢复
mysql> source /opt/all.sql;
..省略内容
mysql> use tom;
Database changed
mysql> show tables;
+---------------+
| Tables_in_tom |
+---------------+
| tom           |
+---------------+
1 row in set (0.00 sec)
mysql> source /opt/pp.sql;
mysql> show tables;
+---------------+
| Tables_in_tom |
+---------------+
| pp            |
| tom           |
+---------------+
2 rows in set (0.00 sec)
mysql> select * from pp;
+----+----------+----------+
| id | name     | address  |
+----+----------+----------+
|  1 | zhangsan | hangzhou |
+----+----------+----------+
1 row in set (0.00 sec)

已经恢复  这边我们是恢复所有数据库  

也可以单独的对标进行备份恢复

到此这篇关于MySQL使用mysqldump实现数据完全备份的文章就介绍到这了,更多相关MySQL mysqldump数据备份内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • MySql数据库备份的几种方式

    MySql数据库备份的几种方式

    这篇文章主要介绍了MySql数据库备份的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • DBeaver如何将mysql表结构以表格形式导出

    DBeaver如何将mysql表结构以表格形式导出

    DBeaver是一款多功能数据库工具,支持包括MySQL在内的多种数据库,本文介绍如何使用DBeaver将MySQL的表结构以表格形式导出,为数据库管理和文档整理提供便利,这种方法简洁有效,适合需要文档化数据库结构的开发者和数据库管理员
    2024-10-10
  • MySQL创建和删除表操作命令实例讲解

    MySQL创建和删除表操作命令实例讲解

    这篇文章主要介绍了MySQL创建和删除表操作命令实例讲解,本文讲解了创建表、创建临时表、查看已经创建的mysql表等内容,需要的朋友可以参考下
    2014-12-12
  • MySQL PXC构建一个新节点只需IST传输的方法(推荐)

    MySQL PXC构建一个新节点只需IST传输的方法(推荐)

    下面小编就为大家带来一篇MySQL PXC构建一个新节点只需IST传输的方法(推荐)。小编觉的挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • MySQL/MariaDB中如何支持全部的Unicode

    MySQL/MariaDB中如何支持全部的Unicode

    MySQL/MariaDB中,utf8字符集并不是对Unicode的真正实现,那么MySQL/MariaDB中如何支持全部的Unicode,感兴趣的朋友可以了解一下
    2021-08-08
  • Mysql GROUP BY查询每组某值最大的一条数据

    Mysql GROUP BY查询每组某值最大的一条数据

    这篇文章主要介绍了Mysql GROUP BY查询每组某值最大的一条数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • MySQL数据库8——数据库中函数的应用详解

    MySQL数据库8——数据库中函数的应用详解

    这篇文章主要介绍了MySQL数据库8——数据库中函数的应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • mysql常用函数汇总(分享)

    mysql常用函数汇总(分享)

    以下是对mysql中的常用函数进行了汇总介绍。需要的朋友可以过来参考下
    2013-08-08
  • Mysql隔离性之Read View的用法说明

    Mysql隔离性之Read View的用法说明

    这篇文章主要介绍了Mysql隔离性之Read View的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • mysql增量备份及恢复的操作方法

    mysql增量备份及恢复的操作方法

    增量备份是在全备或上次增量备份基础上,只备份新增或修改的文件,减少数据量和时间,binlog记录数据库变更,重启时创建新日志文件,增量备份复杂但减轻服务器负担,而binlog帮助精确恢复数据
    2023-09-09

最新评论