阿里云的源yum方式安装Mongodb

1)在/etc/yum.repos.d 创建一个mongodb-org.repo 源文件

[root@qd-vpc-dev-op01 ~]$ cd /etc/yum.repos.d/
[root@qd-vpc-dev-op01 yum.repos.d]$ touch mongodb-org.repo
[root@qd-vpc-dev-op01 yum.repos.d]$ cat mongodb-org.repo
[mongodb-org]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1
根据自己的系统版本选择填写的5  6  7版本的源
2)清空缓存和更新yum源
[root@qd-vpc-dev-op01 yum.repos.d]$ yum clean all
[root@qd-vpc-dev-op01 yum.repos.d]$ yum make cache
#更新yum源
[root@qd-vpc-dev-op01 yum.repos.d]$ yum update
3) 安装MongoDB
[root@qd-vpc-dev-op01 yum.repos.d]$ yum install -y mongodb-org
4)启动MongoDB
[root@qd-vpc-dev-op01 yum.repos.d]$ service mongod start
[root@qd-vpc-dev-op01 yum.repos.d]$ chkconfig mongod on
5)配置远程访问
[root@qd-vpc-dev-op01 yum.repos.d]$cat /etc/mongod.conf|grep -v "#"|grep -v "^$"
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
processManagement:
net:
  port: 27017
[root@qd-vpc-dev-op01 yum.repos.d]$ service mongod restart
Restarting mongod (via systemctl):                         [  OK  ]
6)打开MongoDB
[root@qd-vpc-dev-op01 yum.repos.d]$ mongo 127.0.0.1:27017
MongoDB shell version: 3.2.16
connecting to: 127.0.0.1:27017/test
Server has startup warnings:
2017-09-30T18:27:19.681+0800 I CONTROL  [initandlisten]
2017-09-30T18:27:19.681+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-09-30T18:27:19.681+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-09-30T18:27:19.681+0800 I CONTROL  [initandlisten]
2017-09-30T18:27:19.681+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-09-30T18:27:19.681+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-09-30T18:27:19.681+0800 I CONTROL  [initandlisten]
2017-09-30T18:27:19.681+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
2017-09-30T18:27:19.681+0800 I CONTROL  [initandlisten]

利用阿里云pythonSDK修改RSD白名单

因为公司没有购买公网IP,每次路由器重启后IP 就会变动,阿里云服务器上的RDS需要指定白名单才能访问!为了安全性也不能开放所有的IP,每次IP变更后就需要去修改白名单不厌其烦!
参考了GiveMeFive_Y的博客源码https://blog.csdn.net/GiveMeFive_Y/article/details/78687665
修改了一个适合自己的使用的版本
第一步安装SDK

#pip install aliyun-python-sdk-rds
#pip install aliyun-python-sdk-ecs

第二步修改参数

#!/usr/bin/env python
# coding=utf-8
import requests
import json
import os
import re
import sys
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import DescribeSecurityGroupAttributeRequest
from aliyunsdkecs.request.v20140526 import AuthorizeSecurityGroupRequest
from aliyunsdkecs.request.v20140526 import AuthorizeSecurityGroupEgressRequest
from aliyunsdkecs.request.v20140526 import RevokeSecurityGroupRequest
from aliyunsdkecs.request.v20140526 import RevokeSecurityGroupEgressRequest
from aliyunsdkecs.request.v20140526 import DescribeSecurityGroupsRequest
from aliyunsdkrds.request.v20140815 import ModifySecurityIpsRequest
AccessKey = “666666666” #此处填写阿里云的accesskey
AccessSecret = “7777777777” #此处填写阿里云的accesssecret
RegionId = “cn-shanghai” #此处填写RDS服务器所属地域
dbInstanceId = “rm-XXXXXX” #此处填写RDS服务器的实例ID
dbInstanceIPArrayName = “gongshi” #此处填写RDS数据库安全组的名称
dbInstanceIPArrayAttribute = “,” #此处填写RDS数据库安全组的IP的分割符
class Alis():
def __init__(self, AccessKey, AccessSecret, RegionId):
self.AccessKey = AccessKey
self.AccessSecret = AccessSecret
self.RegionId = RegionId
def client(self):
c = client.AcsClient(self.AccessKey, self.AccessSecret, self.RegionId)
return c
def describeSecurityGroupsRequest(self):
request = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest()
request.set_accept_format(‘json’)
return request
def modifySecurityIpsRequest(self, DBInstanceId, SecurityIps, DBInstanceIPArrayName, DBInstanceIPArrayAttribute):
“””修改数据库实例白名单
“””
request = ModifySecurityIpsRequest.ModifySecurityIpsRequest()
request.set_DBInstanceId(DBInstanceId)
request.set_SecurityIps(SecurityIps)
request.set_DBInstanceIPArrayName(DBInstanceIPArrayName)
request.set_DBInstanceIPArrayAttribute(DBInstanceIPArrayAttribute)
return request
def my_ip_method_1(self):
get_ip_method = os.popen(‘curl -s ip.cn’)
get_ip_responses = get_ip_method.readlines()[0]
get_ip_pattern = re.compile(r’\d+\.\d+\.\d+\.\d+’)
get_ip_value = get_ip_pattern.findall(get_ip_responses)[0]
print get_ip_value
return get_ip_value
def my_ip_method_2(self):
get_ip_method = os.popen(‘curl -s http://ip-api.com/json’)
get_ip_responses = get_ip_method.readlines()[0]
get_ip_responses = eval(str(get_ip_responses))
get_ip_value = get_ip_responses[‘query’]
return get_ip_value
def my_ip_method_3(self):
get_ip_method = requests.get(‘http://ifconfig.co/json’).content
get_ip_value = eval(get_ip_method)
get_ip_value = get_ip_value[‘ip’]
request = get_ip_value
return request
def read_old_ip(slef):
f = open(‘./ip.txt’)
line = f.read().splitlines()
print line[0]
f.close()
return line[0]
def write_ip_txt(slef,ip):
f = open(‘./ip.txt’,’w’)
print ip
f.write(ip)
f.close()
if __name__ == ‘__main__’:
ali = Alis(AccessKey, AccessSecret, RegionId) # 阿里云后台创建
clt = ali.client()
securityIps = ali.my_ip_method_1()#根据自己情况确定使用哪个公网IP查询接口
old_ip = ali.read_old_ip()
if old_ip == securityIps:
print(‘The specified value of parameter Value is the same as old’)
else:
req = ali.modifySecurityIpsRequest(dbInstanceId, securityIps,dbInstanceIPArrayName, dbInstanceIPArrayAttribute)
res = clt.do_action_with_exception(req)
print(“修改RDS白名单%s” % res)
ip=securityIps
ali.write_ip_txt(ip)
第三步在python的目录下建立一个ip.txt的空文件夹,用于存储记录之前的历史IP
 
 
 
 

利用阿里云接口动态设置DDNS

因为公司没有购买公网IP,每次路由器重启后IP 就会变动,但是公司有很多服务器需要外网的回调进行开发调试!本来是使用的花生壳的动态域名解析,但是花生壳不争气啊,免费的动态服务,动不动就挂掉,不厌其烦!
还好阿里云的域名提供了接口更新DNS映射,并且阿里云的DNS修改后生效非常的快!
本着重来不干重复造轮子的事情,通过收索找到了一个可以动态通过接口修改IP的python源码  ,github地址https://github.com/terencechuen/terence_proj/tree/master/aliyun_ddns
我的centos服务器使用的是python2的环境,使用的下面这个版本的python代码
将此代码下载到linux 服务器,然后编辑源代码
# -*- coding: UTF-8 -*-
import json
import os
import re
import sys
from datetime import datetime
import requests
from aliyunsdkalidns.request.v20150109 import UpdateDomainRecordRequest, DescribeDomainRecordsRequest, \
DescribeDomainRecordInfoRequest
from aliyunsdkcore import client
access_key_id = “”
access_Key_secret = “”
# 请填写你的账号ID
account_id = “”
# 如果填写yes,则运行程序后仅显示域名信息,并不会更新记录,用于获取解析记录ID。
# 如果填写no,则运行程序后不显示域名信息,仅更新记录。
i_dont_know_record_id = ‘no’
# 请填写你的一级域名
rc_domain = ‘baidu.com’
# 请填写你的解析记录
rc_rr = ‘vpn’
# 请填写你的记录类型,DDNS请填写A,表示A记录
rc_type = ‘A’
# 请填写解析记录ID
rc_record_id = ‘3825227609856000’
# 请填写解析有效生存时间TTL,单位:秒
rc_ttl = ‘600’
# 请填写返还内容格式,json,xml
rc_format = ‘json’
def my_ip_method_1():
get_ip_method = os.popen(‘curl -s ip.cn’)
get_ip_responses = get_ip_method.readlines()[0]
get_ip_pattern = re.compile(r’\d+\.\d+\.\d+\.\d+’)
get_ip_value = get_ip_pattern.findall(get_ip_responses)[0]
return get_ip_value
def my_ip_method_2():
get_ip_method = os.popen(‘curl -s http://ip-api.com/json’)
get_ip_responses = get_ip_method.readlines()[0]
get_ip_responses = eval(str(get_ip_responses))
get_ip_value = get_ip_responses[‘query’]
return get_ip_value
def my_ip_method_3():
get_ip_method = requests.get(‘http://ifconfig.co/json’).content
get_ip_value = eval(get_ip_method)
get_ip_value = get_ip_value[‘ip’]
return get_ip_value
def check_records(dns_domain):
clt = client.AcsClient(access_key_id, access_Key_secret, ‘cn-hangzhou’)
request = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
request.set_DomainName(dns_domain)
request.set_accept_format(rc_format)
result = clt.do_action_with_exception(request)
result = result.decode()
result_dict = json.JSONDecoder().decode(result)
result_list = result_dict[‘DomainRecords’][‘Record’]
for j in result_list:
print(‘Subdomain:’ + j[‘RR’].encode() + ‘ ‘ + ‘| RecordId:’ + j[‘RecordId’].encode())
return
def old_ip():
clt = client.AcsClient(access_key_id, access_Key_secret, ‘cn-hangzhou’)
request = DescribeDomainRecordInfoRequest.DescribeDomainRecordInfoRequest()
request.set_RecordId(rc_record_id)
request.set_accept_format(rc_format)
result = clt.do_action_with_exception(request).decode()
result = json.JSONDecoder().decode(result)
result = result[‘Value’]
return result
def update_dns(dns_rr, dns_type, dns_value, dns_record_id, dns_ttl, dns_format):
clt = client.AcsClient(access_key_id, access_Key_secret, ‘cn-hangzhou’)
request = UpdateDomainRecordRequest.UpdateDomainRecordRequest()
request.set_RR(dns_rr)
request.set_Type(dns_type)
request.set_Value(dns_value)
request.set_RecordId(dns_record_id)
request.set_TTL(dns_ttl)
request.set_accept_format(dns_format)
result = clt.do_action_with_exception(request)
return result
def write_to_file():
time_now = datetime.now().strftime(‘%Y-%m-%d %H:%M:%S’)
current_script_path = sys.path[1]
print current_script_path
log_file = ‘/root/ddns’ + ‘/’ + ‘aliyun_ddns_log.txt’
write = open(log_file, ‘a’)
write.write(time_now + ‘ ‘ + str(rc_value) + ‘\n’)
write.close()
return
if i_dont_know_record_id == ‘yes’:
check_records(rc_domain)
elif i_dont_know_record_id == ‘no’:
rc_value = my_ip_method_1()
print rc_value
rc_value_old = old_ip()
if rc_value_old == rc_value:
print(‘The specified value of parameter Value is the same as old’)
else:
print(update_dns(rc_rr, rc_type, rc_value, rc_record_id, rc_ttl, rc_format))
write_to_file()
 
执行改脚本
[root@localhost ddns]# python AliyunDDNS_NonSDK.py
[#180328-15:19] DNS Record was updated from [1797x1q502.imwork.net] to [171.217.104.245].