CentOS 6.5环境 MongoDB 3.2.8 三节点副本集搭建

本文档描述在两台机器上部署MongoDB 三个节点副本集。其中A服务器上部署两个节点实例,B服务器上部署1个节点实例。

系统环境:CentOS release 6.5 (Final)
安装软件:mongodb-linux-x86_64-rhel62-3.2.8.tgz
下载地址:http://www.mongodb.org/downloads
安装机器:192.168.1.21和192.168.1.23
软件上传位置:/root
软件安装位置:/home/mongodb/mongodb/bin
数据存放位置:/home/mongodb/data
日志存放位置:/home/mongodb/logs
Keyfile存放位置:/home/mongodb/keyfile
参数文件存放位置:/home/mongodb/config

检查是否安装过mongodb
rpm -qa | grep mongodb
service mongodb status

将27001、27002、27003端口加入防火墙
# vi /etc/sysconfig/iptables
-A INPUT -p tcp --dport 27001 -j ACCEPT(A服务器上)
-A INPUT -p tcp --dport 27002 -j ACCEPT(A服务器上)
-A INPUT -p tcp --dport 27003 -j ACCEPT(B服务器上)
备注:加在COMMIT之前
重启防火墙
# service iptables restart

大内存页面redhat_transparent_hugepage参数设置为never

先查看该参数值

# cat /sys/kernel/mm/redhat_transparent_hugepage/enabled
[always] madvise never
# cat /sys/kernel/mm/redhat_transparent_hugepage/defrag
[always] madvise never

配置disable-transparent-hugepages服务

vi /etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides: disable-transparent-hugepages
# Required-Start: $local_fs
# Required-Stop:
# X-Start-Before: mongod mongodb-mms-automation-agent
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description: Disable Linux transparent huge pages, to improve
# database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    re='^[0-1]+$'
    if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
    then
      # RHEL 7
      echo 0 > ${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > ${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac

对该服务器授权,并设置 开机自启动
sudo chmod 755 /etc/init.d/disable-transparent-hugepages
sudo chkconfig --add disable-transparent-hugepages


重启服务器,并检查该参数是否已经生效

# cat /sys/kernel/mm/redhat_transparent_hugepage/enabled
always madvise [never]
# cat /sys/kernel/mm/redhat_transparent_hugepage/defrag
always madvise [never]

官方链接:

Disable Transparent Huge Pages (THP)【https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/】


创建mongodb用户
# groupadd mongodb
# useradd mongodb -g mongodb


上传安装包,并创建相应的目录

cd /root
mv mongodb-linux-x86_64-rhel62-3.2.8.tgz /home/mongodb
su - mongodb
tar -xvf mongodb-linux-x86_64-rhel62-3.2.8.tgz
mv mongodb-linux-x86_64-rhel62-3.2.8 mongodb
mkdir -p /home/mongodb/data
mkdir -p /home/mongodb/data/replSet1(A服务器上)
mkdir -p /home/mongodb/data/replSet2(A服务器上)
mkdir -p /home/mongodb/data/replSet3(B服务器上)
mkdir -p /home/mongodb/config
mkdir -p /home/mongodb/logs
mkdir -p /home/mongodb/keyfile


配置三个节点的config文件

vi /home/mongodb/config/replSet1.conf(A服务器上)

dbpath=/home/mongodb/data/replSet1
replSet=Shard1
bind_ip=192.168.1.21,localhost
port=27001
oplogSize=5000
logpath=/home/mongodb/logs/replSet1.log
logappend=true
fork=true
# keyFile=/home/mongo/keyfile/security
wiredTigerCacheSizeGB=1

vi /home/mongodb/config/replSet2.conf(A服务器上)

dbpath=/home/mongodb/data/replSet2
replSet=Shard1
bind_ip=192.168.1.21,localhost
port=27002
oplogSize=5000
logpath=/home/mongodb/logs/replSet2.log
logappend=true
fork=true
# keyFile=/home/mongo/keyfile/security
wiredTigerCacheSizeGB=1


vi /home/mongodb/config/replSet3.conf(B服务器上)

dbpath=/home/mongodb/data/replSet3
replSet=Shard1
bind_ip=192.168.1.23,localhost
port=27003
oplogSize=5000
logpath=/home/mongodb/logs/replSet3.log
logappend=true
fork=true
# keyFile=/home/mongo/keyfile/security
wiredTigerCacheSizeGB=1


分别启动三个实例进程

/home/mongodb/mongodb/bin/mongod -f /home/mongodb/config/replSet1.conf
/home/mongodb/mongodb/bin/mongod -f /home/mongodb/config/replSet2.conf
/home/mongodb/mongodb/bin/mongod -f /home/mongodb/config/replSet3.conf


通过27001端口登录其中一个节点,配置并初始化副本集。

# cp /home/mongodb/mongodb/bin/mongo /usr/sbin/
$ mongo --port 27001
MongoDB shell version: 3.2.8
connecting to: 127.0.0.1:27001/test
> config={_id:'Shard1',members:[{_id:0,host:'192.168.1.21:27001'}]}
{
        "_id" : "Shard1",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.1.21:27001"
                }
        ]
}
> rs.initiate(config)
{ "ok" : 1 }
Shard1:SECONDARY>
Shard1:PRIMARY>
Shard1:PRIMARY> rs.add("192.168.1.21:27002")
Shard1:PRIMARY> rs.add("192.168.1.21:27003")
Shard1:PRIMARY> rs.status()
{
        "set" : "Shard1",
        "date" : ISODate("2016-11-10T15:11:13.857Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.1.21:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1704,
                        "optime" : {
                                "ts" : Timestamp(1478790664, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2016-11-10T15:11:04Z"),
                        "electionTime" : Timestamp(1478789349, 2),
                        "electionDate" : ISODate("2016-11-10T14:49:09Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "192.168.1.21:27002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1238,
                        "optime" : {
                                "ts" : Timestamp(1478790664, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2016-11-10T15:11:04Z"),
                        "lastHeartbeat" : ISODate("2016-11-10T15:11:12.744Z"),
                        "lastHeartbeatRecv" : ISODate("2016-11-10T15:11:13.744Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "192.168.1.21:27001",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "192.168.1.23:27003",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 9,
                        "optime" : {
                                "ts" : Timestamp(1478790664, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2016-11-10T15:11:04Z"),
                        "lastHeartbeat" : ISODate("2016-11-10T15:11:12.744Z"),
                        "lastHeartbeatRecv" : ISODate("2016-11-10T15:11:13.086Z"),
                        "pingMs" : NumberLong(0),
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}

设置三个节点的权重

Shard1:PRIMARY> cfg = rs.conf()
Shard1:PRIMARY> cfg.members[0].priority = 2
Shard1:PRIMARY> cfg.members[1].priority = 2
Shard1:PRIMARY> cfg.members[2].priority = 1
Shard1:PRIMARY> rs.reconfig(cfg)
说明:cfg.members[0].priority =括号中的数字是执行rs.conf()得出的节点顺序,第一个节点在这里写0,第二个节点写1,依次类推。

创建Keyfile,并分别复制到各节点的指定目录(创建一次,然后复制到其他节点)
在节点1上创建验证文件security于/app/mongodb/mmapv1/key目录,关赋予可读权限,命令如下
# cd /home/mongodb/keyfile
# openssl rand -base64 741 > security
# chmod 600 security


登录Primary节点,并创建管理员(可以自己创建其他账号及角色)
Shard1:PRIMARY> use admin
switched to db admin
Shard1:PRIMARY> db.createUser(
 {
 user:"admin",
 pwd:"Password",
 roles:[{role:"userAdminAnyDatabase",db:"admin"}]
 }
 )
 
db.grantRolesToUser("admin",[{role:"clusterManager",db:"admin"}])
db.grantRolesToUser("admin",[{role:"clusterAdmin",db:"admin"}])
db.grantRolesToUser("admin",[{role:"clusterMonitor",db:"admin"}])
db.grantRolesToUser("admin",[{role:"hostManager",db:"admin"}])
db.grantRolesToUser("admin",[{role:"userAdminAnyDatabase",db:"admin"}])
db.grantRolesToUser("admin",[{role:"readWrite",db:"admin"}])
db.grantRolesToUser("admin",[{role:"backup",db:"admin"}])
db.grantRolesToUser("admin",[{role:"restore",db:"admin"}])

重新修改config文件,将以下这个参数配置注释去掉,使其生效。

keyFile=/home/mongo/keyfile/security


关闭所有进程,并重新启动进程。然后通过命令进行登录验证。

mongo 192.168.1.21:27001/admin -u admin -p Password

数据导出导入命令

mongodump -h 192.168.1.91  --port 30000 -u admin -p Password -o /data --db db_name --authenticationDatabase admin
mongorestore -h 192.168.1.21 --port 27001 -u admin -p Password --db db_name -c INBOUND_MSG --authenticationDatabase admin /data/db_name/INBOUND_MSG.bson 

文章到此完毕!


上一篇:HTML5学习笔记三 HTML元素、属性、标题、段落简介


下一篇:Oracle 10.2.0.1.0数据库启动报错 ORA-00600 [keltnfy-ldminit]