场景:由于并发或者需求来临之时,需要添加RDS数据库只读实例的机器数量,通过手动添加较慢,现则编写一脚本可批量快速添加。
#!/usr/bin/env python #coding=utf-8 def create_rds(): #安装阿里云Python SDK,pip install aliyun-python-sdk-core,pip install aliyun-python-sdk-ecs from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkrds.request.v20140815.CreateReadOnlyDBInstanceRequest import CreateReadOnlyDBInstanceRequest access_key_id = 'xxxxxxxxxxxxxxxx' your_accesskey_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx' your_region_id = 'xxxxxxxxx' client = AcsClient(access_key_id, your_accesskey_secret, your_region_id) #需要阿里云ram授权获取access_key request = CreateReadOnlyDBInstanceRequest() request.set_accept_format('json') request.set_ZoneId("cn-shenzhen-e") request.set_DBInstanceId("rm-wz9k4pbbl5mhbXXX") request.set_DBInstanceClass("rds.mysql.s3.large") request.set_DBInstanceStorage(150) request.set_EngineVersion("5.7") request.set_PayType("Postpaid") request.set_InstanceNetworkType("VPC") request.set_VPCId("vpc-wz9gnt2uralXXXXXXXX") request.set_VSwitchId("vsw-wz94lk1d25xxxxxxx") #输入添加实例的规格 response = client.do_action_with_exception(request) return response #创建单台只读实例节点 #print(str(response,encoding='utf-8')) rds_num = input('请输入需要增加rds只读节点的数量(1-3台,只读实例最大数量为5,确认实际数量再输入):') for i in range(rds_num): a = create_rds() a = a.find('mysql.rds.aliyuncs.com') if a >= 0: print('已开始添加第{}台'.format(i+1)) else: print('第{}台添加失败'.format(i+1)) break #调用函数,添加需要的数量