折腾:
期间,需要想办法去把阿里云的RDS服务器上的mysql数据库,其只允许内网访问。
现在想办法下载到本地。用于测试。
阿里云 rds mysql 导出
自己去试了试:
貌似可以通过登录数据库然后用命令去导出?
迁移 RDS for MySQL 数据到本地 MySQL_从 RDS 到本地数据库_数据迁移_用户指南_云数据库 RDS 版-阿里云
下载数据备份和日志备份_备份与恢复_用户指南_云数据库 RDS 版-阿里云
好像是需要:
先去开通暂时免费的备份
然后下载备份数据
RDS-MySQL通过数据集成导入/导出_最佳实践_数据集成-阿里云
Linux VPS/服务器上轻松导入、导出MySQL数据库 – 阿里云
在阿里云Rds上采用MySQLdump工具对数据进行导出导入|云分享 – 开发者论坛
算了,还是暂时考虑用
mysqldump
去试试吧
结果发现:
另外一台阿里云ECS服务器空间暂时只剩5G多
而本身mysql就5G多,为了防止ECS服务器空间满了而挂掉
所以等同事来了,清空空间后,再去内网连接后,用mysqldump去导出
现在先去看看登录后如何,结果需要处理:
现在服务器已经有足够的11G的空间,供我去备份5G多的mysql了:
<code>[root@xxx-general-01 crawler]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/vda1 50G 36G 11G 77% / devtmpfs 7.8G 0 7.8G 0% /dev tmpfs 7.8G 0 7.8G 0% /dev/shm tmpfs 7.8G 456K 7.8G 1% /run tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup tmpfs 1.6G 0 1.6G 0% /run/user/0 </code>
此处背景再说一下:
阿里云的一个ECS服务器:
阿里云的RDS的mysql服务器
要在ECS中去用mysqldump去导出RDS的mysql的数据
然后再想办法,把ECS中备份好的mysql,下载到Mac本地,用于搭建本地mysql,用于测试代码。
先去看help:
<code>[root@xxx-general-01 for_bacup_mysql]# mysqldump --help mysqldump Ver 10.14 Distrib 5.5.56-MariaDB, for Linux (x86_64) Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Dumping structure and contents of MySQL databases and tables. Usage: mysqldump [OPTIONS] database [tables] OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] OR mysqldump [OPTIONS] --all-databases [OPTIONS] Default options are read from the following files in the given order: /etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf The following groups are read: mysqldump client client-server client-mariadb The following options may be given as the first argument: --print-defaults Print the program argument list and exit. --no-defaults Don't read default options from any option file. --defaults-file=# Only read default options from the given file #. --defaults-extra-file=# Read this file after the global files are read. -A, --all-databases Dump all the databases. This will be same as --databases ... default-auth (No default value) </code>
再去看别人怎么用的:
mysqldump 导出
mysqldump 导出 远程
MySQL mysqldump数据导出详解 – pursuer.chen – 博客园
-A, –all-databases Dump all the databases. This will be same as –databases
with all databases selected.
-B, –databases Dump several databases. Note the difference in usage; in
this case no tables are given. All name arguments are
regarded as database names. ‘USE db_name;’ will be
included in the output.
-C, –compress Use compression in server/client protocol.
-h, –host=name Connect to host.
-u, –user=name User for login if not current user.
-p, –password[=name]
Password to use when connecting to server. If password is
not given it’s solicited on the tty.
-P, –port=# Port number to use for connection.
对于此处要导出的mysql:
感觉命令是:
<code>mysqldump -C -h yyy.mysql.rds.aliyuncs.com -P PORT -B xxx -u root -p your_password > aliyun_rds_mysql_xxx.sql </code>
去找找
–default-character-set
–default-character-set=name
Set the default character set.
MySql导入导出数据库(含远程导入导出) – CSDN博客
再去搞清楚,如何压缩的,毕竟此处有5G多,希望能压缩变小点:
mysqldump 导出 压缩
“比如:mysqldump sms | gzip > sms.sql.gz如果不压缩,我直接导成的文件有3G,压缩了则只有100M,还是非常显著的。”
mysqldump导出压缩(gzip)文件 – Mysql教程 – PHP粉丝网
“mysqldump options> | gzip > outputfile.sql.gz”
印象中,7z压缩效率更高,尤其是对于文本(此处导出的mysql就是文本)
所以去看看此处服务器中是否有7z
没有:
所以参考之前自己的:
去安装7z,再去考虑用7z压缩
安装好了
看之前用法好像是:
<code>7za a -t7z -r -bt www.crifan.com_allFiles_20180320.7z /home/wwwroot/www.crifan.com/* </code>
此处,难道是:
<code>xxx > 7za a -t7z -r -bt aliyun_rds_xxx_mysql_dump.7z </code>
加起来估计就是:
<code>mysqldump -h xxx.mysql.rds.aliyuncs.com -P PORT -B databases_name -u root -p your_password > 7za a -t7z -r -bt aliyun_rds_xxx_mysql_dump.7z </code>
去试试
结果:
<code>[root@xxx-general-01 for_bacup_mysql]# mysqldump -h xxx.mysql.rds.aliyuncs.com -P port -B db_name -u root -p your_password > 7za a -t7z -r -bt aliyun_rds_xxx_mysql_dump.7z mysqldump: unknown option '-7' </code>
搞错了,改为:
<code>mysqldump -h xxx.mysql.rds.aliyuncs.com -P PORT -B db_name -u root -p your_password | 7za a -t7z -r -bt > aliyun_rds_xxxx_mysql_dump.7z </code>
结果:
<code>[root@xxx-general-01 for_bacup_mysql]# mysqldump -h xxx.mysql.rds.aliyuncs.com -P PORT -B dbname -u root -p your_password | 7za a -t7z -r -bt > aliyun_rds_xxx_mysql_dump.7z Command Line Error: Cannot find archive name Enter password: mysqldump: Got error: 1045: "Access denied for user 'root'@'x.x.x.197' (using password: YES)" when trying to connect </code>
先去把-u和-p中间不加空格:
<code>mysqldump -h xxx.mysql.rds.aliyuncs.com -P PORT -B db_name -uroot -pyour_password | 7za a -t7z -r -bt > aliyun_rds_xxx_mysql_dump.7z </code>
结果:
<code>[root@xxx-general-01 for_bacup_mysql]# mysqldump -h xxx.mysql.rds.aliyuncs.com -P port -B dbname -uroot -pyour_password | 7za a -t7z -r -bt > aliyun_rds_xxx_mysql_dump.7z Command Line Error: Cannot find archive name mysqldump: Got errno 32 on write </code>
算了,放弃7z,换成gzip吧:
<code>mysqldump -h xxx.mysql.rds.aliyuncs.com -P port -B dbname -uroot -pyour_password | gzip > aliyun_rds_xxx_mysql_dump.sql.gz </code>
结果不到一秒瞬间就完成了:
<code>[root@xxx-general-01 for_bacup_mysql]# mysqldump -h xxx.mysql.rds.aliyuncs.com -P port -B dbname -uroot -pyour_password | gzip > aliyun_rds_xxx_mysql_dump.sql.gz [root@xxx-general-01 for_bacup_mysql]# ll -lha total 100K drwxr-xr-x 2 root root 4.0K Apr 26 10:50 . drwxr-xr-x 4 root root 4.0K Apr 26 10:12 .. -rw-r--r-- 1 root root 90K Apr 26 10:50 aliyun_rds_xxx_mysql_dump.sql.gz [root@xxx-general-01 for_bacup_mysql]# </code>
感觉不对啊。
所以,把所有参数中间的空格都去掉
<code>mysqldump -hxxx.mysql.rds.aliyuncs.com -Pport -Bxxx -uroot -pyour_password | gzip > aliyun_rds_xxx_mysql_dump.sql.gz </code>
结果语法出错:
<code>[root@xxx-general-01 for_bacup_mysql]# mysqldump -hxxx.mysql.rds.aliyuncs.com -Pport -Bdbname -uroot -pyour_password | gzip > aliyun_rds_xxx_mysql_dump.sql.gz Usage: mysqldump [OPTIONS] database [tables] OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] OR mysqldump [OPTIONS] --all-databases [OPTIONS] For more options, use mysqldump --help </code>
再去:
<code>mysqldump --host=xxx.mysql.rds.aliyuncs.com --port=port--databases=dbname --user=root --password=your_password --default-character-set=utf8 | gzip > aliyun_rds_xxx_mysql_dump.sql.gz </code>
结果:
<code>[root@xxx-general-01 for_bacup_mysql]# mysqldump --host=xxx.mysql.rds.aliyuncs.com --port=port --databases=dbname --user=root --password=your_password --default-character-set=utf8 | gzip > aliyun_rds_xxx_mysql_dump.sql.gz Warning: mysqldump: ignoring option '--databases' due to invalid value 'dbname' Usage: mysqldump [OPTIONS] database [tables] OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] OR mysqldump [OPTIONS] --all-databases [OPTIONS] For more options, use mysqldump --help [root@xxx-general-01 for_bacup_mysql]# </code>
然后再去看看help参数解释
<code>mysqldump --host=xxx.mysql.rds.aliyuncs.com --port=port --user=root --password=your_password --default-character-set=utf8 dbname | gzip > aliyun_rds_xxx_mysql_dump.sql.gz </code>
结果:
还是瞬间(把整个mysql的数据库 5G多?)就备份好了?
<code>[root@xxx-general-01 for_bacup_mysql]# mysqldump --host=xxx.mysql.rds.aliyuncs.com --port=port --user=root --password=your_password --default-character-set=utf8 dbname | gzip > aliyun_rds_xxx_mysql_dump.sql.gz [root@xxx-general-01 for_bacup_mysql]# ll total 92 -rw-r--r-- 1 root root 91692 Apr 26 10:56 aliyun_rds_xxx_mysql_dump.sql.gz [root@xxx-general-01 for_bacup_mysql]# ll -lha total 100K drwxr-xr-x 2 root root 4.0K Apr 26 10:52 . drwxr-xr-x 4 root root 4.0K Apr 26 10:12 .. -rw-r--r-- 1 root root 90K Apr 26 10:56 aliyun_rds_xxx_mysql_dump.sql.gz </code>
压缩后的文件,只有90K?
还是自己理解错误,其实此处的xxxx的表中,本身数据就不多,所以压缩后90K就是正常的。
下载到本地看看效果
看起来是OK的?
至此,算是从服务器上备份下来需要的数据库的某个表的数据到本地了。
【总结】
此处去把,只能内网访问的阿里云的RDS的mysql数据库中某个表的数据,去备份到本地。
其实就是:
登录另外一台阿里云的ECS服务器,两者同属一个内网,本身有互相访问的权限。
然后去执行mysqldump去导出备份即可,且(以为很大,所以)用了gzip压缩以减小文件体积。
具体命令是:
<code>mysqldump --host=xxx.mysql.rds.aliyuncs.com --port=port --user=root — assword=your_password --default-character-set=utf8 dbanme | gzip > aliyun_rds_xxx_mysql_dump.sql.gz </code>
或-u的用户名和-p的密码的参数重去掉空格的写法:
<code>mysqldump -h xxx.mysql.rds.aliyuncs.com -P port -B dbname -uroot -pyour_password | gzip > aliyun_rds_xxx_mysql_dump.sql.gz </code>
都可以。
转载请注明:在路上 » 【已解决】把阿里云上只能内网访问的mysql数据库备份到Mac本地