Skip to content

Instantly share code, notes, and snippets.

@vamjakuldip
Last active September 26, 2020 10:44
Show Gist options
  • Save vamjakuldip/59bb86f61c9b17a88bc0c228f5ce62a3 to your computer and use it in GitHub Desktop.
Save vamjakuldip/59bb86f61c9b17a88bc0c228f5ce62a3 to your computer and use it in GitHub Desktop.
mongodb create databse, create admin user, create database user, create database url
// After install mongo db.
// commmand to start mongodb service
mongo
// go to admin databse
use admin
// Create the root user
db.createUser({user:"admin", pwd:"password", roles:[{role:"root", db:"admin"}]})
Exit from the MongoDB shell.
//restart and authenticate mongodb
mongo -u admin -p password --authenticationDatabase admin
// You can see the mongo connecting. Check the databases using the following command:
show dbs
// create new database
use newdatabse
// add atleast one doc to that database
db.createCollection("mycollection")
{ "ok" : 1 }
// add user to databse
db.createUser(
{
user: "newUser",
pwd: "password",
roles:[{role: "userAdmin" , db:"newdatabse"}]})
// database connection url
mongodb://newUser:password@localhost:27017/newdatabse
//when conncetion timeout use below commant
// check services that using mongodb
ps -eaf | grep mongod
// kill server using commant
sudo kill <pId>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment