centos安装MongoDB,并设置开机自启动
1:安装
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz
mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb
vim /etc/profile(添加如下图环境变量)
source /etc/profile(使环境变量生效)
- 1
- 2
- 3
- 4
- 5
2:mongodb启动
cd /usr/local/momgodb/bin ./mongod
- 1
mongodb客户端连接
cd /usr/local/momgodb/bin ./mongo
- 1
3:以守护进程方式启动
cd /usr/local/mongodb touch log
vi mongodb.conf(创建配置文件)
- 1
- 2
chmod –R a+x ./mongodb.conf
kill掉当前MongoDB进程(以防端口被占用,出现以下错误)
- 1
- 2
cd /us r/local/mongodb/bin/
./mongod –f mongodb.conf(如果不是同一目录,输入刚刚创建的配置文件的绝对位置)
出现以下结果 表示配置文件启动成功
- 1
- 2
- 3
cd /etc/init.d
vi mongodb(创建启动文件,内容如下,确保下面启动和关闭的命令可以正常执行)
- 1
- 2
#!/bin/sh
#
#chkconfig: 2345 80 90
#description: mongodb
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
start() {
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf
}
stop() {
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/bin/mongodb.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
- 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
chmod –R a+x mongodb
chkconfig --add mongodb
chkconfig mongodb on
mongodb start(启动之前确保已经kill 掉mongodb,出现如下,表明启动成功)
- 1
- 2
- 3
- 4