企业上云最大的难点在于业务数据库迁移,很多企业在上云前,数据库域名或IP往往是已被写入应用客户端代码中,当从本地构建的数据库迁移到RDS时,需要用户修改客户端代码进行应用改造,此过程复杂,且风险高。
解决方案
通过内网DNS解析创建一个与之前相同的数据库域名,然后通过CNAME记录指向RDS分配的系统内网域名,这样在数据库上云过程中,企业用户无需修改客户端代码,能够减少应用改造,降低迁移风险。
准备工作
-
开通服务。
-
准备如下资源。
资源
数量
示例值
线下数据库连接域名
1
db.example.com
Ecs服务器
1&more
1.1.XX.XX、2.2.XX.XX
RDS数据库实例
1
rm-2ze44q293vs56l9po*****.mysql.rds.aliyuncs.com
操作步骤
1、获取RDS内网地址
-
前往RDS管理控制台,单击左侧导航栏的 实例列表,在实例列表页面单击 实例ID/名称 下的实例ID。
-
左侧导航栏选择 数据库连接,然后复制 内网地址 后方的域名。
2、添加内网域名解析域名及解析记录
-
选择 内网域名解析 页面下的 用户域名 页签。
-
单击 添加域名 (Zone) 按钮,在 添加内网域名解析域名(Zone) 对话框中输入域名,本示例输入 db.example.com,域名类型根据您的实际需求选择,推荐选择 内网域名解析加速区, 然后单击 确定。
-
在 用户域名 页面,单击目标域名
db.example.com操作 列的 解析记录 按钮。 -
在 解析记录 页面单击 添加记录,记录类型:CNAME,主机记录:@,解析请求来源:默认,记录值:
rm-2ze44q293vs56l9po*****.mysql.rds.aliyuncs.com(此域名为测试域名,您根据实际情况将数据库的内网地址粘贴进去),完成上述参数填写后单击 确定。 -
返回 用户域名 页面,单击目标域名
db.example.com操作 列的 生效范围设置,在 域名设置 页面设置域名生效范围,需要关联数据库所在VPC。
3、 效果测试
-
登录域名生效VPC网络内的ECS服务器,进行解析生效测试,返回如下结果即为成功。若返回结果异常,请排查解析记录是否正确配置。
dig db.example.com; <<>> DiG 9.11.4-P2-RedHat-9.11.4-16.P2.el7_8.6 <<>> db.example.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44628 ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;db.example.com. IN A ;; ANSWER SECTION: db.example.com. 60 IN CNAME rm-2ze44q293vs5619po125010.mysql.rds.aliyuncs.com. rm-2ze44q293vs5619po125010.mysql.rds.aliyuncs.com. 60 IN A 172.17.154.201 ;; Query time: 5 msec ;; SERVER: 100.100.2.136#53(100.100.2.136) ;; WHEN: Mon Jul 06 16:20:42 CST 2020 ;; MSG SIZE rcvd: 122 -
RDS连接测试:登录域名生效VPC网络内的ECS服务器,进行远程命令测试,返回如下结果即为成功。
mysql -utest_dns -ptest_dns -hdb.example.com -P3306 -Dtest_dns[root@xxx ~]# mysql -utest_dns -ptest_dns -hdb.example.com -P3306 -Dtest_dns mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 94 Server version: 8.0.18 Source distribution Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test_dns | +--------------------+ 3 rows in set (0.00 sec) mysql>
常见错误
-
ERROR 2003 (HY000): Can’t connect to MySQL server on ‘db.example.com’(110)请检查数据库白名单设置,将ECS的IP加入白名单中。
[root@xxx ~]# mysql -utest_dns -hdb.example.com -P3306 -Dtest_dns test_dns ERROR 2003 (HY000): Can’t connect to MySQL server on ‘db.example.com’ (110)