mongodb is a document type database aka NoSQL. The power of NoSQL DBs are its flexibility of unfixed schemas unlike in mysql/oracle SQL based database. A schema enforces fixed columns; thus columns will need to be defined before the database can be used. NoSQL type on the other hand allows key:value type of definition in each document (just like records), and these can differ from other documents in the same set.
In this guide, are references to basic mongodb commands that should get you started.
installing mongodb
on macs use homebrew (https://brew.sh/) if you do not have it; get it. once installed, you can view the list of brew packages via the following command in terminal:
$ brew search [yourpackage]
$ brew search mongo
now you can install mongodb via
$ brew install mongodb
on linux (debian or ubuntu) use the following: (reference :https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ )
$ sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 9DA31620334BD75D9DCB49F368818C72E52529D4
$ echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
$ sudo apt-get update
$ sudo apt-get install -y mongodb-org
you are highly encouraged to refer to the above url (of mongodb community) to check the keys as well as the version of your OS so that you can install the appropriate distribution package intended for.
adding root user
If no users are configured in admin.system.users, one may access the database from the localhost interface without authenticating. Thus, from the server running the database (and thus on localhost), run the database shell and configure an administrative user:
$ mongo
$ use admin
$ db.createUser( { user: “mongoroot”, pwd: “mysuperdupermongorooterpassword”, roles: [ { role: “root” , db: “admin” } ] } )
$ db.createUser( { user: “mongoadmin”, pwd: “mysuperdupermongorooterpassword”, roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ] } )
now remove admin user
$ use admin
$ db.dropUser(“mongoadmin”)
turn on authentication by editing mongo configuration file. go to approximately line 28 and remove the # from auth=true. if you are not familar with vim, nano is just as good.
$ vim /etc/mongod.conf
now relaunch mongodb
$ service mongod restart
test logging in by using the mongo command with parsing password
$ mongo –username mongoroot –password –authenticationDatabase admin
setup single db admin
$ use mynewdb
$ db.createUser( { user: “accountUser”, pwd: “password”, roles: [ “readWrite”, “dbAdmin” ] } )
ref: http://docs.mongodb.org/manual/reference/method/db.createUser/
mongodb default location
- configuration: /etc/mongodb.conf
- database: /var/lib/mongodb
if you install via homebrew on mac, this will be different
mongodb basic commands
list all databases
$ show dbs
select a database
$ use mynewdb
show current db
$ db
add user
$ use mynewdb
$ db.createUser( { user: “mynewadmin”, pwd: “passwordchangeme”, roles: [ “readWrite”, “dbAdmin” ] } )
change password
$ db.changePassword(“mynewadmin”, “yetanothernewpassword”)
drop user
$ db.dropUser(“mynewadmin”)
add master admin
$ use admin
$ db.createUser( { user: “mongoroot”, pwd: “mysuperdupermongorooterpassword”, roles: [ { role: “root” , db: “admin” } ] } )
$ db.createUser( { user: “mongoadmin”, pwd: “mysuperdupermongoadminpassword”, roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ] } )
restarting mongo
$ service mongod restart
a sample setup mongodb
- user: mongoprodroot
- user: mongoprodadmin
$ db.createUser( { user: “mongoprodroot”, pwd: “yetanotheruniquepassword”, roles: [ { role: “root” , db: “admin” } ] } )
$ db.createUser( { user: “mongoprodadmin”, pwd: “yetanotheruniquepassword”, roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ] } )
vultr referral link
if you are setting up a cloud server, my referral link banner is below