MongoDB数据库的两种正确停库方法

本文介绍了两种正常停止MongoDB数据库的方法:一是通过查找mongod进程并使用kill-2命令来停止;二是通过MongoDB shell执行db.shutdownServer()命令来关闭数据库服务。这两种方法能够确保数据库的稳定性和数据的安全。
介绍两种正常停止MongoDB数据库的方法:
方法1:
kill -2法:
实验如下:
bash-4.2$ ps -ef | grep mongo
................................................................................
mongo      3847      1  0 17:03 ?        00:00:00 /usr/libexec/gconfd-2
mongo      3898   3219  0 17:03 ?        00:00:00 /usr/bin/gnome-software --gapplication-service
mongo      3902      1  0 17:03 ?        00:00:00 /usr/libexec/tracker-store
mongo      3909      1  0 17:03 ?        00:00:03 /usr/bin/vmtoolsd -n vmusr
mongo      3954      1  0 17:03 ?        00:00:00 /usr/libexec/gvfsd-trash --spawner :1.3 /org/gtk/gvfs/exec_spaw/0
mongo      3976   3498  0 17:03 ?        00:00:00 /usr/libexec/ibus-engine-simple
mongo      3982      1  0 17:03 ?        00:00:00 /usr/libexec/evolution-calendar-factory
mongo      4226      1  0 17:04 ?        00:00:00 /usr/libexec/dconf-service
mongo      4252   3470  1 17:05 ?        00:00:56 /usr/lib64/firefox/firefox
mongo      4764      1  0 17:20 ?        00:00:00 /usr/libexec/gvfsd-metadata
mongo      5409      1  0 17:27 ?        00:00:02 /usr/libexec/gnome-terminal-server
mongo      5412   5409  0 17:27 ?        00:00:00 gnome-pty-helper
mongo      5413   5409  0 17:27 pts/0    00:00:00 bash
mongo      6173      1  0 17:29 ?        00:00:14 mongod --config /etc/mongodb.conf --rest
mongo      8388   5413  0 18:07 pts/0    00:00:00 ps -ef
mongo      8389   5413  0 18:07 pts/0    00:00:00 grep mongo


找到mongod --config那一行对应的pid,然后执行:

bash-4.2$ kill -2 6173  

注意:不能使用kill -9 PID 杀死进程,这样可能导致MongoDB数据库损坏。


再次连接MongoDB发现报错:
bash-4.2$ mongo
MongoDB shell version: 3.2.7-39-g8da92ea
connecting to: test
2016-07-01T18:07:54.741+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2016-07-01T18:07:54.741+0800 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:229:14
@(connect):1:6


exception: connect failed
bash-4.2$ 


方法2:
用命令db.shutdownServer(),实验如下:
再次启动mongodb
bash-4.2$ mongod --config /etc/mongodb.conf --rest
about to fork child process, waiting until server is ready for connections.
forked process: 8570
child process started successfully, parent exiting

bash-4.2$ mongo
MongoDB shell version: 3.2.7-39-g8da92ea
connecting to: test
Server has startup warnings: 
2016-07-01T18:12:42.307+0800 I CONTROL  [initandlisten] 
2016-07-01T18:12:42.307+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-07-01T18:12:42.307+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-07-01T18:12:42.307+0800 I CONTROL  [initandlisten] 
2016-07-01T18:12:42.307+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-07-01T18:12:42.307+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-07-01T18:12:42.307+0800 I CONTROL  [initandlisten] 
> use admin
switched to db admin
> db.shutdownServer();
server should be down...
2016-07-01T18:13:09.344+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-07-01T18:13:09.345+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2016-07-01T18:13:09.345+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed 



再次连接MongoDB发现报错:
bash-4.2$ mongo
MongoDB shell version: 3.2.7-39-g8da92ea
connecting to: test
2016-07-01T18:13:36.603+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2016-07-01T18:13:36.603+0800 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:229:14
@(connect):1:6

exception: connect failed

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15498/viewspace-2121355/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15498/viewspace-2121355/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值