前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mongodb分片

mongodb分片

作者头像
零月
发布2018-04-25 16:15:38
1.5K0
发布2018-04-25 16:15:38
举报
文章被收录于专栏:从零开始的linux
分别在三台机器上面创建
代码语言:javascript
复制
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台的配置文件
代码语言:javascript
复制
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
代码语言:javascript
复制
[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端口初始化副本集
代码语言:javascript
复制
[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
三台机器分片配置
代码语言:javascript
复制
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

启动分片配置

代码语言:javascript
复制
三台机器上都要操作
[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

代码语言:javascript
复制
[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

代码语言:javascript
复制
[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

代码语言:javascript
复制
[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台
代码语言:javascript
复制
[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:
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-02-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始的linux 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 分别在三台机器上面创建
  • 配置3台的配置文件
  • 启动三台机器的config server
  • 登陆任意一台机器的21000端口初始化副本集
  • 三台机器分片配置
  • 启动分片配置
    • 因为仲裁节点不能登陆所以只能在db1和db2上
      • 因为仲裁节点不能登陆所以只能在db2和db3上
        • 因为仲裁节点不能登陆所以只能在db1和db3上
          • 配置路由服务器3台
          相关产品与服务
          数据库
          云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档