MongoDB

From RHS Wiki
Jump to navigation Jump to search

Installation

Ubuntu 14.04, 15.10

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
sudo apt-get update
sudo apt-get install -y mongodb-org

Check if service is active

sudo systemctl status mongd

If it's not working

sudo reboot

If it still not working, create and run the script enable_mongo.sh with:

echo '[Unit]
Description=High-performance, schema-free document-oriented database
After=syslog.target network.target
 
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod -f /etc/mongod.conf
 
[Install]
WantedBy=multi-user.target' > /lib/systemd/system/mongod.service

# Setup the required directories
mkdir -p /var/run/mongodb/
mkdir -p /var/log/mongodb/
mkdir -p /var/lib/mongodb/
mkdir -p /data/db/

chown mongodb:mongodb /var/run/mongodb/
chown mongodb:mongodb /var/log/mongodb/
chown mongodb:mongodb /var/lib/mongodb/
chown mongodb:mongodb /data/db

chmod 0755 /var/log/mongodb/
chmod 0755 /var/run/mongodb/
chmod 0755 /var/lib/mongodb/
chmod 0755 /data/db/

# Start the new service and enable it on boot
systemctl --system daemon-reload
systemctl enable mongod.service

echo "Starting"
systemctl start mongod.service
# Specific version install: sudo apt-get install -y mongodb-org=3.2.1 mongodb-org-server=3.2.1 mongodb-org-shell=3.2.1 mongodb-org-mongos=3.2.1 mongodb-org-tools=3.2.1

Enable authentication

  • Connect to mongo instance:
mongo
  • Add an admin user:
use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
  • Edit configuration file
sudo nano /etc/mongod.conf
  • Restart mongoDB server:
sudo service mongod restart
  • connect to your instance:
security:
  authorization: enabled

Start/Stop/Restart MongoDB

sudo service mongod start | stop | restart

Verify that MongoDB has started successfully

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

[initandlisten] waiting for connections on port <port>

Guides

Enable authentication
Getting Started
Before deploying MongoDB in a production environment
Manage user accounts and roles

Data directories

/var/log/mongodb
/var/lib/mongodb

Create Database

use DATABASE_NAME

Show databases

show dbs
db.adminCommand('listDatabases')
db.getMongo().getDBNames()
  • To view databases they must have data

To show the current database type:

db

Insert

db.COLLECTION_NAME.insert({"name":"data1"})

Get Colection Names

db.getCollectionNames()

Query

db.COLLECTION_NAME.find()
db.COLLECTION_NAME.find({"name": "data1"})

RDBMS Where Clause Equivalents in MongoDB

Operation Syntax Example RDBMS Equivalent
Equality {<key>:<value>} db.mycol.find({"by":"tutorials point"}).pretty() where by = 'tutorials point'
Less Than {<key>:{$lt:<value>}} db.mycol.find({"likes":{$lt:50}}).pretty() where likes < 50
Less Than Equals {<key>:{$lte:<value>}} db.mycol.find({"likes":{$lte:50}}).pretty() where likes <= 50
Greater Than {<key>:{$gt:<value>}} db.mycol.find({"likes":{$gt:50}}).pretty() where likes > 50
Greater Than Equals {<key>:{$gte:<value>}} db.mycol.find({"likes":{$gte:50}}).pretty() where likes >= 50
Not Equals {<key>:{$ne:<value>}} db.mycol.find({"likes":{$ne:50}}).pretty() where likes != 50

AND / OR Example:
Likes greater than 100 and whose title is either 'MongoDB Overview' or by is 'tutorials point'.
Equivalent sql where clause is 'where likes>10 AND (by = 'tutorials point' OR title = 'MongoDB Overview')'

db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"}, {"title": "MongoDB Overview"}]}).pretty()

Security

To enable Client Access Control follow this steps:

  1. Start the mongod service if not already running
    sudo service mongod start
    
    or if runnig mongo directly:
    mongod --port 27017 --dbpath /data/db1
  2. Connect to the mongo instance without access control
    mongo --port 27017
  3. Create the user administrator
    use admin
    db.createUser(
    {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    }
    )
  4. Edit /etc/mongod.conf, add:
    security:
    
    authorization: enabled
  5. Restart mongo service
     sudo service mongod restart
  6. Connect to the mongo instance as the administrator:
    mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"
  7. Create Additional users as needed. Example:
    use reporting
    
    db.createUser(
    {
    user: "reportsUser",
    pwd: "12345678",
    roles: [
    { role: "read", db: "reporting" },
    { role: "read", db: "products" },
    { role: "read", db: "sales" },
    { role: "readWrite", db: "accounts" }
    ]
    }
    )