#bin/bash dir='/root/' wget_stat=`which wget|awk -F/ '{print $4}'` mongodb_tar="mongodb-linux-x86_64-rhel62-3.0.6.tgz" mongodb_packet="mongodb-linux-x86_64-rhel62-3.0.6" mongodb_packet_stat=`ls $dir$mongodb_tar` user=`grep mongodb /etc/passwd|awk -F: '{print $1}'` dir2=`ls /root/.script -d` mongodb_size=`ls -l $dir/mongodb-linux-x86_64-rhel62-3.0.6.tgz|awk '{print $5}'` cat <<eof1> /etc/mongodb.conf ### Basic Defaults bind_ip = 0.0.0.0 port = 27017 fork = true pidfilepath = /usr/local/mongodb/mongodb.pid logpath = /usr/local/mongodb/logs/mongodb.log dbpath =/usr/local/mongodb/data/ journal = true auth = true nohttpinterface = true logappend = true eof1 cat <<eof2> $dir.script/restart_mongodb.sh mongodb_pid=`lsof -i:27017|grep mongod|awk '{print $2 }'` mongo_num=`lsof -i:27017|grep mongod|wc -l` if [[ $mongo_num == 0 ]];then /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf&&lsof -i:27017 else /bin/kill -9 $mongodb_pid && /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf&&lsof -i:27017 fi eof2 if [[ -d $dir2 ]]; then echo "has .script" else mkdir /root/.script fi if [[ $user != mongodb ]]; then useradd -s /sbin/nologin -M mongodb fi if [[ $wget_stat != wget ]]; then yum install -y wget fi ##install mongodb if [[ $mongodb_packet_stat != $dir$mongodb_tar ]]; then cd $dir && wget https://fastdl.mongodb.org/linux/$mongodb_tar tar -zxvf $mongodb_tar&&mv $mongodb_packet /usr/local/mongodb&&mkdir /usr/local/mongodb/data && mkdir /usr/local/mongodb/logs elif [[ $mongodb_size == 50380166 ]]; then rm -rf $dir$mongodb_packet tar -zxvf $mongodb_tar&&mv $mongodb_packet /usr/local/mongodb&&mkdir /usr/local/mongodb/data && mkdir /usr/local/mongodb/logs else rm -rf $dir/mongodb-linux-x86_64-rhel62-3.0.6.tgz cd $dir && wget https://fastdl.mongodb.org/linux/$mongodb_tar tar -zxvf $mongodb_tar&&mv $mongodb_packet /usr/local/mongodb&&mkdir /usr/local/mongodb/data && mkdir /usr/local/mongodb/logs fi ##start mongodb echo "/usr/local/mongodb/bin/mongod -f /etc/mongodb.conf" >>/etc/rc.local /usr/local/mongodb/bin/mongod -f /etc/mongodb.conf&&lsof -i:27017