mongodb分片

分别在三台机器上面创建

mkdir -pv /data/mongodb/mongos/log
mkdir -pv /data/mongodb/config/{data,log}
mkdir -pv /data/mongodb/shard1/{data,log}
mkdir -pv /data/mongodb/shard2/{data,log}
mkdir -pv /data/mongodb/shard3/{data,log}

配置3台的配置文件

mkdir /var/run/mongodb
mkdir /etc/mongod
vim /etc/mongod/config.conf
pidfilepath = /var/run/mongodb/configsrv.pid
dbpath = /data/mongodb/config/data
logpath = /data/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
configsvr = true
replSet = configs
maxConns = 20000

启动三台机器的config server

[root@db1 opt]# mongod -f /etc/mongod/config.conf 
[root@db1 opt]# ps aux | grep mongod
root      11361  0.8  4.4 1051304 44364 ?       Sl   18:52   0:00 mongod -f /etc/mongod/config.conf
[root@db1 opt]# netstat -nltp | grep mongod
tcp        0      0 0.0.0.0:21000           0.0.0.0:*               LISTEN      11361/mongod

登陆任意一台机器的21000端口初始化副本集

[root@db1 opt]# mongo --port 21000
> config = {_id:"configs",members:[{_id : 0, host : "128.0.0.15:21000"},{_id : 1, host : "128.0.0.16:21000"},{_id : 2, host : "128.0.0.17:21000"}] }
{
    "_id" : "configs",
    "members" : [
        {
            "_id" : 0,
            "host" : "128.0.0.15:21000"
        },
        {
            "_id" : 1,
            "host" : "128.0.0.16:21000"
        },
        {
            "_id" : 2,
            "host" : "128.0.0.17:21000"
        }
    ]
}
> rs.initiate(config)
{ "ok" : 1 }
configs:OTHER> rs.status()
{
    "set" : "configs",
    "date" : ISODate("2018-02-05T11:08:23.945Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "configsvr" : true,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1517828895, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1517828895, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1517828895, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1517828895, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "128.0.0.15:21000",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 938,
            "optime" : {
                "ts" : Timestamp(1517828895, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-02-05T11:08:15Z"),
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1517828841, 1),
            "electionDate" : ISODate("2018-02-05T11:07:21Z"),
            "configVersion" : 1,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "128.0.0.16:21000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 72,
            "optime" : {
                "ts" : Timestamp(1517828895, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1517828895, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-02-05T11:08:15Z"),
            "optimeDurableDate" : ISODate("2018-02-05T11:08:15Z"),
            "lastHeartbeat" : ISODate("2018-02-05T11:08:23.635Z"),
            "lastHeartbeatRecv" : ISODate("2018-02-05T11:08:22.307Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "128.0.0.15:21000",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "128.0.0.17:21000",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 72,
            "optime" : {
                "ts" : Timestamp(1517828895, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1517828895, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-02-05T11:08:15Z"),
            "optimeDurableDate" : ISODate("2018-02-05T11:08:15Z"),
            "lastHeartbeat" : ISODate("2018-02-05T11:08:23.674Z"),
            "lastHeartbeatRecv" : ISODate("2018-02-05T11:08:23.306Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "128.0.0.15:21000",
            "configVersion" : 1
        }
    ],
    "ok" : 1
}
configs:PRIMARY> exit
bye

三台机器分片配置

pidfilepath = /var/run/mongodb/shard1.pid
dbpath = /data/mongodb/shard1/data
logpath = /data/mongodb/shard1/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
httpinterface = true
rest = true
replSet = shard1
shardsvr = true
maxConns = 20000

[root@db1 opt]# sed -i "s/shard1/shard2/g" /etc/mongod/shard3.conf
[root@db1 opt]# cat !$
cat /etc/mongod/shard2.conf
pidfilepath = /var/run/mongodb/shard2.pid
dbpath = /data/mongodb/shard2/data
logpath = /data/mongodb/shard2/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
httpinterface = true
rest = true
replSet = shard2
shardsvr = true
maxConns = 20000

[root@db1 opt]# sed -i "s/shard1/shard3/g" /etc/mongod/shard3.conf
[root@db1 opt]# cat !$
cat /etc/mongod/shard3.conf
pidfilepath = /var/run/mongodb/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
httpinterface = true
rest = true
replSet = shard3
shardsvr = true
maxConns = 20000
[root@db1 opt]# cd /etc/mongod/
[root@db1 mongod]# ls
config.conf  shard1.conf  shard2.conf  shard3.conf

[root@db1 mongod]# scp shard2.conf shard3.conf bd2:/etc/mongod/
^C[root@db1 mongod]# scp shard2.conf shard3.conf db2:/etc/mongod/
root@db2's password: 
shard2.conf                                          100%  269   296.5KB/s   00:00    
shard3.conf                                          100%  269   310.3KB/s   00:00    
[root@db1 mongod]# scp shard2.conf shard3.conf db3:/etc/mongod/
root@db3's password: 
shard2.conf                                          100%  269   285.9KB/s   00:00    
shard3.conf                                          100%  269   282.0KB/s   00:00

启动分片配置

三台机器上都要操作
[root@db1 mongod]# mongod -f /etc/mongod/shard1.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 11504
child process started successfully, parent exiting
[root@db2 opt]# mongod -f /etc/mongod/shard1.conf
[root@db3 opt]# mongod -f /etc/mongod/shard1.conf

因为仲裁节点不能登陆所以只能在db1和db2上

启动shard1

[root@db1 mongod]# mongo --port 27001
> use admin
switched to db admin
> config = {_id:"shard1",members:[{_id : 0, host : "128.0.0.15:27001"},{_id : 1, host : "128.0.0.16:27001"},{_id : 2, host : "128.0.0.17:27001",arbiterOnly:true}] }
{
    "_id" : "shard1",
    "members" : [
        {
            "_id" : 0,
            "host" : "128.0.0.15:27001"
        },
        {
            "_id" : 1,
            "host" : "128.0.0.16:27001"
        },
        {
            "_id" : 2,
            "host" : "128.0.0.17:27001",
            "arbiterOnly" : true
        }
    ]
}
> rs.initiate(config)
{ "ok" : 1 }
shard1:PRIMARY> rs.status()
{
    "set" : "shard1",
    "date" : ISODate("2018-02-05T11:53:10.723Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1517831581, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1517831581, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1517831581, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "128.0.0.15:27001",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 967,
            "optime" : {
                "ts" : Timestamp(1517831581, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-02-05T11:53:01Z"),
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1517831580, 1),
            "electionDate" : ISODate("2018-02-05T11:53:00Z"),
            "configVersion" : 1,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "128.0.0.16:27001",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 21,
            "optime" : {
                "ts" : Timestamp(1517831581, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1517831581, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-02-05T11:53:01Z"),
            "optimeDurableDate" : ISODate("2018-02-05T11:53:01Z"),
            "lastHeartbeat" : ISODate("2018-02-05T11:53:10.139Z"),
            "lastHeartbeatRecv" : ISODate("2018-02-05T11:53:10.561Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "128.0.0.15:27001",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "128.0.0.17:27001",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 21,
            "lastHeartbeat" : ISODate("2018-02-05T11:53:10.164Z"),
            "lastHeartbeatRecv" : ISODate("2018-02-05T11:53:06.475Z"),
            "pingMs" : NumberLong(0),
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

因为仲裁节点不能登陆所以只能在db2和db3上

启动shard2

[root@db1 mongod]# mongod -f /etc/mongod/shard2.conf
[root@db2 mongod]# mongod -f /etc/mongod/shard2.conf
[root@db3 mongod]# mongod -f /etc/mongod/shard2.conf
[root@db2 mongod]# mongo --port 27002
> use admin
switched to db admin
> config = {_id:"shard2",members:[{_id : 0, host : "128.0.0.15:27002",arbiterOnly:true},{_id : 1, host : "128.0.0.16:27002"},{_id : 2, host : "128.0.0.17:27002"}] }
{
    "_id" : "shard2",
    "members" : [
        {
            "_id" : 0,
            "host" : "128.0.0.15:27002",
            "arbiterOnly" : true
        },
        {
            "_id" : 1,
            "host" : "128.0.0.16:27002"
        },
        {
            "_id" : 2,
            "host" : "128.0.0.17:27002"
        }
    ]
}
> rs.initiate(config)
{ "ok" : 1 }
shard2:OTHER> rs.status()
{
    "set" : "shard2",
    "date" : ISODate("2018-02-05T12:35:34.080Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1517834128, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1517834128, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1517834128, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "128.0.0.15:27002",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 37,
            "lastHeartbeat" : ISODate("2018-02-05T12:35:33.244Z"),
            "lastHeartbeatRecv" : ISODate("2018-02-05T12:35:33.960Z"),
            "pingMs" : NumberLong(0),
            "configVersion" : 1
        },
        {
            "_id" : 1,
            "name" : "128.0.0.16:27002",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 2049,
            "optime" : {
                "ts" : Timestamp(1517834128, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-02-05T12:35:28Z"),
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1517834107, 1),
            "electionDate" : ISODate("2018-02-05T12:35:07Z"),
            "configVersion" : 1,
            "self" : true
        },
        {
            "_id" : 2,
            "name" : "128.0.0.17:27002",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 37,
            "optime" : {
                "ts" : Timestamp(1517834128, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1517834128, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-02-05T12:35:28Z"),
            "optimeDurableDate" : ISODate("2018-02-05T12:35:28Z"),
            "lastHeartbeat" : ISODate("2018-02-05T12:35:33.244Z"),
            "lastHeartbeatRecv" : ISODate("2018-02-05T12:35:34.054Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "128.0.0.16:27002",
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

因为仲裁节点不能登陆所以只能在db1和db3上

启动shard3

[root@db1 mongod]# mongod -f /etc/mongod/shard3.conf
[root@db2mongod]# mongod -f /etc/mongod/shard3.conf
[root@db3mongod]# mongod -f /etc/mongod/shard3.conf
[root@db3 opt]# mongo --port 27003
> use admin
switched to db admin
> config = {_id:"shard3",members:[{_id : 0, host : "128.0.0.15:27003"},{_id : 1, host : "128.0.0.16:27003",arbiterOnly:true},{_id : 2, host : "128.0.0.17:27003"}] }
{
    "_id" : "shard3",
    "members" : [
        {
            "_id" : 0,
            "host" : "128.0.0.15:27003"
        },
        {
            "_id" : 1,
            "host" : "128.0.0.16:27003",
            "arbiterOnly" : true
        },
        {
            "_id" : 2,
            "host" : "128.0.0.17:27003"
        }
    ]
}
> rs.initiate(config)
{ "ok" : 1 }
shard3:OTHER> rs.status()
{
    "set" : "shard3",
    "date" : ISODate("2018-02-05T12:42:56.776Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1517834568, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1517834568, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1517834568, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "128.0.0.15:27003",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 21,
            "optime" : {
                "ts" : Timestamp(1517834568, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1517834568, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-02-05T12:42:48Z"),
            "optimeDurableDate" : ISODate("2018-02-05T12:42:48Z"),
            "lastHeartbeat" : ISODate("2018-02-05T12:42:56.392Z"),
            "lastHeartbeatRecv" : ISODate("2018-02-05T12:42:53.041Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "128.0.0.17:27003",
            "configVersion" : 1
        },
        {
            "_id" : 1,
            "name" : "128.0.0.16:27003",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 21,
            "lastHeartbeat" : ISODate("2018-02-05T12:42:56.392Z"),
            "lastHeartbeatRecv" : ISODate("2018-02-05T12:42:51.999Z"),
            "pingMs" : NumberLong(0),
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "128.0.0.17:27003",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 316,
            "optime" : {
                "ts" : Timestamp(1517834568, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-02-05T12:42:48Z"),
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1517834566, 1),
            "electionDate" : ISODate("2018-02-05T12:42:46Z"),
            "configVersion" : 1,
            "self" : true
        }
    ],
    "ok" : 1
}

配置路由服务器3台

[root@db1 mongod]# vim /etc/mongod/mongos.conf
pidfilepath = /var/run/mongodb/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
configdb = configs/128.0.0.15:21000,128.0.0.16:21000,128.0.0.17:21000
maxConns = 20000
[root@db1 mongod]# mongos -f /etc/mongod/mongos.conf 
[root@db2 mongod]# mongos -f /etc/mongod/mongos.conf 
[root@db3 mongod]# mongos -f /etc/mongod/mongos.conf 
#把所有的分片和路由器串联
[root@db1 mongod]# mongo --port 20000
mongos> mongos> sh.addShard("shard1/128.0.0.15:27001,128.0.0.16:27001,128.0.0.17:27001")
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> sh.addShard("shard2/128.0.0.15:27002,128.0.0.16:27002,128.0.0.17:27002")
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> sh.addShard("shard3/128.0.0.15:27003,128.0.0.16:27003,128.0.0.17:27003")
{ "shardAdded" : "shard3", "ok" : 1 }
#查看集群
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5a783aebf1f1012ae074d793")
}
  shards:
    {  "_id" : "shard1",  "host" : "shard1/128.0.0.15:27001,128.0.0.16:27001",  "state" : 1 }
    {  "_id" : "shard2",  "host" : "shard2/128.0.0.16:27002,128.0.0.17:27002",  "state" : 1 }
    {  "_id" : "shard3",  "host" : "shard3/128.0.0.15:27003,128.0.0.17:27003",  "state" : 1 }
  active mongoses:
    "3.4.9" : 3
 autosplit:
    Currently enabled: yes
  balancer:
    Currently enabled:  yes
    Currently running:  no
        Balancer lock taken at Mon Feb 05 2018 19:07:24 GMT+0800 (CST) by ConfigServer:Balancer
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours: 
        No recent migrations
  databases:

本文分享自微信公众号 - 从零开始的linux(gh_4a9d788f5f27),作者:零月

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2018-02-07

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 安装mysql5.7

    安装mysql5.7 wget http://dev.mysql.com/get/mysql57-community-release-el6-9.noarch....

    零月
  • 安装mongodb

    配置mongodb的官方源 vim /etc/yum.repos.d/mongodb-org-3.0.repo 加入: [mongodb-org-3.0] n...

    零月
  • 安装haproxy

    安装haproxy ##下载包 wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.8.tar.g...

    零月
  • - `Yoga (= 0.44.0.React)` required by `React/Core (0.44.0)`

    [!] Unable to satisfy the following requirements:

    ZY_FlyWay
  • 基于 Python 的僵尸网络将 Linux 机器变成挖矿机器人

    F5 Networks 的安全研究人员发现了一个新的 Linux 加密僵尸网络,并将其命名为"PyCryptoMiner",它主要的攻击目标是具有公开 SSH...

    Python中文社区
  • poj-2909-哥德巴赫猜想

    For any even number n greater than or equal to 4, there exists at least one pair...

    瑾诺学长
  • SC和Storj——分布式存储技术哪家强?

    一句话概括项目 SC和Storj都是做基于区块链的去中心化存储。 SC和Storj具体运作方式 SC和Storj除算法略有不同外,其运作方式基本相同。存储数据的...

    企鹅号小编
  • Go语言学习之旅 4 - Go 的并发原语

    会启动一个新的 Go 程并执行这个函数。 比如: go say("world") // 将启动一个线程来执行 say 函数

    zhangyunfeiVir
  • Type Coercion in Javascript

    Question: What exactly is type coercion in Javascript? For example, on the us...

    lesM10
  • 原创译文 | 英伟达的詹森•黄谈白宫的人工智能倡议

    不是那种你希望在人工智能技术上加速的人。但美国科技公司上周到白宫朝圣,告诉他这很重要。《AI政策峰会》呼吁工业、学术界和政府合作,在人工智能领域与其他国家展开竞...

    灯塔大数据

扫码关注云+社区

领取腾讯云代金券