ubuntu:启动(upstart)mongodb的第二个实例

pky*_*eck 12 linux ubuntu daemon upstart mongodb

mongodb附带的标准upstart脚本运行正常:

# Ubuntu upstart file at /etc/init/mongodb.conf

limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
    mkdir -p /var/lib/mongodb/
    mkdir -p /var/log/mongodb/
end script

start on runlevel [2345]
stop on runlevel [06]

script
  ENABLE_MONGODB="yes"
  if [ -f /etc/default/mongodb ]; then . /etc/default/mongodb; fi
  if [ "x$ENABLE_MONGODB" = "xyes" ]; then exec start-stop-daemon --start --quiet --chuid mongodb --exec  /usr/bin/mongod -- --config /etc/mongodb.conf; fi
end script
Run Code Online (Sandbox Code Playgroud)

如果我想运行mongod的第二个实例我想我只是复制/etc/mongodb.conf- > /etc/mongodb2.conf/etc/init/mongodb.conf- > /etc/init/mongodb2.conf并更改第一个conf文件中的std端口.然后调整上面的脚本以从新创建的开始/etc/mongodb2.conf.

然后我可以说start mongodb2,服务开始......但它在开始后就被杀死了.我要改变什么,让两个进程都启动并运行?

 # Ubuntu upstart file at /etc/init/mongodb2.conf

limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
    mkdir -p /var/lib/mongodb2/
    mkdir -p /var/log/mongodb2/
end script

start on runlevel [2345]
stop on runlevel [06]

script
  ENABLE_MONGODB="yes"
  if [ -f /etc/default/mongodb ]; then . /etc/default/mongodb; fi
  if [ "x$ENABLE_MONGODB" = "xyes" ]; then exec start-stop-daemon --start --quiet --chuid mongodb --exec  /usr/bin/mongod -- --config /etc/mongodb2.conf; fi
end script
Run Code Online (Sandbox Code Playgroud)

pky*_*eck 10

我无法让"标准"新贵脚本工作(如上所述),所以我改变了这样:

# Ubuntu upstart file at /etc/init/mongodb.conf

limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
    mkdir -p /var/lib/mongodb/
    mkdir -p /var/log/mongodb/
end script

start on runlevel [2345]
stop on runlevel [06]

script

  exec sudo -u mongodb /usr/bin/mongod --config /etc/mongodb.conf

end script
Run Code Online (Sandbox Code Playgroud)

如果你想运行的MongoDB的其他实例只复制*conf文件并进行修改/etc/mongodb2.conf/etc/init/mongodb2.conf

# Ubuntu upstart file at /etc/init/mongodb2.conf

limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
    mkdir -p /var/lib/mongodb2/
    mkdir -p /var/log/mongodb2/
end script

start on runlevel [2345]
stop on runlevel [06]

script

  exec sudo -u mongodb /usr/bin/mongod --config /etc/mongodb2.conf

end script
Run Code Online (Sandbox Code Playgroud)

我认为这是不工作的唯一事情就是restart mongodb-你必须stopstart一次...


小智 5

我知道已经有一个公认的解决方案,但我认为这个更优雅.

另一种方法是使用start-stop-daemon的pid文件创建.例如,我有2个mongos在同一台服务器上运行,有2个不同的upstart脚本,两个魔术线是:

exec start-stop-daemon --make-pidfile --pidfile /var/run/mongodb-router.pid --start --startas /data/bin/mongos --chuid mongo -- --logappend --logpath /mnt/log/mongos.log --configdb mongo2-config01,mongo2-config02,mongo2-config03


exec start-stop-daemon --make-pidfile --pidfile /var/run/mongodb-routerrt.pid --start --startas /data/bin/mongos --chuid mongo -- --logappend --logpath /mnt/log/mongos-rt.log --configdb mongort-config01,mongort-config02,mongort-config03 --port 27027
Run Code Online (Sandbox Code Playgroud)

请注意,有一个'--pidfile /var/run/mongodb-router.pid',另一个有'--pidfile /var/run/mongodb-routerrt.pid'和一个不同的端口.