MongoDB Install and mongo shell
MongoDB 3.0
Import the public key used by the package management system. This is because the Ubuntu package management tools (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys.
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Then, we need to create the /etc/apt/sources.list.d/mongodb-org-3.0.list list file using the following command:
$ echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
Reload the local package database:
$ sudo apt-get update
Install the latest stable version of MongoDB:
$ sudo apt-get install -y mongodb-org
Before we start MongoDB for the first time, create the directory to which the mongodb process will write data. By default, the mongodb process uses the /data/db directory. If we create a directory other than this one, we must specify that directory in the dbpath option when starting the mongodb process later in this procedure:
$ sudo mkdir -p /data/db
Once the MongoDB packages are installed, we can configure username and password for the database server.
Before we set up a username and password for MongoDB, we need to open mongodb shell on your server. We can login by typing:
$ mongo
Once we're in the MongoDB shell, switch to the database named admin:
> use admin switched to db admin
Create the root user with the following command:
> db.createUser({user:"admin", pwd:"admin", roles:[{role:"root", db:"admin"}]}) Successfully added user: { "user" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] }
Now, we've created user admin with password admin and have the permission/role as root and the database is admin.
The MongoDB runs using the mongodb user account. Issue the following command to start mongod:
$ sudo service mongod start
The MongoDB instance stores its data files in /var/lib/mongodb and its log files in /var/log/mongodb by default.
We can verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading:
$ tail -2 /var/log/mongodb/mongod.log 2015-08-26T11:02:12.293-0700 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, storage: { dbPath: "/var/lib/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } } 2015-08-26T11:02:12.713-0700 I NETWORK [initandlisten] waiting for connections on port 27017
Note that the default port configured in /etc/mongod.conf is 27017:
$ netstat -nlpt|grep 27017 tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN -
Here is the summary of MongoDB at wiki:
MongoDB (from "humongous") is a cross-platform document-oriented database. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (BSON), making the integration of data in certain types of applications easier and faster.
- mongod is the "Mongo Daemon". It's basically the host process for the database. When we start mongod, we're basically saying "start the MongoDB process and run it in the background". mongod has several default parameters, such as storing data in /data/db and running on port 27017.
- mongo is the command-line shell that connects to a specific instance of mongod. When we run mongo with no parameters it defaults to connecting to the localhost on port 27017.
Collection is a group of MongoDB documents.
A collection is the equivalent of an RDBMS table.
A collection exists within a single database.
Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection have a similar or related purpose.
When we run mongo without any arguments, the mongo shell will attempt to connect to the MongoDB instance running on the localhost interface on port 27017.
$ sudo service mongod start mongod start/running, process 22380 $ mongo MongoDB shell version: 3.0.6 connecting to: test ...
To see dbs:
> show dbs local 0.078GB mean-development 0.078GB myFirstMB 0.203GB rest_test 0.078GB test 0.078GB test-database 0.078GB todoApp 0.078GB todos 0.078GB
To switch current database to <test>:
> use test switched to db test
To print a list of all collections for current database:
> show collections system.indexes todos
db is the variable that references the current database. The variable is automatically set to the default database test or is set when we use the use <db> to switch current database:
> db test
To get mongodb server stats, type the command db.stats() in mongodb client. This will show the database name, number of collection and documents in the database. Output the command is shown below:
> db.stats() { "db" : "test", "collections" : 3, "objects" : 7, "avgObjSize" : 75.42857142857143, "dataSize" : 528, "storageSize" : 20480, "numExtents" : 3, "indexes" : 1, "indexSize" : 8176, "fileSize" : 67108864, "nsSizeMB" : 16, "extentFreeList" : { "num" : 0, "totalSize" : 0 }, "dataFileVersion" : { "major" : 4, "minor" : 22 }, "ok" : 1 }
For more shell option, check mongo Shell Quick Reference
MongoDB 3.0
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization