centos7部署mongodb分片集群4.0.11

  sre

sPuBu9.jpg

先配hosts

172.16.0.13 shard01
172.16.0.14 shard02
172.16.0.15 shard03

关闭防火墙

service firewalld stop
systemctl disable firewalld
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

下载mongo

cd /opt
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.11.tgz
tar zxvf mongodb-linux-x86_64-rhel70-4.0.11.tgz
mv mongodb-linux-x86_64-rhel70-4.0.11 mongodb
mkdir -p /opt//mongodb/conf
mkdir -p /opt//mongodb/mongos/log
mkdir -p /opt//mongodb/config/{data,log}
mkdir -p /opt//mongodb/shard1/{data,log}
mkdir -p /opt//mongodb/shard2/{data,log}
mkdir -p /opt//mongodb/shard3/{data,log}

增加环境变量

echo "export PATH=/opt/mongodb/bin:$PATH">> ~/.bashrc
source ~/.bashrc

在三个节点上配置config服务

echo """
pidfilepath = /opt/mongodb/config/log/configsrv.pid
dbpath = /opt/mongodb/config/data
logpath = /opt/mongodb/config/log/congigsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 21000
fork = true
configsvr = true
#副本集名称
replSet=testdb
#设置最大连接数
maxConns=20000
""" >> /opt/mongodb/conf/config.conf

启动三台服务器的config server

/opt/mongodb/bin/mongod -f /opt/mongodb/conf/config.conf

登录任意一台配置服务器,初始化配置副本集

/opt/mongodb/bin/mongo --port 21000
#配置config变量,"_id" : "configs"应与配置文件中配置的replSet=configs一致
#"members"中的"host"为三个节点的ip和port
config = {
    _id : "testdb",
     members : [
         {_id : 0, host : "shard01:21000" },
         {_id : 1, host : "shard02:21000" },
         {_id : 2, host : "shard03:21000" }
     ]
 }
#若报错则检查防火墙配置

初始化副本集

rs.initiate(config)

配置成功

{
    "ok" : 1,
    "operationTime" : Timestamp(1565153403, 1),
    "gleStats" : {
        "lastOpTime" : Timestamp(1565153403, 1),
        "electionId" : ObjectId("000000000000000000000000")
    },
    "lastCommittedOpTime" : Timestamp(0, 0),
    "clusterTime" : {
        "clusterTime" : Timestamp(1565153403, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

设置第一个分片副本集(三个节点都执行)

echo """
pidfilepath = /opt/mongodb/shard1/log/shard1.pid
dbpath = /opt/mongodb/shard1/data
logpath = /opt/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
#副本集名称
replSet=shard1
shardsvr = true
#设置最大连接数
maxConns=20000
""" >> vi /opt/mongodb/conf/shard1.conf

启动三台服务器的shard1 server

/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard1.conf

在任意一台服务器的mongo(非仲裁节点服务器),初始化副本集:

/opt/mongodb/bin/mongo --port 27001

定义副本集配置

#"arbiterOnly":true 代表其为仲裁节点。
config = {
   _id : "shard1",
    members : [
        {_id : 0, host : "shard01:27001" },
        {_id : 1, host : "shard02:27001" },
        {_id : 2, host : "shard03:27001" , arbiterOnly: true }
    ]
}

初始化副本集配置:

rs.initiate(config);

设置第二个分片副本集(三个节点)

echo """
pidfilepath = /opt/mongodb/shard2/log/shard2.pid
dbpath = /opt/mongodb/shard2/data
logpath = /opt/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
#副本集名称
replSet=shard2
shardsvr = true
#设置最大连接数
maxConns=20000
""" >> /opt/mongodb/conf/shard2.conf

启动三台服务器的shard2 server:

/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard2.conf

任意一台服务器的mongo(非仲裁节点服务器),初始化副本集

/opt/mongodb/bin/mongo --port 27002
#定义副本集配置
config = {
    _id : "shard2",
     members : [
         {_id : 0, host : "shard01:27002"  , arbiterOnly: true },
         {_id : 1, host : "shard02:27002" },
         {_id : 2, host : "shard03:27002" }
     ]
 }

初始化副本集配置:

rs.initiate(config);

设置第三个分片副本集(三个节点)

echo """
pidfilepath = /opt/mongodb/shard3/log/shard3.pid
dbpath = /opt/mongodb/shard3/data
logpath = /opt/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
#副本集名称
replSet=shard3
shardsvr = true
#设置最大连接数
maxConns=20000
""" >> /opt/mongodb/conf/shard3.conf

启动三台服务器的shard3 server:

/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard3.conf

任意一台服务器的mongo(非仲裁节点服务器),初始化副本集:

/opt/mongodb/bin/mongo --port 27003
#定义副本集配置
config = {
    _id : "shard3",
     members : [
         {_id : 0, host : "shard01:27003" },
         {_id : 1, host : "shard02:27003" , arbiterOnly: true},
         {_id : 2, host : "shard03:27003" }
     ]
 }

初始化副本集配置:

rs.initiate(config);

配置路由服务器 mongos(所有节点)

echo """
pidfilepath = /opt/mongodb/mongos/log/mongos.pid
logpath = /opt/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 27017
fork = true
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
configdb = testdb/shard01:21000,shard02:21000,shard03:21000
#设置最大连接数
maxConns=20000
""" >> /opt/mongodb/conf/mongos.conf

启动三台服务器的mongos server

/opt/mongodb/bin/mongos -f /opt/mongodb/conf/mongos.conf

启用分片

在任意一台mongos

/opt/mongodb/bin/mongo --port 27017

串联路由服务器与分shashard01

sh.addShard("shard1/shard01:27001,shard02:27001,shard03:27001")
sh.addShard("shard2/shard01:27002,shard02:27002,shard03:27002")
sh.addShard("shard3/shard01:27003,shard02:27003,shard03:27003")

查看集群状态

sh.status()

可看到如下所示信息:

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5d4a5888ca9e40c0fbf9888c")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/shard01:27001,shard02:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/shard02:27002,shard03:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/shard01:27003,shard03:27003",  "state" : 1 }
  active mongoses:
        "4.0.11" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }

登录:

/opt/mongodb/bin/mongo shard01:27017

指定testdb分片生效

use admin
db.runCommand( { enablesharding :"testdb"});

后可看到如下提示:

mongos> use admin
switched to db admin
mongos> db.runCommand( { enablesharding :"testdb"});
{
    "ok" : 1,
    "operationTime" : Timestamp(1565156424, 5),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1565156424, 5),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

指定数据库里需要分片的集合和片键:

db.runCommand( { shardcollection : "testdb.t1",key : {id: "hashed"} } )
db.runCommand( { shardcollection : "testdb.t2",key : {id: "hashed"} } )

启停:先启动所有节点配置服务器,在启动所有节点的分片,最后启动所有节点的mongos

/opt/mongodb/bin/mongod -f /opt/mongodb/conf/config.conf
/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard1.conf
/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard2.conf
/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard3.conf
/opt/mongodb/bin/mongos -f /opt/mongodb/conf/mongos.conf

服务化

cd /etc/systemd/system
echo """
[Unit]
Description=mongo-config

[Service]
user=root
group=root
Type=forking
ExecStart=/opt/mongodb/bin/mongod -f /opt/mongodb/conf/config.conf
TimeoutStopSec=0
Restart=always
RestartSec=15s
TimeoutStartSec=30s
#ExecStop=/opt/mongodb/bin/mongod -f /opt/mongodb/conf/config.conf --shutdown

[Install]
WantedBy=multi-user.target
Alias=mongoc
""" >> mongo-config.service
echo """
[Unit]
Description=mongo-shard1
Requires=mongo-config.service
After=mongo-config.service

[Service]
user=root
group=root
Type=forking
ExecStart=/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard1.conf
TimeoutStopSec=0
Restart=always
RestartSec=15s
TimeoutStartSec=30s
#ExecStop=/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard1.conf --shutdown

[Install]
WantedBy=multi-user.target
Alias=mongosh1
""" >> mongo-shard1.service
echo """
[Unit]
Description=mongo-shard2
Requires=mongo-shard1.service
After=mongo-shard1.service

[Service]
user=root
group=root
Type=forking
ExecStart=/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard2.conf
TimeoutStopSec=0
Restart=always
RestartSec=15s
TimeoutStartSec=30s
#ExecStop=/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard2.conf --shutdown

[Install]
WantedBy=multi-user.target
Alias=mongosh2
""" >> mongo-shard2.service
echo """
[Unit]
Description=mongo-shard3
Requires=mongo-shard2.service
After=mongo-shard2.service

[Service]
user=root
group=root
Type=forking
ExecStart=/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard3.conf
TimeoutStopSec=0
Restart=always
RestartSec=15s
TimeoutStartSec=30s
#ExecStop=/opt/mongodb/bin/mongod -f /opt/mongodb/conf/shard3.conf --shutdown

[Install]
WantedBy=multi-user.target
Alias=mongosh3
""" >> mongo-shard3.service
echo """
[Unit]
Description=mongos
Requires=mongo-shard3.service
After=mongo-shard3.service

[Service]
user=root
group=root
Type=forking
ExecStart=/opt/mongodb/bin/mongos -f /opt/mongodb/conf/mongos.conf
TimeoutStopSec=0
Restart=always
RestartSec=15s
TimeoutStartSec=30s
#ExecStop=/opt/mongodb/bin/mongos -f /opt/mongodb/conf/mongos.conf

[Install]
WantedBy=multi-user.target
""" >> mongos.service

开机自启

chmod +x mongo*.service
systemctl daemon-reload
systemctl enable mongo-config.service
systemctl enable mongo-shard1.service
systemctl enable mongo-shard2.service
systemctl enable mongo-shard3.service
systemctl enable mongos.service

调用

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

mongodb:// 前缀,代表这是一个Connection String
username:password@ 如果启用了鉴权,需要指定用户密码
hostX:portX 多个 mongos 的地址列表
/database 鉴权时,用户帐号所属的数据库
?options 指定额外的连接选项

LEAVE A COMMENT

Captcha Code