#!/usr/bin/env python3
#-*- coding = utf-8 -*-
import sys
import os
import platform
from time import sleep
#检测当前是什么操作系统
def checksystem():
system_type=platform.uname().version.lower()
if 'ubuntu' in system_type:
release = 'ubuntu'
elif 'centos' in system_type:
release = 'centos'
return release
#检测curl
def checkUrl():
release=checksystem()
if os.system('''ls /usr/bin/curl | grep -Eqi curl''') == 0:
pass
else:
print('\33[32;正在安装 Curl\33[0m')
if release == 'centos':
os.system('yum update > /dev/null 2>&1')
os.system('yum -y install curl > /dev/null 2>&1')
else:
os.system('apt-get update > /dev/null 2>&1')
os.system('apt-get -y install curl > /dev/null 2>&1')
#检测wegt
def checkWget():
release=checksystem()
if os.system('''ls /usr/bin/wget | grep -Eqi wget''') == 0:
pass
else:
print('\33[32;正在安装 Wget\33[0m')
if release == 'centos':
os.system('yum update > /dev/null 2>&1')
os.system('yum -y install wget > /dev/null 2>&1')
else:
os.system('apt-get update > /dev/null 2>&1')
os.system('apt-get -y install wget > /dev/null 2>&1')
#创建mongod的主配置文件
def ViMongod():
# 创建存放目录
os.system('mkdir -p /data/mongod')
os.chdir('/data/mongod')
os.system('mkdir -p conf log pid mongod25117 mongod25118 mongod25119')
with open('/data/mongod/conf/mongod25117.conf', 'w', encoding='utf-8') as mog:
mog.write('''#副本集配置文件
systemLog:
verbosity: 0
traceAllExceptions: true
destination: file
path: /data/mongod/log/mongod25117.log #日志路径 自建
logAppend: true
logRotate: rename
timeStampFormat: ctime
processManagement:
fork: true
pidFilePath: /data/mongod/pid/mongod25117.pid #pid文件路径 如无自建
net:
port: 25117 #端口号自定义
bindIp: 0.0.0.0
security:
keyFile: /data/mongod/conf/keyFile.pem #认证文件 自定义生成 openssl rand -base64 512 > /data/mongod/conf/keyFile.pem chmod 600 /data/mongod/keyFile
clusterAuthMode: keyFile
authorization: enabled
transitionToAuth: false
javascriptEnabled: true
storage:
dbPath: /data/mongod/mongod25117 #数据保存路径 文件夹自建
journal:
enabled: true
directoryPerDB: true
syncPeriodSecs: 60
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 48 #数据库调用内存(单独做数据库时可以设置为机器内存80%)
journalCompressor: zlib
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
replication:
replSetName: testDB #副本集名字''')
os.chdir('/data/mongod/conf')
os.system('openssl rand -base64 512 > /data/mongod/conf/keyFile.pem')
os.system('chmod 600 /data/mongod/conf/keyFile.pem')
def centos_yum_mongod():
with open('/etc/yum.repos.d/mongodb-org-4.2.repo', 'w', encoding='utf-8') as yum_repo:
yum_repo.write('''[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc''')
os.system(
'''yum -y install mongodb-org-4.2.12 mongodb-org-server-4.2.12 mongodb-org-shell-4.2.12 mongodb-org-mongos-4.2.12 mongodb-org-tools-4.2.12''')
# 固定版本
with open('/etc/yum.conf', 'a', encoding='utf-8') as etc_yum:
etc_yum.write(
'''exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools''')
def creat_Mongod():
os.chdir('/opt')
release = checksystem()
print('\33[32;1m开始安装Mongod\33[0m')
if release == 'centos':
#调用函数添加yum源
centos_yum_mongod()
sleep(2)
#编写配置文件
ViMongod()
pass
else:
os.system('wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -')
os.system('echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list')
os.system('sudo apt-get update')
os.system('sudo apt-get install -y mongodb-org=4.2.9 mongodb-org-server=4.2.9 mongodb-org-shell=4.2.9 mongodb-org-mongos=4.2.9 mongodb-org-tools=4.2.9')
os.system('''echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections''')
#调用创建配置文件
ViMongod()
#拷贝配置文件,并启动
def start_CP():
os.chdir('/data/mongod/conf')
# 拷贝文件
os.system('cp mongod25117.conf mongod25118.conf')
sleep(2)
os.system('cp mongod25117.conf mongod25119.conf')
sleep(2)
# 修改配置文件
os.system('''sed -i '/25117/s/25117/25118/' mongod25118.conf''')
os.system('''sed -i '/25117/s/25117/25119/' mongod25119.conf''')
# 启动
os.system('mongod -f mongod25117.conf')
sleep(3)
os.system('mongod -f mongod25118.conf')
sleep(3)
os.system('mongod -f mongod25119.conf')
sleep(3)
#创建集群
def create_repalist(IP1,IP2,IP3):
os.system("""/usr/bin/mongo --port 25117 <<EOF
use admin
cfg={_id:"testDB", members:[ {_id:0,host:'%s:25117',priority:2},
{_id:1,host:'%s:25118',priority:1},
{_id:2,host:'%s:25119',priority:1}]};
rs.initiate(cfg)
rs.status()
exit;
EOF""" % (IP1,IP2,IP3))
#创建管理员用户
def create_Admin_User():
txt='''
建议手动登陆mongo 进行创建用户设置s密码
db.createUser({ user:"admin",pwd:"xxxxxx",roles:[{ role:"root", db:"admin"},{ role :"userAdmin", db:"admin"}]})
db.auth('admin','xxxxxx')
'''
return txt
#卸载mongodb
def dele_mongo():
release=checksystem()
if release == 'centos':
os.system('''rpm -qa | grep mongodb-org | xargs rpm -e''')
os.system('''sudo rm -r /var/log/mongodb''')
os.system('''sudo rm -r /var/lib/mongo''')
os.system('''sudo rm -rf /data/mongod/''')
else:
os.system('''sudo apt-get purge mongodb-org*''')
os.system('''sudo rm -r /var/log/mongodb''')
os.system('''sudo rm -r /var/lib/mongodb''')
os.system('''sudo rm -rf /data/mongod/''')
if __name__=='__main__':
try:
if sys.argv[1] == 'create':
checkUrl()
checkWget()
creat_Mongod()
start_CP()
IP1=IP2=IP3='192.168.88.10'
create_repalist(IP1,IP2,IP3)
print(create_Admin_User())
elif sys.argv[1] == 'remove':
dele_mongo()
else:
print('\33[31;1m 请输入 create | remove !! \33[0m')
except Exception as e:
print('\33[31;1m Error:{} \33[0m'.format(e))
执行
python3 main.py create