renyanping 发表于 2018-10-26 12:10:06

Mongodb的启动和停止

  1、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:查看进程
  # ps -ef|grep -v grep |grep mongod
  root      362021320 14:05 pts/1    00:00:00 mongod --port 10001 --dbpath /opt/mongo/data/ --logpath /opt/mongo/logs/mongodb.log
  # cat mongod.lock
  3620
  #
  示例二:查看端口号
  # netstat -ntlp|grep 27017
  # netstat -ntlp|grep 10001
  tcp      0      0 0.0.0.0:10001               0.0.0.0:*                   LISTEN      3620/mongod
  #
  root@gflinux102 logs]# more mongodb.log
  2015-02-10T14:05:14.531+0800 MongoDB starting : pid=3620 port=10001 dbpath=/opt/mongo/data/ 32-bit host=gflinux102
  2015-02-10T14:05:14.531+0800
  2015-02-10T14:05:14.531+0800 ** NOTE: This is a 32 bit MongoDB binary.
  2015-02-10T14:05:14.531+0800 **       32 bit builds are limited to less than 2GB of data (or less with --journal).
  2015-02-10T14:05:14.531+0800 **       Note that journaling defaults to off for 32 bit and is currently off.
  2015-02-10T14:05:14.531+0800 **       See http://dochub.mongodb.org/core/32bit
  启动示例:
  # 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选项。
  # mongod --help|grep "-f"
  -f [ --config ] arg         configuration file specifying additional options
  #
  示例:
  mongod --config ~/.mongodb.conf
  配置文件模板如下,注意这个是手工编辑的:
  # 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
  # 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杀死
  # ps -ef|grep -v grep |grep mongod
  root      3854   10 15:06 ?      00:00:00 mongod -f /opt/mongo/data/mongod.conf
  或者这样查看pid:
  # cat /opt/mongo/data/mongod.lock
  3854
  杀死进程:
  # kill `cat /opt/mongo/data/mongod.lock` (SIGTERM)
  # 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提供了辅助函数,来简化这一过程。
  # 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
  2015-02-10T15:37:43.973+0800 ** NOTE: This is a 32 bit MongoDB binary.
  2015-02-10T15:37:43.973+0800 **       32 bit builds are limited to less than 2GB of data (or less with --journal).
  2015-02-10T15:37:43.973+0800 **       Note that journaling defaults to off for 32 bit and is currently off.
  2015-02-10T15:37:43.973+0800 **       See http://dochub.mongodb.org/core/32bit
  2015-02-10T15:37:43.973+0800
  > show dbs
  admin(empty)
  local0.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

页: [1]
查看完整版本: Mongodb的启动和停止