Saturday, July 7, 2018

[MEMO] Setup local mongod (unsecured)

1. Download mongod (version >= 3.4, so it supports Decimal128)

2. Do not start mongod with --auth, setup a script and run the daemon just like this:
#!/bin/sh
./mongod \--dbpath /Users/tyliu2/mongodb-osx-x86_64-3.6.4/data/db \--logpath /Users/tyliu2/mongodb-osx-x86_64-3.6.4/logs/mongod.log \--pidfilepath /Users/tyliu2/mongodb-osx-x86_64-3.6.4/logs/mongod.pid \--port 27017
3. Start mongod as above

4. Choose and download your favourite mongo db client (I chose Robo 3T)

5. Login with direct connection

6. Setup username and password for admin
use admin
db.createUser({
    user: "admin",
    pwd: "XXXXXXXX",
    roles: [
        { role: "root", db: "admin" }
    ], 
              passwordDigestor: "server" //if version 4.0 else set it false
})
**!! Please note that in MongoDB ver4, you need to specify passwordDigestor

7. Add "--auth" to the mongod startup script
#!/bin/sh
./mongod \        --auth \        --dbpath /Users/tyliu2/mongodb-osx-x86_64-3.6.4/data/db \        --logpath /Users/tyliu2/mongodb-osx-x86_64-3.6.4/logs/mongod.log \        --pidfilepath /Users/tyliu2/mongodb-osx-x86_64-3.6.4/logs/mongod.pid \        --port 27017
8. Stop and restart mongod with the above script

9. Login with user "admin" and its password

10. Create a new user that uses a new db for your application
use test
db.createUser({
    user: "test",
    pwd: "XXXXXXXX",
    roles: [
        { role: "readWrite", db: "test" }
    ]
})

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home