Mysql之备份(Mysqldump)

本篇文章旨在介绍Mysql的备份,借助mysqldump命令。

1.准备数据

准备一个数据库d1,表t1
表结构如下:

mysql> desc t1;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| xh    | int        | NO   | PRI | NULL    | auto_increment |
| xm    | varchar(5) | YES  |     | NULL    |                |
+-------+------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

插入数据后,内容如下:

mysql> select * from t1;
+----+------+
| xh | xm   |
+----+------+
|  1 | zs   |
|  2 | ls   |
+----+------+
2 rows in set (0.00 sec)
2.实验开始
a-备份

mysqldump命令的格式:

mysqldump -h 127.0.0.1 -u root -p666 xxx > f:\数据库备份练习\xxx.sql
#127.0.0.1为ip地址
#root为用户名
#666为密码
#xxx为存储的数据库名
#>后为要存储的位置以及文件名

注意是在操作系统命令行输入,不是在mysql里。

[root@VM-4-9-centos ~]# mysqldump -u root -p d1 > d1.bak
Enter password:
[root@VM-4-9-centos ~]# ls
d1.bak 
b-还原

在还原前,我们需要先建立数据库。

#删除数据库
mysql> drop database d1;
Query OK, 1 row affected (0.03 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

#还原
[root@VM-4-9-centos ~]# mysql -u root -p d1 < d1.bak
Enter password:

查看还原效果:

mysql> use d1;
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> desc t1;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| xh    | int        | NO   | PRI | NULL    | auto_increment |
| xm    | varchar(5) | YES  |     | NULL    |                |
+-------+------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> select * from t1;
+----+------+
| xh | xm   |
+----+------+
|  1 | zs   |
|  2 | ls   |
+----+------+
2 rows in set (0.00 sec)
c-原理
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不是少爷.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值