使用MongoDB 3.2版本来构建副本集

使用3台服务器(mongo1、mongo2、mongo3)构建副本集
操作系统使用CentOS7
按照原始手册的参考进行实施。

操作系统环境准备

首先应用yum update。

# yum update -y

追加60GB的硬盘用于MongoDB数据,并创建逻辑卷管理(LVM)。使用xfs文件系统将数据挂载到/data/db路径下。

# pvcreate /dev/sdb
# vgcreate mongoVg /dev/sdb
# lvcreate -n mongoLv -l 100%FREE mongoVg
# mkfs.xfs /dev/mongoVg/mongoLv
# mkdir /data
# mkdir /data/db
# mount /dev/mapper/mongoVg-mongoLv /data/db
# df -h
ファイルシス                サイズ  使用  残り 使用% マウント位置
/dev/mapper/centos-root        18G  1.1G   17G    6% /
devtmpfs                      907M     0  907M    0% /dev
tmpfs                         917M     0  917M    0% /dev/shm
tmpfs                         917M  8.3M  909M    1% /run
tmpfs                         917M     0  917M    0% /sys/fs/cgroup
/dev/sda1                     494M  166M  328M   34% /boot
tmpfs                         184M     0  184M    0% /run/user/1000
/dev/mapper/mongoVg-mongoLv    60G   33M   60G    1% /data/db

现在,我们已经成功地在/data/db目录下创建了一个用作数据库数据存储区域的空间。此外,为了确保mongo1、mongo2和mongo3之间可以进行互相的主机名解析,我们会将它们添加到hosts文件中。

安装MongoDB 3.2

为了添加 MongoDB 仓库(请参考官方文档),创建 /etc/yum.repos.d/mongodb-org-3.2.repo。

[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1
# yum install -y mongodb-org-server-3.2.3 mongodb-org-shell-3.2.3 mongodb-org-tools-3.2.3

设定・启动

将mongod配置文件/etc/mongod.conf被创建。

# cat /etc/mongod.conf
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

由于需要对dbPath等内容进行多项修正,因此我使用yaml格式进行了新建。

systemLog:
    destination: file
    logAppend: true
    path: /var/log/mongodb/mongod.log
storage:
    dbPath: /data/db
    journal:
       enabled: true
    engine: "wiredTiger"
    wiredTiger:
        engineConfig:
            directoryForIndexes: true
            cacheSizeGB: 1
processManagement:
    fork: true
    pidFilePath: /var/run/mongodb/mongod.pid
net:
    port: 27017
replication:
    replSetName: "rs-db"

我会更改已创建的数据目录的所有者。

# chown -R mongod:mongod /data/

由于将设置文件更改为yml格式,因此请提前更改init脚本中CONFIGFILE的路径。

# CONFIGFILE="/etc/mongod.conf"
CONFIGFILE="/etc/mongod.yml"
# chkconfig mongod on
# /etc/rc.d/init.d/mongod start

我会在三台服务器上进行至此为止的工作。

复制品套装的构建

从这里开始,我们将在作为主要数据库的mongo1上执行。

使用mongoShell连接到数据库。

# mongo

切换到管理员数据库

> use admin
switched to db admin

复制套装的初始建立

> rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "mongo1:27017",
        "ok" : 1
}
rs-db:SECONDARY>
rs-db:PRIMARY>
rs-db:PRIMARY>

在mongod.yml中指定的副本集名称rs-db将显示为PRIMARY状态。

接下来,创建一个名为”dbadmin”的管理用户。

rs-db:PRIMARY> db.createUser({ user: "dbadmin", pwd: "********", roles: [{ role: "root", db: "admin" }]})
Successfully added user: {
        "user" : "dbadmin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}

只要创建一个用户,就无法进行未认证的操作,需要重新进入。

$ mongo localhost:27017/admin -u dbadmin -p
MongoDB shell version: 3.2.3
Enter password:
connecting to: localhost:27017/admin
Server has startup warnings:
2016-02-28T10:33:55.836+0900 I CONTROL  [initandlisten]
2016-02-28T10:33:55.836+0900 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-02-28T10:33:55.836+0900 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-02-28T10:33:55.836+0900 I CONTROL  [initandlisten]
2016-02-28T10:33:55.836+0900 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-02-28T10:33:55.836+0900 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-02-28T10:33:55.836+0900 I CONTROL  [initandlisten]
2016-02-28T10:33:55.836+0900 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.
2016-02-28T10:33:55.836+0900 I CONTROL  [initandlisten]
rs-db:PRIMARY>

尽管会有很多警告信息,但有关如何遏制这种情况,将在后文中详述。

rs-db:PRIMARY> show dbs
admin  0.000GB
local  0.000GB
rs-db:PRIMARY> db.system.users.find()
{ "_id" : "admin.dbadmin", "user" : "dbadmin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "W91BUAmHg+IQDBTHP3caJQ==", "storedKey" : "MeDkofLaL42A0QfQrGdBTNY6HB4=", "serverKey" : "gq9thZq/dC7EoHEuG/CmUV6w8/4=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }

有两个数据库,一个叫admin,另一个叫local。在admin数据库中创建了一个名为dbadmin的用户。

目前显示复制品套装的状态


rs-db:PRIMARY> rs.conf()
{
        "_id" : "rs-db",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "mongo1:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                }
        }
}

在这里,将mongo2和mongo3两个主机添加到副本集中。

rs-db:PRIMARY> rs.add( { host: "mongo2:27017"} )
{ "ok" : 1 }
rs-db:PRIMARY> rs.add( { host: "mongo3:27017"} )
{ "ok" : 1 }
rs-db:PRIMARY> rs.conf()
{
        "_id" : "rs-db",
        "version" : 3,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "mongo1:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "mongo2:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "mongo3:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                }
        }
}

显示复制设备的状态

rs-db:PRIMARY> rs.status()
{
        "set" : "rs-db",
        "date" : ISODate("2016-02-28T11:10:31.900Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongo1:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 122,
                        "optime" : {
                                "ts" : Timestamp(1456744206, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2016-02-28T11:10:06Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1456744148, 2),
                        "electionDate" : ISODate("2016-02-28T11:09:08Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "mongo2:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 29,
                        "optime" : {
                                "ts" : Timestamp(1456744206, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2016-02-28T11:10:06Z"),
                        "lastHeartbeat" : ISODate("2016-02-28T11:10:30.543Z"),
                        "lastHeartbeatRecv" : ISODate("2016-02-28T11:10:31.576Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "mongo1:27017",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "mongo3:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 25,
                        "optime" : {
                                "ts" : Timestamp(1456744206, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2016-02-28T11:10:06Z"),
                        "lastHeartbeat" : ISODate("2016-02-28T11:10:30.570Z"),
                        "lastHeartbeatRecv" : ISODate("2016-02-28T11:10:28.740Z"),
                        "pingMs" : NumberLong(0),
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}

经过以上步骤,mongo1、mongo2、mongo3这三台机器上的副本集rs-db已经建立完成。

補充:减少连接时的警告信息。

可以通过参考MongoDB 3.0安装后的警告消除方法来解决警告问题,方法与3.0版本相同。

最后

目前为止,与3.0版本相比没有任何变化。
接下来,我想要进行分片集群的构建。
关于分片,似乎有一些变化,比如配置服务器对副本集的支持等。

广告
将在 10 秒后关闭
bannerAds