search

Home  >  Q&A  >  body text

nosql - 如何使用bash脚本安全关闭mongodb服务的进程?

我直接kill掉mongodb的服务进程,下一次启动的时候就会出现unclean shutdown的提示。
我知道进入mongo程序进入mongo自己的shell可以关闭服务,不过如果我要使用bash将如何实现呢?
我知道mongo有一个--eval参数,可以直接运行命令,但如果我直接--eval "db.shutdownServer()"的话老是出现让我use admin的提示,但是如果我--eval "use admin;db.shutdownServer()"的话呢又说我语法错误,真不知道要怎么弄才能对

巴扎黑巴扎黑2878 days ago760

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-04-21 10:59:16

    I found the answer to this question myself:

    ./mongo admin --eval "shutdownServer()"

    That’s it. Of course, if you have a port or password, please add the corresponding parameters. The format can be seen in ./mongo --help
    The reason why I have been asking this question before is because --help says that adding a username requires -u username, which is similar to mysql, but I have tried it

    -u admin --eval "db.shutdownServer()"

    Still not working, I don’t know if it’s a bug

    reply
    0
  • 阿神

    阿神2017-04-21 10:59:16

    Please try to use the kill -2 command to close the corresponding mongod process

    reply
    0
  • 怪我咯

    怪我咯2017-04-21 10:59:16

    ./mongo ip:port/admin --eval "db.shutdownServer()"

    reply
    0
  • PHPz

    PHPz2017-04-21 10:59:16

    mongo -u zhangsan -p 123456  127.0.0.1:27277/admin --eval "db.shutdownServer()"
    使用这个命令,配合supervisor简直是爽到爆

    reply
    0
  • Cancelreply