Ruby on Rails Development Using Mongoid 5.0.0 - 1. Setup MongoDB

MongoDB

This tutorial series will help you start your Rails project with MongoDB.

And I use Mongoid 5.0.0 as an example.

In this tutorial, you will be able to see how to

  1. Install MongoDB in Mac OSX

  2. Create Some Database Users in MongoDB

  3. Setup Rails Projects

(Tips)

  1. Dump Data

  2. Restore Data

Let’s go !

Install MongoDB in Mac OSX

1
2
3
4
5
6
7
8
9
10
11
# Intall Homebrew (Mac OSX package manager)

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

# Update the Hombrew packages first

brew update

# Install MongoDB

brew install MongoDB

If you want to see where MongoDB is installed

1
brew ls mongodb

Create Some Database Users in MongoDB

Since I want to enable access control on a MongoDB instance, just like Mysql, I have to use username and password to access MongoDB.

First, you must to know where you can setup MongoDB.

1
2
3
4
5
6
7
# In MacOSX

vim /usr/local/etc/mongod.conf

# In Ubuntu 

vim /etc/mongod.conf

Second, you have to make sure that authorization is disable.

In MacOSX

1
2
3
4
5
6
7
8
9
10
11
12
# /usr/local/etc/mongod.conf

systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1
security:
  authorization: disabled 

In Ubuntu

1
2
3
4
5
6
# /etc/mongod.conf

...

noauth = true
...

Then (re)start MongoDB

1
2
3
4
5
# In Ubuntu
sudo service mongod restart

# In Mac OSX
mongod --config /usr/local/etc/mongod.conf

Ok, now you are a super user, you can access any database and perform any action. let’s create an administrator named ‘siteUserAdmin’ and he has “userAdminAnyDatabase” role first.

1
2
3
4
5
6
7
8
9
$ mongo
$ use admin
$ db.createUser(
 {
  user: 'siteUserAdmin',
  pwd: '1234567890',
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
 }
)

Ok, now we can enable authorization mode.

In MacOSX

1
2
3
4
5
6
# vim /usr/local/etc/mongod.conf

...

security:
  authorization: enabled 

In Ubuntu

1
2
3
4
5
6
# /etc/mongod.conf

...

auth = true
...

And don’t forgot restart MongoDB.

Then we now have to use user name and password to access database.

1
mongo --host localhost --port 27017 --username siteUserAdmin --password  --authenticationDatabase admin

Now, we will create an user named ‘dbadmin’ who has ‘dbOwner’ role.

1
2
3
4
5
6
7
8
9
10
11
# assume your project will use database: 'your_awesome_project_development'

use your_awesome_project_development

db.createUser(
    {
        user: 'dbadmin',
        pwd: '1234567890',
        roles: [ { role: "dbOwner", db: "your_awesome_project_development" } ]
    }
)

if you want to update user’s setting.

1
2
3
4
5
6
7
8
9
10
db.updateUser(
    "dbadmin",
    {
        pwd: 'aloha',
        roles: 
          [
              {role: "read", db: "your_awesome_project_development"}
          ]
    }
)

if you want to know which roles can perform which actions, you can find anwser here:

http://docs.mongodb.org/master/reference/built-in-roles/#userAdmin

Setup Rails Projects

1. create a project without active-record

1
rails new your_awesome_project --skip-active-record

2. add Mongoid to Gemfile

1
2
3
# Gemfile

gem 'mongoid', '~> 5.0.0'

3. Create a mongoid.yml

1
2
bundle install
rails g mongoid:config

4. Update yout mongoid.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
development:
  clients:
    default:
      database: your_awesome_project_development
      hosts:
        - localhost:27017
      options:
        user: 'dbadmin'
        password: '1234567890'
        roles:
          - 'dbOwner'
test:
  clients:
    default:
      database: your_awesome_project_test
      hosts:
        - localhost:27017
      options:
        max_retries: 1
        retry_interval: 0
        user: 'dbadmin_tester'
        password: '1234567890'
        roles:
          - 'dbOwner'

Dump Data

Please make sure that you have backup role in production MongoDB.

I use ‘readWriteAnyDatabase’ role.

Assume that you need to dump data from production data and restore to local mongo server.

1
mongodump --host your.production.mongo.server.ip --port 37017 --username user --password --out /Users/AlohaCC/Desktop/production-mongodump-2015-10-04

Restore Data

Please make sure that you have backup role in local MongoDB.

1
mongorestore --host localhost --port 3017 --username user --password  /Users/AlohaCC/Desktop/production-mongodump-2015-10-04
Comments

Comments

Google Analytics Alternative