Install MongoDB on Centos 7
阿新 • • 發佈:2019-03-22
limits num cas tina entos awk ren hugepage miss
#!/bin/bash
#######################
#mongodb簡介
#mongodb是個非關系型數據庫,但操作跟關系型數據最類似。mysql是關系型數據庫
#mongodb是面向文檔存儲的非關系型數據庫,數據以json的格式進行存儲
#mongodb可用來永久存儲,也可用來緩存數據
#mongodb提供副本集和分片集群功能,操作簡單
#############################
if [ `whoami` != root ]
then
echo "Please login as root to continue :)"
exit 1
fi
if [ ! -d /home/tools/ ];then
mkdir -p /home/tools
else
rm -rf /home/tools && mkdir -p /home/tools
fi
#Prohibit memory giant pages
echo ‘never‘ >/sys/kernel/mm/transparent_hugepage/enabled
echo ‘never‘ >/sys/kernel/mm/transparent_hugepage/defrag
#Add commands to /etc/rc.local
chmod +x /etc/rc.d/rc.local
echo "echo ‘never‘ >/sys/kernel/mm/transparent_hugepage/enabled" >>/etc/rc.local
echo "echo ‘never‘ >/sys/kernel/mm/transparent_hugepage/defrag" >>/etc/rc.local
#Disable firewall and selinux
sed -i ‘/SELINUX/s/enforcing/disabled/‘ /etc/selinux/config
systemctl disable firewalld.service
#Setting Handles Number and Process
cat >> /etc/security/limits.conf << EOF
* soft nofile 204800
* hard nofile 204800
* soft nproc 204800
* hard nproc 204800
EOF
sed -i ‘s/4096/204800/g‘ /etc/security/limits.d/20-nproc.conf
#download mongodb on centos 7
cd /home/tools && wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.6.tgz
#install mongodb
tar zxvf mongodb-linux-x86_64-rhel70-4.0.6.tgz
mv mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb-linux-x86_64-rhel70-4.0.6
ln -s /usr/local/mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb
#Create data directory
mkdir -p /data/mongodb/27017/
cat > /data/mongodb/27017/mongodb.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /data/mongodb/27017/mongodb.log
storage:
dbPath: /data/mongodb/27017/
journal:
enabled: true
processManagement:
fork: true
net:
port: 27017
bindIp: 0.0.0.0
EOF
#Add mongodb users and setting permission
useradd mongodb && chown -R mongodb.mongodb /data/mongodb/ /usr/local/mongodb/
#Add autoStart script
cat >/etc/init.d/mongodb <<EOF
#!/bin/bash
# Description:mongodb ORS SERVER
# chkconfig: - 85 15
# Written by jerry
MONGODB_EXEC="/usr/local/mongodb/bin/mongod"
MONGODB_DATA="/data/mongodb/27017/"
MONGODB_CONF="/data/mongodb/27017/mongodb.conf"
PORT=\$(netstat -tunlp|grep 27017|awk ‘{print \$4}‘|cut -d ‘:‘ -f2)
MONGODB_USER=mongodb
case \$1 in
start)
echo -n "Starting mongodb..."
if [[ \$PORT = 27017 ]];then
echo "mongodb is alreday running!"
else
/bin/su - \$MONGODB_USER -s /bin/bash -c "\$MONGODB_EXEC -f \$MONGODB_CONF"
fi
echo " done"
;;
stop)
echo -n "Stoping mongodb..."
/bin/su - \$MONGODB_USER -s /bin/bash -c "\$MONGODB_EXEC --shutdown --dbpath \$MONGODB_DATA"
echo " done"
;;
restart)
\$0 stop
\$0 start
;;
status)
if [[ \$PORT != 27017 ]];then
echo "mongodb is not running!"
else
echo "mongodb is running!"
fi
;;
*)
echo "Usage: \$0"
exit 1
esac
EOF
#Setting environment variables
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
#Add permission to /etc/init.d/mongodb
chmod +x /etc/init.d/mongodb
#Add to chkconfig service
chkconfig --add mongodb
#Setting up MongoDB auto-start
chkconfig mongodb on
#Start MongoDB
service mongodb start
Install MongoDB on Centos 7