背景:数据库表记录内容过多,使用navicat的数据同步功能耗时漫长
1、备份数据库
#备份命令,其中yuesheng_novel为需要备份的数据库名,备份到/usr/local目录下
[root@izwz98l0u5sq2hwfpuszvoz ~]# mysqldump -u root -p yuesheng_novel >/usr/local/yuesheng_novel.sql
Enter password:
2、压缩(带宽限制,不压缩,下载太久了)
#压缩命令,进入到文件所在目录,直接执行
[root@izwz98l0u5sq2hwfpuszvoz local]# tar -zcvf yuesheng_novel.tar.gz yuesheng_novel.sql
#压缩后,查看一下压缩结果,可以看到,329M压缩成了122M
[root@izwz98l0u5sq2hwfpuszvoz local]# du -sh * | sort -n
122M yuesheng_novel.tar.gz
329M yuesheng_novel.sql
3、使用FTP工具讲压缩文件下载到本地,然后上传到新的服务器
4、进入新服务器解压
#进入文件所在目录
[root@VM_0_12_centos ~]# cd /yuesheng/
[root@VM_0_12_centos yuesheng]# ls
yuesheng_novel.tar.gz
#使用解压命令解压,使用tar -zxvf yuesheng_novel.tar.gz -C new_dir,可以指定解压目录
[root@VM_0_12_centos yuesheng]# tar -zxvf yuesheng_novel.tar.gz
yuesheng_novel.sql
[root@VM_0_12_centos yuesheng]# ls
yuesheng_novel.sql yuesheng_novel.tar.gz
#查看解压结果
[root@VM_0_12_centos yuesheng]# du -sh * | sort -n
122M yuesheng_novel.tar.gz
329M yuesheng_novel.sql
5、建立同名数据库
6、备份恢复
#进入mysql
[root@VM_0_12_centos yuesheng]# mysql -u root -p
Enter password:
#选择数据库
mysql> use yuesheng_novel
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> source /yuesheng/yuesheng_novel.sql
#重新备份,做一次校验
[root@VM_0_12_centos yuesheng]# mysqldump -u root -p yuesheng_novel > /yuesheng/test.sql
Enter password:
[root@VM_0_12_centos yuesheng]# du -sh * | sort -n
122M yuesheng_novel.tar.gz
329M test.sql
329M yuesheng_novel.sql
扩展:文件压缩和解压
注:filename是待压缩全文件名,test为压缩后文件名,不含后缀,new_dir为解压路径,例如:/usr/local
格式 | 描述 | 压缩 | 解压 | 指定解压目录 |
---|---|---|---|---|
zip格式 | 使用最多,跨平台,但是压缩率不高 | zip -r test.zip filename | unzip test.zip | unzip test.zip -d new_dir |
tar格式 | linux文档打包格式,耗用CPU和时间少,只打包,不压缩 | tar -cvf test.tar dir | tar -xvf test.tar | tar -xvf test.tar -C new_dir |
tar.gz格式 | 常用,耗用cpu一版,压缩率理想 | tar -zcvf test.tar.gz filename | tar -zxvf test.tar.gz | tar -zxvf test.tar.gz -C new_dir |
tar.bz2格式 | 压缩率最高,耗用cpu和时间多 | tar -jcvf test.tar.bz2 filename | tar -jxvf test.tar.bz2 | tar -jxvf test.tar.bz2 -C new_dir |
#linux使用zip压缩需要先yum安装依赖
yum install -y unzip zip
#未安装报错
-bash: zip: command not found
-bash: unzip: command not found
#使用tar.bz2压缩需要先yum安装依赖
yum -y install bzip2
#未安装报错
tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now