how to set mogodb in upstart service with authentication

后端 未结 4 998
醉梦人生
醉梦人生 2021-01-07 09:45

I have created a superuser tom in mongodb v2.6.11 with username & password and now i want to add this at ubunt

相关标签:
4条回答
  • 2021-01-07 10:08

    1 : Got to -> $cd /etc/init/

    2 :Through vi Or nano editor Create file myservice.conf with below code

    description "service to start mongodb at startup"
    author "plutopunch :)"
    
    start on started mountall
    stop on shutdown
    
    respawn
    respawn limit 99 5
    
    script
         export HOME="/home/admin"
    
         exec sudo mongod --port 27017 --auth --dbpath /var/lib/mongodb
    end script
    
    post-start script
    end script
    

    3 : restart pc

    http://upstart.ubuntu.com/cookbook/

    0 讨论(0)
  • 2021-01-07 10:11

    There is absolutely no need to fiddle with the startup files (which you really should not do unless you exactly know what you are doing) or install additional software for managing MongoDB.

    I'd rather suggest to read MongoDB's extensive documentation of the config file options.

    The only thing you need to do is to set

    auth=true
    

    for legacy config files or

    security:
      authorization: enabled
    

    for YAML config files in /etc/mongod.conf

    0 讨论(0)
  • 2021-01-07 10:12

    Thanks @Markus W mahalberg extending your answer in detailed steps

    Correct Mongodb Way to set mongo in startup with authentication


    Steps :

    1 : sudo apt-get install mongodb-org - in new terminal

    2 : sudo mongod --port 27017 --dbpath /var/lib/mongodb

    3 : mongo --port 27017 - in new terminal

    4 : use admin

    5 : Lets setup superuser

    db.createUser(
    {
        user: "tom",
        pwd: "jerry",
        roles: [
                  { role: "userAdminAnyDatabase", db: "admin" },
                  { role: "readWriteAnyDatabase", db: "admin" },
                  { role: "dbAdminAnyDatabase", db: "admin" },
                  { role: "clusterAdmin", db: "admin" }
               ]
    })
    

    6 : Lets configure for Startup service for that Do Changes In /etc/mongod.conf and set

    auth = true

    port = 27017

    7 : sudo /etc/init.d/mongod stop OR sudo service mongod stop - in new terminal

    8 : sudo /etc/init.d/mongod start OR sudo service mongod start

    9 : restart your pc and before restarting your pc please empty this file /var/log/mongodb/mongod.log and save

    Now lets check TWO things Authentication & mongodb running at startup

    1 :Check Authentication setup

    mongo --port 27017 -u "tom" -p "jerry" --authenticationDatabase "admin" - in new terminal

    Note : this step is most important step .

    it will give Output on terminal like

    MongoDB shell version: 2.6.11
    connecting to: 127.0.0.1:27017/test
    >
    

    2 :Check if mongodb is running at startup

    Run below command without running mongod as mongod should have started in startup

    mongo --port 27017 -u "tom" -p "jerry" --authenticationDatabase "admin" - in new terminal

    If it dosent start then open this file '/var/log/mongodb/mongod.log' .

    if it contains Error log :

    Unable to create/open lock file: /var/lib/mongodb/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

    Then apply these permissions and restart and again check

    sudo chown -R mongodb:mongodb /var/lib/mongodb/.
    sudo chown -R mongodb:mongodb /var/log/mongodb/mongod.log
    sudo /etc/init.d/mongod stop` OR `sudo service mongod stop    
    sudo /etc/init.d/mongod start` OR `sudo service mongod start 
    

    If it does not contains above error log then you should have successfully started mongodb at startup

    0 讨论(0)
  • 2021-01-07 10:14

    Alternatively possible to use supervisor:

    apt install supervisor
    

    Setting for process mongodb:

    create file myupstartservice.conf at location /etc/supervisor/conf.d and put below code

    [program:mongo]
    command=/usr/bin/mongod --auth --config /etc/mongod.conf
    autostart=true
    autorestart=true
    user=root
    priority=100
    

    then checkservice

    service supervisor restart

    some reference

    0 讨论(0)
提交回复
热议问题