MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。它以高性能、易部署、易使用,存储数据非常方便等优点被广泛使用。其安装配置相当简单,有如轻量级的mysql,但功能丝毫不差。本文主要描述mongoDB的启动与停止。
一、准备环境
1、当前环境
# more /etc/redhat-release
CentOS release 6.7 (Final)
2、安装mongod
# which mongod
/var/lib/mongodb/bin/mongod
参考:
Linux下快速安装MongoDB
Windows平台下安装MongoDB
3、创建数据目录
# mkdir -pv /data/mongodata/{rs1,rs2,rs3}
# mkdir -pv /var/lib/mongodb/conf
二、启动mongodb
1、基于命令行方式启动mongodb
# mongod --dbpath=/data/mongodata/rs1 --logpath=/data/mongodata/rs1/rs1.log &
缺省端口为
[root@node3 rs1]# netstat -nltp|grep mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 5062/mongod
2、基于配置文件的命令行启动
vi /var/lib/mongodb/conf/rs2.conf
port = 27000
dbpath = /data/mongodata/rs2
logpath = /data/mongodata/rs2/rs2.log
smallfiles = true
fork = true
pidfilepath = /var/run/mongo.pid
# mongod --config /var/lib/mongodb/conf/rs2.conf &
### Author : Leshami
### Blog : http://blog.csdn.net/leshami
# netstat -nltp|grep 27000
tcp 0 0 0.0.0.0:27000 0.0.0.0:* LISTEN 5356/mongod
3、以守护进程方式启动mongodb
# mongod --dbpath=/data/mongodata/rs3 --logpath=/data/mongodata/rs1/rs3.log --fork --port 28000
# netstat -nltp|grep mongod
tcp 0 0 0.0.0.0:28000 0.0.0.0:* LISTEN 5465/mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 5435/mongod
tcp 0 0 0.0.0.0:27000 0.0.0.0:* LISTEN 5448/mongod
4、使用系统服务的方式启动mogodb
启动脚本
# vi /etc/init.d/mongod
#!/bin/sh
# chkconfig: 2345 93 18
#MogoDB home directory
MONGODB_HOME=/var/lib/mongodb
#mongodb command
MONGODB_BIN=$MONGODB_HOME/bin/mongod
#mongodb config file
MONGODB_CONF=$MONGODB_HOME/conf/mongodb.conf
#mongodb PID
MONGODB_PID=/var/run/mongo.pid
#set open file limit
SYSTEM_MAXFD=65535
MONGODB_NAME="mongodb"
. /etc/rc.d/init.d/functions
if [ ! -f $MONGODB_BIN ]
then
echo "$MONGODB_NAME startup: $MONGODB_BIN not exists! "
exit
fi
start(){
ulimit -HSn $SYSTEM_MAXFD
$MONGODB_BIN --config="$MONGODB_CONF" --fork ##added @20160901
ret=$?
if [ $ret -eq 0 ]; then
action $"Starting $MONGODB_NAME: " /bin/true
else
action $"Starting $MONGODB_NAME: " /bin/false
fi
}
stop(){
PID=$(ps aux |grep "$MONGODB_NAME" |grep "$MONGODB_CONF" |grep -v grep |wc -l)
if [[ $PID -eq 0 ]];then
action $"Stopping $MONGODB_NAME: " /bin/false
exit
fi
kill -HUP `cat $MONGODB_PID`
ret=$?
if [ $ret -eq 0 ]; then
action $"Stopping $MONGODB_NAME: " /bin/true
rm -f $MONGODB_PID
else
action $"Stopping $MONGODB_NAME: " /bin/false
fi
}
restart() {
stop
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
esac
# chmod u+x /etc/init.d/mongod
# service mongod start
about to fork child process, waiting until server is ready for connections.
forked process: 5543
child process started successfully, parent exiting
Starting mongodb: [ OK ]
三、停止mongoDB
1、向mongod进程发送信号
###SIGINT信号
# ps -ef|grep mongod|grep rs1
root 5435 4914 1 19:13 pts/2 00:00:14 mongod --dbpath=/data/mongodata/rs1 --logpath=/data/mongodata/rs1/rs1.log
# kill -2 5435
2016-08-30T17:02:00.528+0800 I CONTROL[signalProcessingThread] got signal 2(Interrupt), will terminate after current cmd ends
2016-08-30T17:02:00.530+0800 I REPL [signalProcessingThread] Stopping replication applier threads
2016-08-30T17:02:00.554+0800 I STORAGE [conn1253] got request after shutdown()
2016-08-30T17:02:00.774+0800 I CONTROL [signalProcessingThread] now exiting
2016-08-30T17:02:00.774+0800 I NETWORK [signalProcessingThread] shutdown: going to close listening sockets...
2016-08-30T17:02:00.774+0800 I NETWORK [signalProcessingThread] closing listening socket: 6
2016-08-30T17:02:00.775+0800 I NETWORK [signalProcessingThread] closing listening socket: 7
2016-08-30T17:02:00.775+0800 I NETWORK [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2016-08-30T17:02:00.775+0800 I NETWORK [signalProcessingThread] shutdown: going to flush diaglog...
2016-08-30T17:02:00.775+0800 I NETWORK [signalProcessingThread] shutdown: going to close sockets...
2016-08-30T17:02:00.775+0800 I STORAGE [signalProcessingThread] shutdown: waiting for fs preallocator...
2016-08-30T17:02:00.775+0800 I STORAGE [signalProcessingThread] shutdown: final commit...
2016-08-30T17:02:00.775+0800 I JOURNAL [signalProcessingThread] journalCleanup...
2016-08-30T17:02:00.775+0800 I JOURNAL [signalProcessingThread] removeJournalFiles
2016-08-30T17:02:00.777+0800 I NETWORK [conn1254] end connection 192.168.1.247:58349 (0 connections now open)
2016-08-30T17:02:00.779+0800 I JOURNAL [signalProcessingThread] Terminating durability thread ...
2016-08-30T17:02:00.881+0800 I JOURNAL [journal writer] Journal writer thread stopped
2016-08-30T17:02:00.882+0800 I JOURNAL [durability] Durability thread stopped
2016-08-30T17:02:00.882+0800 I STORAGE [signalProcessingThread] shutdown: closing all files...
2016-08-30T17:02:00.884+0800 I STORAGE [signalProcessingThread] closeAllFiles() finished
2016-08-30T17:02:00.884+0800 I STORAGE [signalProcessingThread] shutdown: removing fs lock...
2016-08-30T17:02:00.885+0800 I CONTROL [signalProcessingThread] dbexit: rc: 0
###SIGTERM信号
# ps -ef|grep mongod|grep rs3
# ps -ef|grep mongod|grep rs3
root 5465 1 1 19:14 ? 00:00:13 mongod --dbpath=/data/mongodata/rs3 --logpath=/data/mongodata/rs1/rs3.log --fork --port 28000
# kill -4 5465
信号 产生方式
sigint 通过ctrl+c将会对当进程发送此信号
sigterm kill命令不加参数就是发送这个信号
对进程的影响
sigint 信号被当前进程树接收到,也就是说,不仅当前进程会收到信号,它的子进程也会收到
sigterm只有当前进程收到信号,子进程不会收到。如果当前进程被kill了,那么它的子进程的父进程将会是init,也就是pid为1的进程
上述信号在发出后
不再接受新的连接请求
等待现有的连接处理完毕
关闭所有打开的连接
将内存的数据写出到磁盘
安全停止
2、使用系统服务脚本方式停止mongod
# ps -ef|grep mongod
root 5675 1 3 19:33 ? 00:00:00 /var/lib/mongodb/bin/mongod --config=/var/lib/mongodb/conf/rs2.conf
root 5689 4950 0 19:33 pts/3 00:00:00 grep mongod
[root@node3 conf]#
[root@node3 conf]# service mongod stop
Stopping mongodb: [ OK ]
3、db.shutdownServer()方式
# mongo localhost:27000
> use admin
> db.shutdownServer()
4、使用命令行方式关闭(补充@20160901)
# mongod -f /etc/mongo-m.conf --shutdown
5、强制关闭mongod
# kill -9 5675
缺点:
数据库直接关闭
数据丢失
数据文件容易损坏(需要进行修复)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。