背景:数据库表记录内容过多,使用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