1: Add the
MongoDB Repository
vi
/etc/yum.repos.d/mongodb.repo
[mongodb]
name=MongoDB
Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
Then exit
and save the file with the command :wq
2: Install
MongoDB
yum
install mongo-10gen mongo-10gen-server
|
|
Config
server
The config server processes are mongod instances that store
the cluster’s metadata.
More
Replca
Set
A MongoDB replica set is a cluster of mongod instances that
replicate amongst one another and ensure automated failover.
More
Mongos
mongos for “MongoDB Shard,” is a routing service for MongoDB
shard configurations that processes queries from the application layer, and
determines the location of this data in the sharded cluster, in order to
complete these operations.
More
Example:
Server Setup
Add
a new user
Create a new user named mongodb
on each server, this user will be the one who starts the mongodb processes.
|
adduser mongodb
su -
mongodb
|
Prepare
directories:
We need to prepare all the data and
log directories with proper privileges.
|
# Commands are using
to setup the server.
# Creating a
directory for data path.
sudo mkdir /var/lib/mongodb/dbs
sudo chown mongodb:mongodb
-R /var/lib/mongodb/dbs
cd /etc/
sudo mkdir mongodb
sudo chown mongodb:mongodb
-R /etc/mongodb
sudo cp mongodb.conf
mongodb/
sudo mv mongodb.conf
mongodb.conf.default
|
We did this on all the servers
running mongodb cluster.
Configuration
Servers:
Make a configuration file for
mongodb’s config server.
|
cd /etc/mongodb/
vi config_db.conf
|
The configuration file of mongod
running on config servers should have,
|
fork=true
dbpath=/var/lib/mongodb/dbs/config_db
logpath=/var/log/mongodb/config_db.log
logappend=true
port=27020
|
Last step, start the config server
by
|
sudo mongod
--configsvr --config /etc/mongodb/config_db.conf
|
And do the same thing on all 3
config servers of our own.
Mongos
First we need to create a
configuration file for mongos.
|
cd /etc/mongodb/
vi mongos.conf
|
The content in mongos configuration
file is
|
fork = true
port = 27017
configdb =
xxx.xxx.xxx.xxx:port,xxx.xxx.xxx.xxx:port,xxx.xxx.xxx.xxx:port # Here you
should put the domain name of your 3 configuration servers.
logpath=/var/log/mongodb/mongos.log
|
Now we start our mongos process.
|
mongos --config
/etc/mongodb/mongos.conf
|
Replica
Sets
First we need to create
configuration files for our mongod.
We have 3 Replica Sets and each set
has 3 mongod running, one of them is ”arbiter”, we create 3
configuration files
on each of our data server like.
|
cd /etc/mongodb/
touch set0_db.conf
touch set1_db.conf
touch set2_db.conf
|
The content inside should have
|
fork = true
port = 27017
dbpath=/var/lib/mongodb/dbs/set<index
of this set>_db
logpath=/var/log/mongodb/set<index
of this set>_db.log
logappend = true
journal = true
replSet =
set<index of this set>
|
And as usual, we start the mongod
process using command:
|
mongod --config
set<index of this set>_db.conf
|
Last step, we need to initialize
these 3 sets seperately.
set0
|
rs.initiate({_id:'set0',
members:[{_id: 0, host: 'xxx.xxx.xxx.xxx:port'}, {_id: 1, host:
'xxx.xxx.xxx.xxx:port'}]});
rs.addArb("xxx.xxx.xxx.xxx:port");
|
set1
|
rs.initiate({_id:'set1',
members:[{_id: 0, host: 'xxx.xxx.xxx.xxx:port'}, {_id: 1, host:
'xxx.xxx.xxx.xxx:port'}]});
rs.addArb("xxx.xxx.xxx.xxx:port");
|
set2
|
rs.initiate({_id:'set2',
members:[{_id: 0, host: 'xxx.xxx.xxx.xxx:port'}, {_id: 1, host:
'xxx.xxx.xxx.xxx:port'}]});
rs.addArb("xxx.xxx.xxx.xxx:port");
|
Add
Shards:
Now we can connect to ‘mongos’ and
add our 3 Replica Sets as 3 shards.
|
mongo --host
<domain name of mongos> --port <port of mongos running>
connecting to:
<domain name of mongos>/test
mongos>
sh.addShard("set0/<primary of set0>:<port>");
{
"shardAdded" : "set0", "ok" :
1 }
mongos>
sh.addShard("set1/<primary of set1>:<port>");
{
"shardAdded" : "set1", "ok" :
1 }
mongos>
sh.addShard("set2/<primary of set2>:<port>");
{
"shardAdded" : "set2", "ok" :
1 }
|
Enable
database sharding:
In order to make use of sharding in
MongoDB, we need to manually choose the database and collections those
we want them to be sharded.
Take our system as an example.
First connect to mongos,
|
mongo --host
<host> --port <port>
|
Then type the following commands in
mongo shell.
Here we need to set the collection ‘students’ in database ‘test’ being able to
be sharded.
|
use admin
sh.enableSharding("test")
sh.shardCollection("test.students",
{ "grades": 1 })
|
That’s it, we have successfully set
up our MongoDB Sharding Cluster!
Verify Sharding:
Now you need to find out if your
cluster is really working.
You can use the following code to verify the sharding we currently have.
|
mongo --host
198.211.98.146 --port 27017
use admin
db.runCommand( {
listshards : 1 } );
|
And you suppose to have the result
below
|
{
"shards" :
[
{
"_id" :
"set0",
"host" :
"set0/198.211.100.130:27018,198.211.100.172:27017"
},
{
"_id" :
"set1",
"host" :
"set1/198.211.100.130:27017,198.211.100.158:27018"
},
{
"_id" :
"set2",
"host" :
"set2/198.211.100.158:27017,198.211.100.172:27018"
}
],
"ok" :
1
}
|
Other Settings
Copy
DB
Sometimes, like we encountered once,
we need to change one of our config servers to another machine.
In this case, we need to do the
following things.
- Shutdown all processes (mongod, mongos, config server).
- Copy the data subdirectories (dbpath tree) from the
config server to the new config servers.
- Start the config servers.
- Restart mongos processes with the new –configdb
parameter.
- Restart mongod processes.
You can use this command to copy a
database from another server.
|
mongo --port 27020
use config
db.copyDatabase("config",
"config", "xxx.xxx.xxx.xxx:27020");
|
Logrotate
Since every day MongoDB generates a
lot of logs, we need a way to compress them and delete them after a period of
time.
So we can created 2 crontab jobs to
achieve this goal.
This script runs daily at 0:05AM to
collect the old logs and compress them.
|
#! /bin/sh
killall -SIGUSR1
mongod
killall -SIGUSR1
mongos # This line only applicable on swordfish
find /var/log/mongodb -type f
\( -iname "*.log.*" ! -iname "*.gz" \)
-exec gzip -f {} \;
|
This script runs every first day of
a month, this will remove all the compressed logs from last month.
|
#! /bin/sh
find /var/log/mongodb -type f
-name "*.gz" -exec rm
-f {} \;
|
We also need to add crontab for
these two shell commands.
|
crontab -e
0 0 * * *
/path/to/your/mongodb_logrotate.sh
0 10 1 * *
/path/to/your/mongodb_clearlog.sh
|
Deploy
MMS Agent
We are now using 10gen’s MMS as our
monitoring system. In order to use this, we need to let their agent running
on our mongos server.
Here is how we set it up.
First download the agent from your
hosts dashboard.
Then
|
# prereqs
sudo apt-get
install python python-setuptools
sudo easy_install
pip
sudo pip
install pymongo
#set up agent
cd /path/to/your/dir
mkdir mms-agent
unzip
name-of-agent.zip -d mms-agent
cd mms-agent
nohup python
agent.py > logs/agent.log 2>&1 &
|
And we finished!
The agent will auto discover other
servers in you cluster, although it still needs some manually work for you to
do in the dashboard, but it is really helpful for us to monitor the whole
system in real time.