简体   繁体   English

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

[英]ubuntu: start (upstart) second instance of mongodb

the standard upstart script that comes with mongodb works fine: 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

if i want to run a second instance of mongod i thought i just copy both /etc/mongodb.conf -> /etc/mongodb2.conf and /etc/init/mongodb.conf -> /etc/init/mongodb2.conf and change the std port in the first conf-file. 如果我想运行mongod的第二个实例,我想我只需复制/etc/mongodb.conf - > /etc/mongodb2.conf/etc/init/mongodb.conf - > /etc/init/mongodb2.conf更改第一个conf文件中的std端口。 then adjust the script above to start with the newly created /etc/mongodb2.conf . 然后调整上面的脚本以从新创建的/etc/mongodb2.conf

i can then just say start mongodb2 and the service starts ... but it is killed right after starting. 然后我可以说start mongodb2start mongodb2服务......但它在启动后立即被杀死。 what do i change, to get both processes up and running? 我要改变什么,让两个进程都启动并运行?

 # 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

i couldn't get the "standard" upstart script to work (as described above), so i changed it like this: 我无法使用“标准”新贵脚本(如上所述),所以我改变了它:

# 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

and if you want to run other instances of mongodb just copy the *.conf files and make the changes to /etc/mongodb2.conf and /etc/init/mongodb2.conf 如果你想运行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

i think the only thing that is not working is restart mongodb - you have to stop and then start again ... 我认为唯一不起作用的是restart mongodb - 你必须stopstart ......

I know there's already an accepted solution but I think this one is more elegant. 我知道已经有一个公认的解决方案,但我认为这个更优雅。

The other way is to use start-stop-daemon's pid file creation. 另一种方法是使用start-stop-daemon的pid文件创建。 For example, I have 2 mongos running on the same server with 2 different upstart scripts, and the two magic lines are: 例如,我有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

Note that one has '--pidfile /var/run/mongodb-router.pid' and the other has '--pidfile /var/run/mongodb-routerrt.pid' and a different port. 请注意,有一个'--pidfile /var/run/mongodb-router.pid',另一个有'--pidfile /var/run/mongodb-routerrt.pid'和一个不同的端口。

This is how I do it. 我就是这样做的。 2 instances of mongodb, with start-stop-daemon, on the same server 在同一台服务器上有2个mongodb实例,带有start-stop-daemon

that are my start-stop-daemon configs 这是我的开始 - 停止 - 守护进程配置

exec start-stop-daemon --make-pidfile --pidfile /var/lib/mongodb/db1.pid --start --quiet --chuid mongodb --name mongod1 --exec /usr/bin/mongod -- --config etc/mongodb1.conf

exec start-stop-daemon --make-pidfile --pidfile /var/lib/mongodb/db2.pid --start --quiet --chuid mongodb --name mongod2 --exec /usr/bin/mongod -- --config etc/mongodb2.conf

pay attention to the --name option. 注意--name选项。 That did the trick for me 这对我有用

Yeah I ran into this same issue today. 是的,我今天遇到了同样的问题。 The reason is that the default script uses the start-stop-daemon to start mongo, which is specifically designed to ensure that only one version of a process is running. 原因是默认脚本使用start-stop-daemon启动mongo,mongo专门用于确保只运行一个版本的进程。 You already figured out that one way to fix this is to not use start-stop-daemon and to start the binary yourself. 你已经发现解决这个问题的一种方法是不使用start-stop-daemon并自己启动二进制文件。 That's the way I do it too but I'd be curious to hear if there's a better way. 这也是我这样做的方式,但我很想知道是否有更好的方法。

I find the below upstart works for me 我发现下面的暴发户对我有用

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

description "manage mongodb instance"

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

limit nofile 20000 20000

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

env MONGODB_USER=mongodb
env MONGODB_DATA=/var/lib/mongodb/
env MONGODB_LOG=/var/log/mongodb/
env MONGODB_PID=/var/run/mongodb.pid

pre-start script
  if [ ! -d $MONGODB_DATA ]; then
    mkdir -p $MONGODB_DATA
    chown $MONGODB_USER:$MONGODB_USER $MONGODB_DATA
  fi

  if [ ! -d $MONGODB_LOG ]; then
    mkdir -p $MONGODB_LOG
    chown $MONGODB_USER:$MOGODB_USER $MONGODB_LOG
  fi
end script

exec start-stop-daemon --start --pidfile $MONGODB_PID --chuid $MONGODB_USER:$MONGODB_USER --exec /usr/bin/mongod -- --config /etc/mongodb/mongodb.conf

pre-stop exec start-stop-daemon --signal QUIT --stop --quiet --pidfile $MONGODB_PID --chuid $MONGODB_USER:$MONGODB_USER --exec /usr/bin/mongod -- --config /etc/mongodb/mongodb.conf

the two daemons cannot listen on the same tcp port, thus you have to change the --port parameter of mongod2 in order to listen to a different port. 两个守护进程无法侦听同一个tcp端口,因此您必须更改mongod2的--port参数才能侦听其他端口。
the two daemons cannot share the same data dir, thus you have to change the --data-dir parameter of mongod2. 两个守护进程无法共享相同的数据目录,因此您必须更改mongod2的--data-dir参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM