4-7 Mongodb服务启动和停止
最后更新于:2022-04-02 07:39:59
### 4、Mongodb 启动和停止服务
Mongodb 启动:
1、mongod --fork --port 12345 --dbpath /srv/mongodb/
2、mongod -f /data/mongodb/conf/mongodb.conf
推荐第二种方式把配置写进文件启动。
Mongodb停止:
>1、kill -2 (mongod process ID)
2、kill (mongod process ID)
3、mongod --shutdown
4、db.shutdownServer()
切勿使用kill -9(即SIGKILL)终止mongod实例。
前两种方式就不介绍了:
3、 --shutdown
```
[root@10-1-1-159 ~]# sudo -u mongod /data/mongodb-4.2.2/bin/mongod -f /data/mongodb/conf/mongodb.conf --shutdown
killing process with pid: 28761
```
4、db.shutdownServer()
```
[root@10-1-1-159 ~]# sudo -u mongod /data/mongodb-4.2.2/bin/mongo 10.1.1.159:27010
rs02:SECONDARY> db.getMongo().setSlaveOk()
rs02:SECONDARY> use admin
switched to db admin
rs02:SECONDARY> db.shutdownServer()
2020-03-20T14:37:28.602+0800 E QUERY [js] Error: shutdownServer failed: {
"operationTime" : Timestamp(1584686240, 1),
"ok" : 0,
"errmsg" : "shutdown must run from localhost when running db without auth",
"code" : 13,
"codeName" : "Unauthorized",
"$clusterTime" : {
"clusterTime" : Timestamp(1584686240, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.shutdownServer@src/mongo/shell/db.js:426:19
@(shell):1:1
rs02:SECONDARY>
```
需要使用localhost连接mongo实例,在配置文件启动时候添加:
>bindIp: 10.1.1.159,localhost
```
[root@10-1-1-159 ~]# sudo -u mongod /data/mongodb-4.2.2/bin/mongo localhost:27020
> use admin
switched to db admin
> db.shutdownServer()
2020-03-20T14:47:49.186+0800 I NETWORK [js] DBClientConnection failed to receive message from localhost:27020 - HostUnreachable: Connection closed by peer
server should be down...
```
官网详细介绍地址:https://docs.mongodb.com/manual/tutorial/manage-mongodb-processes/
![](https://s4.51cto.com/images/blog/202004/23/cd8bca9a63d2a1af57ccb81fc79762c0.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
';