当前位置:网站首页>mysqldump数据备份
mysqldump数据备份
2022-07-06 03:09:00 【菜鸟~~】
上一篇博客我们讲了用mysqlbinlog工具(mysql原生自带的工具)介意快速解析大量的binlog日志文件,并使用二进制日志进行数据恢复的应用实践,这篇博客我们讲一下数据备份先关的命令,因为我们做后台开发一般工作在shell下,而且部署在后台服务器或者是云端的mysql,大部分做了一些限制,我们可能在本地无法直连到后台服务的数据库3306端口上,一般都会有防火墙之类的网络的中间键,没有条件用GUI(图形界面工具)鼠标操作做数据备份之类的,只能通过命令,而且命令也是最快速的,所以这篇博客讲一下常用的数据备份,我们一般通过mysqldump进行。
在linux shell下执行以下命令,即可把mytest库的user表的数据导出到.sql文件中(导出的不仅是数据,SQL语句也导出了)
vim user.sql查看user.sql文件内容如下:

现在mytest库里面,我们把表删了,或者是我们向进行数据迁移,在另一个库上重建这个表。我们有了这个sql脚本在root根目录下放着,权限不够,要在普通用户下执行,所以把user.sql移到普通用户下(或者我们可以一开始就在普通用户下执行mysqldump -u root -p mytest user > ~/user.sql把mytest库的user表的数据导出到.sql文件中,就不用执行接下来的步骤,直接执行source /home/admin/user.sql,就可以恢复出user表和数据)


现在user.sql是root的文件,我们变更一下属主,使得普通用户也可以操作

然后我们source,相当于把.sql脚本重新执行一遍

user表和数据全部恢复出来了,有了.sql的脚本,可以在任意的mysql库上去重建库表及数据
我们还可以直接导出纯表数据
user.txt内容如下:
这样就把user表里面的数据全部导出来了,我们再去用python或者go去写一些脚本进行数据分析的时候,可以通过-t来作为来作为分隔符把每一个字段的数据都获取出来做额外的数据分析,比如分析用户的行为,建立用户的画像等等。
mysqldump数据备份命令总结
- 导出建库建表的SQL
//导出所有库
mysqldump -u 用户名 -p --all-databases > ~/xxx.sql
//一次可以导出多个库
mysqldump -u 用户名 -p --databases db1[db2] > ~/xxx.sql
//导出库或者库里面的某张表
mysqldump -u 用户名 -p dbname [tablename]> ~/xxx.sql
- 导出纯数据
mysql -u 用户名 -p -D school -e 'select * from user where age>10' > ~/user.txt
- 通过备份的.sql,导入数据,建库建表
登录mysql,在mysql的shell上执行下面语句
source ~/school.sql
或者直接在linux的shell下执行
cat ~/data.sql|mysql -u root -p
这样就可以把我们之前备份的数据到mysql的库表中
边栏推荐
- Problems encountered in 2022 work IV
- Leetcode problem solving -- 98 Validate binary search tree
- How to do function test well
- C # create self host webservice
- Inherit day01
- Universal crud interface
- Installation and use tutorial of cobaltstrike-4.4-k8 modified version
- These are not very good
- MySQL advanced notes
- 解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
猜你喜欢
随机推荐
Differences and application scenarios between resulttype and resultmap
Performance analysis of user login TPS low and CPU full
Web security SQL injection vulnerability (1)
Solution: attributeerror: 'STR' object has no attribute 'decode‘
2.13 simulation summary
4. File modification
[network security interview question] - how to penetrate the test file directory through
Audio-AudioRecord Binder通信机制
Analyze 菜单分析
NR modulation 1
Audio audiorecord binder communication mechanism
Custom attribute access__ getattribute__/ Settings__ setattr__/ Delete__ delattr__ method
Function knowledge points
XSS challenges绕过防护策略进行 XSS 注入
【概念】Web 基础概念认知
有没有完全自主的国产化数据库技术
Summary of Bible story reading
Leetcode problem solving -- 98 Validate binary search tree
ArabellaCPC 2019(补题)
How does yyds dry inventory deal with repeated messages in the consumption process?









