forked from daily-scripts/shell-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_mongodb.sh
197 lines (178 loc) · 4.62 KB
/
install_mongodb.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
#mail:[email protected]
#function:auto install mongodb
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
[ $(rpm -q centos-release|cut -d- -f3) != "6" ] && echo "Please use centos6.x" && exit 1
clear
echo "##########################################"
echo "# Auto Install mongodb for centos6.x ##"
echo "# Press Ctrl + C to cancel ##"
echo "# Any key to continue ##"
echo "##########################################"
echo "(1) Install Mongodb-3.2"
echo "(2) Install Mongodb-3.4"
echo "(3) Install Mongodb-3.6"
echo "(4) EXIT"
read -p "Please input your choice:" NUM
case $NUM in
1)
mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.20.tgz"
software_version="mongodb-3.2"
;;
2)
mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.10.tgz"
software_version="mongodb-3.4"
;;
3)
mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.6.5.tgz"
software_version="mongodb-3.6"
;;
4)
echo -e "\033[41;37m You choice channel! \033[0m" && exit 0
;;
*)
echo -e "\033[41;37m Input Error! Place input{1|2|3|4} \033[0m" && exit 1
;;
esac
clear
softdir="/software"
installdir="/usr/local"
sys_init() {
clear
echo -e "\033[42;5m initialization system... \033[0m"
sleep 2
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
/etc/init.d/iptables status >/dev/null
[ $? -eq 0 ] && iptables -I INPUT -p tcp --dport 27017 -j ACCEPT
[ $? -eq 0 ] && /etc/init.d/iptables save
yum -y install wget >/dev/null
setenforce 0
}
download_software() {
clear
echo -e "\033[42;5m download software... \033[0m"
sleep 2
if [ ! -d ${softdir} ];then
mkdir ${softdir} && cd ${softdir}
else
cd ${softdir}
fi
for software_url in ${mongodb_url}
do
wget -c ${software_url}
if [ $? -eq 0 ];then
for software in `ls`
do
tar zxf $software -C $installdir
done
fi
done
}
install_software() {
clear
echo -e "\033[42;5m install server... \033[0m"
sleep 2
mongodbdir=$(ls ${installdir}|grep "mongodb-linux-x86_64")
ln -s ${installdir}/${mongodbdir} ${installdir}/mongodb
mkdir ${installdir}/mongodb/{conf,mongoData,mongoLog}
touch ${installdir}/mongodb/mongoLog/mongodb.log
echo "export PATH=\$PATH:${installdir}/mongodb/bin">/etc/profile.d/mongodb.sh
source /etc/profile.d/mongodb.sh
cat >${installdir}/mongodb/conf/mongodb.conf <<EOF
dbpath=${installdir}/mongodb/mongoData
logpath=${installdir}/mongodb/mongoLog/mongodb.log
logappend=true
journal=true
quiet=true
port=27017
pidfilepath=/var/run/mongod.pid
#replSet =RS
maxConns=20000
#httpinterface=true
fork=true
#auth=true
EOF
}
start_server() {
clear
echo -e "\033[42;5m configuration server... \033[0m"
cat >/etc/init.d/mongodb-server<<EOF
#!/bin/bash
#auth:kaliarch
# mongodb Startup script for mongodb processes
#
# chkconfig: - 90 10
# description: Mongodb provides fast memory based storage.
# processname: Mongodb
. /etc/rc.d/init.d/functions
bash_dir="/usr/local/mongodb"
mongod="\${bash_dir}/bin/mongod"
config="\${bash_dir}/conf/mongodb.conf"
getpid=\$(pidof mongod)
lockfile="\${bash_dir}/mongodb.lock"
pidfile="/var/run/mongod.pid"
#user=nobody
start() {
action $"Starting \$prog: " /bin/true
# Starting mongodb on port 27017 as deamon and user nobody
\$mongod -f \${config} >/dev/null
RETVAL=$?
[ \$RETVAL = 0 ] && touch \${lockfile}
return \$RETVAL
}
stop() {
if test "x\${getpid}" != x; then
action $"Stopping \$prog " /bin/true
killall mongod
fi
RETVAL=\$?
[ \$RETVAL = 0 ] && rm -rf \${lockfile} \${pidfile}
return \$RETVAL
}
case "\$1" in
start)
start
;;
stop)
stop
;;
status)
status -p \${pidfile} \${mongod}
RETVAL=\$?
;;
restart)
stop
start
;;
*)
echo $"Usage: \$0 {start|status|stop|restart}"
exit 1
esac
exit \${RETVAL}
EOF
cd /
chmod +x /etc/init.d/mongodb-server
chkconfig mongodb-server on
service mongodb-server start
}
check_server() {
clear
echo -e "\033[42;5m check server status... \033[0m"
server_port=$(netstat -lntup|grep mongod|wc -l)
server_proc=$(ps -ef |grep mongodb.conf|grep -v grep|wc -l)
if [ ${server_port} -gt 0 -a ${server_port} -gt 0 ];then
echo -e "\033[42;37m mongodb-server install successful! \033[0m"
echo -e "\033[42;37m version:${software_version} \033[0m"
echo -e "\033[42;37m bashpath:${installdir}/mongodb \033[0m"
else
echo -e "\033[42;37m mongodb install error! \033[0m"
fi
}
main() {
sys_init
download_software
install_software
start_server
check_server
}
main