shell脚本----MongoDB4.0.21一键安装
shell脚本----MongoDB4.0.21一键安装及账户密码配置
开发突然说安装到某个服务器安装一个mongodb,账户xxx,密码xxx
恨不得30秒给他装好----嗯~
那就开干
#!/bin/bash #sunli<1916989848@qq.com> #可修改的端口、账户、密码 PORT="27017" User="admin" Passwd="admin@123" #下载二进制安装包 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-4.0.21.tgz #解压 tar -zxvf mongodb-linux-x86_64-rhel62-4.0.21.tgz #删除原来的tgz(可选) #rm -rf mongodb-linux-x86_64-rhel62-4.0.21.tgz #安装路径,数据/日志目录 mv mongodb-linux-x86_64-rhel62-4.0.21/ /usr/local/mongodb mkdir -p /usr/local/mongodb/{data,logs} touch /usr/local/mongodb/logs/mongo.log #配置环境变量 cat > /etc/profile.d/mongodb.sh <<- EOF export MONGODB_HOME=/usr/local/mongodb export PATH=$PATH:$MONGODB_HOME/bin EOF source /etc/profile.d/mongodb.sh #bash xxx.sh次方式执行脚本环境变量才能生效 #创建配置文件,默认27017端口 cat > /usr/local/mongodb/mongo.conf <<- EOF dbpath=/usr/local/mongodb/data logpath=/usr/local/mongodb/logs/mongo.log logappend=true journal=true quiet=true port=$PORT fork=true bind_ip=0.0.0.0 auth=true EOF #加入systemd启动 cat > /usr/lib/systemd/system/mongodb.service <<- EOF [Unit] Description=mongodb After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongo.conf ExecReload=/bin/kill -s HUP \$MAINPID ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/mongo.conf PrivateTmp=true Restart=always RestartSec=1 [Install] WantedBy=multi-user.target EOF #启动并设置开机启动 systemctl daemon-reload systemctl start mongodb.service systemctl enable mongodb.service systemctl status mongodb.service #添加mongodb账户密码 [ `echo $?` == 0 ] && /usr/local/mongodb/bin/mongo --port $PORT admin --eval "db.createUser({user:'$User',pwd:'$Passwd',roles:['userAdminAnyDatabase','root']});"