MongoDB

Jump to: navigation, search
MongoDB is one of the most popular document databases. This article introduces how to use MongoDB on openSUSE.

Tested on openSUSE

Recommended articles

Related articles


Install MongoDB

MongoDB hasn't been included in official repositories. You need to install it form server:database repository. The following link provides both one-click install (for desktop environment) and command line way (for server environment).

https://software.opensuse.org//download.html?project=server%3Adatabase&package=mongodb

Newer version MongoDB only available on x86_64 system.

Create Btrfs subvolume (recommend)

/var/lib/mongodb is where MongoDB stores data by default. However, it is not a Btrfs subvolume and snapper will take snapshot of data changes. If your root file system is Btrfs and has enabled snapshots, changes of MongoDB data will lead to large Btrfs snapshots.

If it is only used for local testing, people usually won't rollback data in database. So it is just a waste of storage space. You can make a Btrfs subvolume and disable snapshots on it. In server, it is often useful to make subvolume for /var/lib/mongodb, too. It enables you to make snapshots independent to operating system snapshots.

The steps of creating subvolume is easy but you should be very careful! You need to run it as root and make sure your MongoDB server has been stopped!

# Stop MongoDB server if it is running
systemctl stop mongodb

# Create a new subvolume
btrfs subvolume create /var/lib/mongodb-temp

# This new subvolume is owned by root.
# We need to change owner to mongodb user and mongodb group
chown mongodb:mongodb /var/lib/mongodb-temp

# Move all data into new subvolume
mv /var/lib/mongodb/* /var/lib/mongodb-temp

# Remove old directory
rmdir /var/lib/mongodb

# Rename new subvolume
mv /var/lib/mongodb-temp /var/lib/mongodb

# Restart MongoDB (optional)
systemctl start mongodb

First run

openSUSE's configuration

openSUSE's mongodb package contains a default configuration at /etc/mongodb.conf and a systemd service file. Advantages of this configuration are:

1. Save data in typical Linux directory, /var/lib/mongodb, rather than /data/db. 2. Enable some common settings for security and convenience. 3. Use the power of systemd.

If you want to run this configuration. Just use systemd commands:

sudo systemctl start mongodb

To run mongodb at system booting:

sudo systemctl enable mongodb

Your own configuration

If you want more freedom, for example, you want to run multiple MongoDB servers with different configurations, you can use mongod command.

mongod --dbpath /database/mongodb1 --port 7000
mongod --dbpath /database/mongodb2 --port 8000
mongod --dbpath /database/mongodb3 --port 9000

Read MongoDB Reference for detailed information.

MongoDB Shell

MongoDB Shell (package: mongodb-shell) is a command line client. When your MongoDB server is already running, you can login MongoDB with mongo command:

gekko@opensuse:~> mongo
MongoDB shell version v3.4.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.3
>

Create admin user

When you installed MongoDB, here is no user defined. You can login without providing username or password. However, if you want to create database and manage users, you have to create an administrator account.

use admin

db.createUser({
    user: "root",
    pwd: "password",
    roles: [ "root" ]
})

exit
  1. use admin means you switch to admin database.
  2. db.createUser() let you create a user in current database.
  3. user: username, can be anything you like.
  4. pwd: password.
  5. roles: roles of user. "root" role give you all power to manage MongoDB.
  6. exit the command to exit mongo shell.

Next time, you can login with admin user:

mongo --authenticationDatabase admin -u root -p

Note: you need to provide the database name ("--authenticationDatabase admin") where you create admin user. Otherwise, authentication will fail.

Create database with user

openSUSE's mongodb configuration force application to provide username and password to access database in MongoDB.

If you have an application, and you would like to create a database for it, do following steps:

Step 1: login mongo shell as admin user

mongo --authenticationDatabase admin -u root -p

Step 2: create database and add a user with necessary permissions

use japaripark

db.createUser({
    user: "serval",
    pwd: "tanoshi",
    roles: [ "readWrite", "dbAdmin" ]
})

Step 3: fill database information in your project

Here is a NodeJS example:

var MongoClient = require('mongodb').MongoClient,
  f = require('util').format,
  assert = require('assert');

var user = encodeURIComponent('serval');
var password = encodeURIComponent('tanoshi');
var authMechanism = 'DEFAULT';

// Connection URL
var url = f('mongodb://%s:%s@localhost:27017/japaripark?authMechanism=%s',
  user, password, authMechanism);

// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  db.close();
});

Communication

Team members

Mailing list


See also

Related articles

External links