加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 服务器 > 系统 > 正文

Mongodb的启动和终止

发布时间:2023-11-10 16:41:40 所属栏目:系统 来源:
导读:  Mongod的启动

  1.1、Mongod的启动选项

   Mongod有许多可配置的选项,在命令行运行mongod --help可以查看所有选项,常用的选项如下:

  序号 选项 含义

  1 --dbpath 指定数据目录,
  Mongod的启动
 
  1.1、Mongod的启动选项
 
      Mongod有许多可配置的选项,在命令行运行mongod --help可以查看所有选项,常用的选项如下:
 
  序号 选项 含义
 
  1 --dbpath 指定数据目录,默认值是/data/db(Windows下是C:\data\db)。每个mongod进程都需要独立的数据目录,所以要是有3个mongod的实例,必须要有独立的数据目录。当mongod启动时,会在数据目录中创建mongod.lock文件,这个文件用于防止其他mongod进程使用该数据目录,其文件内容为mongod线程的pid号。
 
  2 --port 指定服务器监听的端口号,默认的端口号是27017,是个其他进程不怎么用的端口,要是运行多个mongod的进程,则要给每个指定不同的端口号
 
  3 --fork 以守护进程的方式运行Mongod,创建服务器进程
 
  4 --logpath 指定日志输出路径,而不是输出到命令行,如果对文件夹有写权限的话,系统会在文件不存在时创建它。它将覆盖已有文件,清除所有原来的日志记录,如果想保留原来的日志,还需使用--logappend选项。
 
  5 --config 指定配置文件,加载命令行未指定的各种选项。
 
  6 --httpinterface 启用http接口
 
 
 
      示例1:查看进程
 
  [root@gflinux102 data]# ps -ef|grep -v grep |grep mongod
 
  root      3620  2132  0 14:05 pts/1    00:00:00 mongod --port 10001 --dbpath /opt/mongo/data/ --logpath /opt/mongo/logs/mongodb.log
 
  [root@gflinux102 data]# cat mongod.lock
 
  3620
 
  [root@gflinux102 data]#
 
      示例二:查看端口号
 
  [root@gflinux102 data]# netstat -ntlp|grep 27017
 
  [root@gflinux102 data]# netstat -ntlp|grep 10001
 
  tcp        0      0 0.0.0.0:10001               0.0.0.0:*                   LISTEN      3620/mongod         
 
  [root@gflinux102 data]#
 
  root@gflinux102 logs]# more mongodb.log
 
  2015-02-10T14:05:14.531+0800 [initandlisten] MongoDB starting : pid=3620 port=10001 dbpath=/opt/mongo/data/ 32-bit host=gflinux102
 
  2015-02-10T14:05:14.531+0800 [initandlisten]
 
  2015-02-10T14:05:14.531+0800 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
 
  2015-02-10T14:05:14.531+0800 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
 
  2015-02-10T14:05:14.531+0800 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
 
  2015-02-10T14:05:14.531+0800 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
 
      启动示例:
 
  [root@gflinux102 bin]# mongod --port 10001 --dbpath /opt/mongo/data/ --logpath /opt/mongo/logs/mongodb.log
 
  2015-02-10T14:05:14.516+0800
 
  2015-02-10T14:05:14.517+0800 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
 
  2015-02-10T14:05:14.517+0800
 
      在32bit下,mongod只能处理2Gb的数据,注意生产中要使用64bit的机器。
 
  1.2MongoDB的配置文件
 
      MongoDB支持从文件获取配置信息。当需要的配置非常多或者要自动化运维时,就会用到这个,指定配置文件可以用-f或者--config选项。
 
  [root@gflinux102 logs]# mongod --help|grep "  -f"
 
    -f [ --config ] arg         configuration file specifying additional options
 
  [root@gflinux102 logs]#
 
      示例:
 
  mongod --config ~/.mongodb.conf
 
      配置文件模板如下,注意这个是手工编辑的:
 
  [root@gflinux102 bin]# mongod -f /opt/mongo/data/mongod.conf
 
  2015-02-10T15:06:28.199+0800
 
  2015-02-10T15:06:28.200+0800 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
 
  2015-02-10T15:06:28.200+0800
 
  about to fork child process, waiting until server is ready for connections.
 
  forked process: 3854
 
  child process started successfully, parent exiting
 
  [root@gflinux102 data]# vi mongod.conf
 
  # Start MongoDB as a daemon on port 10001
 
  port = 10001
 
  fork = true
 
  logappend = true
 
  dbpath = /opt/mongo/data
 
  logpath = /opt/mongo/logs/mongodb.log
 
     注意:命令行中哪些如--fork的开关选项,其值要设为true。
 
  1.3、停止MongoDB
 
  1.3.1前台进程运行在中断
 
      如果服务器进程作为前台进程运行在终端,直接CTL-C。
 
  1.3.2kill杀死
 
  [root@gflinux102 bin]# ps -ef|grep -v grep |grep mongod
 
  root      3854     1  0 15:06 ?        00:00:00 mongod -f /opt/mongo/data/mongod.conf
 
  或者这样查看pid:
 
  [root@gflinux102 bin]# cat /opt/mongo/data/mongod.lock
 
  3854
 
      杀死进程:
 
  [root@gflinux102 bin]# kill `cat /opt/mongo/data/mongod.lock` (SIGTERM)
 
  [root@gflinux102 bin]# kill -2 `cat /opt/mongo/data/mongod.lock` (SIGINT)
 
      当mongod收到SIGINT或者SIGTERM时,会稳妥退出,即会等到当前运行的操作或者文件预分配完成(需要一些时间),关闭所有打开的连接,将缓存的数据刷新到磁盘,最后停止。
 
      【禁止】:千万不要向运行中的mongodb发送SIGKILL(kill -9),这样会导致数据库直接关闭,可能会使数据文件损坏。
 
  1.3.3使用shutdown命令
 
      使用shutdown命令,{"shutdown":1}。这要在admin数据库下使用,shell提供了辅助函数,来简化这一过程。
 
  [root@gflinux102 bin]# mongo localhost:10001
 
  MongoDB shell version: 2.6.6
 
  connecting to: localhost:10001/test
 
  Server has startup warnings:
 
  2015-02-10T15:37:43.973+0800 [initandlisten]
 
  2015-02-10T15:37:43.973+0800 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
 
  2015-02-10T15:37:43.973+0800 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
 
  2015-02-10T15:37:43.973+0800 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
 
  2015-02-10T15:37:43.973+0800 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
 
  2015-02-10T15:37:43.973+0800 [initandlisten]
 
  > show dbs
 
  admin  (empty)
 
  local  0.078GB
 
  > use admin
 
  switched to db admin
 
  > db.shutdownServer()
 
  2015-02-10T15:39:04.616+0800 DBClientCursor::init call() failed
 
  server should be down...
 
  2015-02-10T15:39:04.624+0800 trying reconnect to localhost:10001 (127.0.0.1) failed
 
  2015-02-10T15:39:04.626+0800 warning: Failed to connect to 127.0.0.1:10001, reason: errno:111 Connection refused
 
  2015-02-10T15:39:04.627+0800 reconnect localhost:10001 (127.0.0.1) failed failed couldn't connect to server localhost:10001 (127.0.0.1), connection attempt failed
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章