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

介绍两种正常停止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

请使用浏览器的分享功能分享到微信等