MongoDB集群搭建及Sharding的实现思路(一)

2014-11-24 17:27:09 · 作者: · 浏览: 2

MongoDB集群搭建


MongoDB的复制集群类型:


·主从模式(master/slave)


·副本集模式(replica set)


副本及模式至少3个节点(一主二从),从节点负责复制主节点的oplog到本地并且应用到本地从而实现冗余。


(·arbiter:仅参与选举,但不持有任何数据


·0优先级:可以触发选举,但是不能被选举成为主节点


·可以使用repiset来定义集群名称)


------------------------------------------------------------------


相关阅读


------------------------------------------------------------------


1.编译安装MongDB


准备环境:


系统:Debian 7.2 64位


mongodb:mongodb-linux-x86_64-2.4.5.tgz(官网有下)


root@namenode1:/usr/local/tools# tar xfmongodb-linux-x86_64-2.4.5.tgz


root@namenode1:/usr/local/tools# mv mongodb-linux-x86_64-2.4.5 /usr/local/
root@namenode1:/usr/local/tools# cd /usr/local/
root@namenode1:/usr/local# ln -s /usr/local/mongodb-linux-x86_64-2.4.5//usr/local/mongodb


root@namenode1:/usr/local# ll | grep mongo
lrwxrwxrwx 1 root staff 38 3
月 17 15:35 mongodb ->/usr/local/mongodb-linux-x86_64-2.4.5/
drwxr-sr-x 3 root staff 4096 3月 17 15:08 mongodb-linux-x86_64-2.4.5


#创建用户


root@namenode1:~# groupadd -r mongod
root@namenode1:~# useradd -M -r -g mongod -d /data/db -s /bin/false -c mongodmongod


#建立目录


root@namenode1:~# mkdir -p /var/log/mongo/


root@namenode1:~# mkdir -p /mongo/data
root@namenode1:~# chown mongod /mongo/data /var/log/mongo/



root@namenode1:~# ll /var/log/ | grep mongo


drwxr-xr-x 2mongod root 4096 Mar 17 15:25mongo
root@namenode1:~# ll /mongo/
total 4
drwxr-xr-x 2 mongod root 4096 Mar 17 15:27 data


#LC_ALL="C"加入环境变量,以防启动出错


root@namenode1:~# echo ‘export LC_ALL="C"’ >> /etc/profile


#创建配置文件(将之前rpm包安装mongodb后的配置文件拷贝过来即可)


root@namenode1:~# cat /etc/mongod.conf
# mongo.conf

#where to log
logpath=/var/log/mongo/mongod.log

logappend=true

# fork and run in background
fork = true

#port = 27017

dbpath=/mongo/data

# location of pidfile
pidfilepath = /var/run/mongodb/mongod.pid

# Disables write-ahead journaling
# nojournal = true

# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

# Turn on/off security. Off is currently the default
#noauth = true
#auth = true

# Verbose logging output.
#verbose = true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true

# Enable db quota management
#quota = true

# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#diaglog = 0

# Ignore query hints
#nohints = true

# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = true

# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting = true

# Turns off table scans. Any query that would do a table scan fails.
#notablescan = true

# Disable data file preallocation.
#noprealloc = true

# Specify .ns file size for new databases.
# nssize =

# Accout token for Mongo monitoring server.
#mms-token =

# Server name for Mongo monitoring server.
#mms-name =

# Ping interval for Mongo monitoring server.
#mms-interval =

# Replication Options

# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com


启动mongodb


export LC_ALL="C"


root@namenode2:/usr/local/mongodb/bin# mongod -f /etc/mongod.conf



2.安装Rockmongo


RockMongo 是一个PHP5写的MongoDB管理工具。主要特征:使用宽松的New BSD License协议,速度快,安装简单,与mysql的phpmyadmin相似


root@namenode1:~# apt-get install apache2 php5 php5-dev php5-cli


root@namenode1:/etc/apache2# cd /var/www/
root@namenode