用shell 脚本实现 搭建MongoDB 单节点
下面是对MongoDB 单机的创建、启动、关闭、加密等等 功能的实现,用shell 脚本
MongoDB 的用户名:root_mongo 和密码:bZSV9J6De3_mongo,角色 root 数据库admin。用户名这密码在shell 里面自行修改。
修改用户名和密码用到的方法是 createAllPasswd()
如果是生产环境请对linux 调优,然后下载numactl 用一下命令启动
numactl --interleave=all bin/mongod -f /conf/mongodbsingle.conf
脚本的执行命令
第一步:初始化mongodb 目录及启动Mongodb和修改ip
ip=192.168.0.24 这里使用 自己的ip地址。
port=29050 端口号可以不修改。
./single create
第二步:添加keyfile 配置
./single passwd
第三步: 添加密码,这里的密码我写死了,可以自行更改
./single addpasswd
第四步:关闭MongoDB
./single stop
第五步: 开启MongoDB
./single start
如果执行有问题 请执行下面命令,然后 在执行 上面五步。
建议,这里还是要用手动执行。删除mongodbsingle 这个目录 就可以了
./single clean
下面脚本适用于测试环境。
#!/bin/bash
#mongodb的家目录 目录是任意目录,注意权限
path=/home/mongodb
mongodbpath=$path/mongodb/bin/
mongodbdatapath=$path/mongodbsingle/data
mongodblogpath=$path/mongodbsingle/log
mongodbpidpath=$path/mongodbsingle/pid
port=29050
ip=192.168.0.24
function startmongodb()
{
$mongodbpath/mongod -f $mongodbdatapath/conf/mongodbsingle.conf
}
function createpath(){
mkdir -p $mongodbdatapath
mkdir -p $mongodblogpath
mkdir -p $mongodbpidpath
mkdir -p $mongodbdatapath/conf
}
function createconfig(){
mkdir -p $mongodbdatapath/journal
mkdir -p $mongodblogpath/journal
touch $mongodblogpath/mongodbsingle.log
touch $mongodbpidpath/mongodbsingle.pid
touch $mongodbdatapath/conf/mongodbsingle.conf
ln -s $mongodblogpath/journal $mongodbdatapath/journal
echo“目录创建成功$mongodbdatapath $mongodblogpath”
cat >> $mongodbdatapath/conf/mongodbsingle.conf << EOF
systemLog:
destination: file
path: $mongodblogpath/mongodbsingle.log
storage:
dbPath: $mongodbdatapath
processManagement:
fork: true
pidFilePath: $mongodbpidpath/mongodbsingle.pid
net:
bindIp: $1
port: $2
EOF
}
function stopmongodb(){
$mongodbpath/mongod --shutdown --dbpath $mongodbdatapath
}
function cleanmongodb(){
rm -rf $path/mongodbsingle
}
function passwdcreate(){
mkdir $path/mongodbsingle/key
openssl rand -base64 756 > $path/mongodbsingle/key/key.file
chmod 400 $path/mongodbsingle/key/key.file
echo "往配置文件里面追加安全认证内容"
cat >> $mongodbdatapath/conf/mongodbsingle.conf << EOF
security:
keyFile: $path/mongodbsingle/key/key.file
authorization: enabled
EOF
}
function createAllPasswd(){
# 加密码
$$mongodbpath/mongo $ip:$port/admin << EOF
# 用户名和密码可以自行修改 user 字段代表用户名,pwd字段代码是密码
db.createUser({user:'root_mongo',pwd:'bZSV9J6De3_mongo',roles:[{role:'root',db:'admin'}]});
EOF
#验证
$$mongodbpath/mongo $ip:$port/admin << EOF
db.auth("root_mongos","bZSV9J6De3");
EOF
}
if [ "$1" == "start" ]
then
echo "********************开始启动mongodb***********************************"
startmongodb
echo "***********************请放心使用*************************************"
fi
if [ "$1" == "create" ]
then
echo "**********************开始创建mongodb************************************"
createpath
createconfig $ip $port
startmongodb
echo "************************mongodb创建mongodb,请放心使用*******************"
fi
if [ "$1" == "stop" ]
then
echo "*******************************正在关闭mongodb*****************************"
stopmongodb
echo "***************************mongodb关闭成功,请您辛苦了*********************"
fi
if [ "$1" == "clean" ]
then
echo "******************删除mongodb目录*****************************************"
cleanmongodb
echo "**************************mongodb单机版,清理干净*************************"
fi
if [ "$1" == "passwd" ]
then
echo "开始创建密码"
passwdcreate
echo "密码创建完成"
fi
if [ "$1" == "addpasswd" ]
then
echo "开始创建密码"
createAllPasswd
echo "密码创建完成"
fi
echo "Usage: $0 [start|create|stop|clean|passwd]"
如有问题可以给我留言。