当前位置:网站首页>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的库表中
边栏推荐
- 适合程序员学习的国外网站推荐
- Zhang Lijun: penetrating uncertainty depends on four "invariants"
- SD card reports an error "error -110 whilst initializing SD card
- Which ecology is better, such as Mi family, graffiti, hilink, zhiting, etc? Analysis of five mainstream smart brands
- Pat 1084 broken keyboard (20 points) string find
- CSP date calculation
- 有没有完全自主的国产化数据库技术
- How to do function test well
- My C language learning record (blue bridge) -- under the pointer
- Apt installation ZABBIX
猜你喜欢

Game theory matlab

What is the investment value of iFLYTEK, which does not make money?

PMP每日一练 | 考试不迷路-7.5

IPv6 jobs
![Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]](/img/3b/385d19e51340ecd6281df47b39f40c.png)
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]

Performance test method of bank core business system
![[matlab] access of variables and files](/img/cf/6f3cfdc4310fcf0bdcaa776d68261e.jpg)
[matlab] access of variables and files

ERA5再分析资料下载攻略

NR modulation 1

Derivation of anti Park transform and anti Clarke transform formulas for motor control
随机推荐
tcpdump: no suitable device found
Rust language -- iterators and closures
[kubernetes series] learn the exposed application of kubernetes service security
Zhang Lijun: penetrating uncertainty depends on four "invariants"
【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
07 singleton mode
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
C # create self host webservice
Differences and application scenarios between resulttype and resultmap
Communication between microservices
How to read excel, PDF and JSON files in R language?
RobotFramework入门(一)简要介绍及使用
Is there a completely independent localization database technology
How to improve the enthusiasm of consumers when the member points marketing system is operated?
Redis cache breakdown, cache penetration, cache avalanche
Summary of Bible story reading
What are the principles of software design (OCP)
Who is the winner of PTA
Linear regression and logistic regression
Solution: attributeerror: 'STR' object has no attribute 'decode‘