centos7.8环境下搭建mongodb3.4.24复制分片集群并添加密码验证 集群的架构

centos7.8环境下搭建mongodb3.4.24复制分片集群并添加密码验证

集群的架构


从图中可以看到有四个组件:mongos、config server、shard、replica set。

mongos:数据库集群请求的入口,所有的请求都通过mongos进行协调,不需要在应用程序添加一个路由选择器,mongos自己就是一个请求分发中心,它负责把对应的数据请求请求转发到对应的shard服务器上。在生产环境通常有多mongos作为请求的入口,防止其中一个挂掉所有的mongodb请求都没有办法操作。

config server:顾名思义为配置服务器,存储所有数据库元信息(路由、分片)的配置。mongos本身没有物理存储分片服务器和数据路由信息,只是缓存在内存里,配置服务器则实际存储这些数据。mongos第一次启动或者关掉重启就会从 config server 加载配置信息,以后如果配置服务器信息变化会通知到所有的 mongos 更新自己的状态,这样 mongos 就能继续准确路由。在生产环境通常有多个 config server 配置服务器,因为它存储了分片路由的元数据,防止数据丢失!

shard:分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程。将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处理更大的负载。基本思想就是将集合切成小块,这些块分散到若干片里,每个片只负责总数据的一部分,最后通过一个均衡器来对各个分片进行均衡(数据迁移)。

replica set:中文翻译副本集,其实就是shard的备份,防止shard挂掉之后数据丢失。复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。

仲裁者(Arbiter),是复制集中的一个MongoDB实例,它并不保存数据。仲裁节点使用最小的资源并且不要求硬件设备,不能将Arbiter部署在同一个数据集节点中,可以部署在其他应用服务器或者监视服务器中,也可部署在单独的虚拟机中。为了确保复制集中有奇数的投票成员(包括primary),需要添加仲裁节点做为投票,否则primary不能运行时不会自动切换primary。

简单了解之后,我们可以这样总结一下,应用请求mongos来操作mongodb的增删改查,配置服务器存储数据库元信息,并且和mongos做同步,数据最终存入在shard(分片)上,为了防止数据丢失同步在副本集中存储了一份,仲裁在数据存储到分片的时候决定存储到哪个节点。

分片集群架构:

Config server: 存储集群所有节点,分片数据路由信息,默认需要配置3个config server节点;
Mongos: 提供对外应用的访问,所有操作均通过mongos执行,一般有多个mongos节点,数据迁移和数据自动平衡;
Mongod: 存储应用数据记录,一般有多个Mongod节点,达到数据的分片目的。



大概步骤介绍:
1.环境准备:下载解压mongo3.4.24二进制安装包
2.安装config server配置服务器
3.安装配置分片副本集
4.配置路由服务器 mongos
5.设置分片集
6.验证测试

环境准备:
centos7.8_x86_64环境

安装包: mongodb-linux-x86_64-rhel70-3.4.24.tgz

服务器的角色规划



1、安装mongodb

# 解压
tar xf mongodb-linux-x86_64-rhel70-3.4.24.tgz -C /usr/local/
# 改名
cd /usr/local
mv mongodb-linux-x86_64-rhel70-3.4.24 mongodb

# 添加运行mongodb的用户mongo,避免直接使用root带来安全隐患
groupadd -g 1506 mongo
useradd -u 1506 -g mongo mongo

# 配置相关的数据和日志目录
mkdir -p /usr/local/mongodb/conf
mkdir -p /data/mongodb/mongos/data
mkdir -p /data/mongodb/mongos/log
mkdir -p /data/mongodb/config/data
mkdir -p /data/mongodb/config/log
mkdir -p /data/mongodb/shard1/data
mkdir -p /data/mongodb/shard1/log
mkdir -p /data/mongodb/shard2/data
mkdir -p /data/mongodb/shard2/log
mkdir -p /data/mongodb/shard3/data
mkdir -p /data/mongodb/shard3/log

# 对这些目录授权
chown -R mongo.mongo /usr/local/mongodb
chown -R mongo.mongo /data/mongodb

配置环境变量

# vim /etc/profile

export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH

# 使立即生效
source /etc/profile


2、config server配置服务器(三台机器都要添加)

mongodb3.4以后要求配置服务器也创建副本集,不然集群搭建不成功。

添加配置文件

vi /usr/local/mongodb/conf/config.conf

## 配置文件内容
pidfilepath = /data/mongodb/config/log/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
 
#declare this is a config db of a cluster;
configsvr = true

#副本集名称
replSet=configs
 
#设置最大连接数
maxConns=20000

启动三台服务器的config server
su mongo
mongod -f /usr/local/mongodb/conf/config.conf

 

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

#连接
mongo --port 21000
#config变量

config = {
   _id : "configs",
    members : [
        {_id : 0, host : "172.30.0.109:21000" },
        {_id : 1, host : "172.30.0.108:21000" },
        {_id : 2, host : "172.30.0.110:21000" }
    ]
}

# 初始化副本集
rs.initiate(config)
其中,"_id" : "configs"应与配置文件中配置的 replicaction.replSetName 一致,"members" 中的 "host" 为三个节点的 ip 和 port


[mongo@eus-image-design-mongo01:/usr/local]$ mongo --port 21000
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:21000/
MongoDB server version: 3.4.24
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings:
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten]
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten]
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten]
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten]
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten]
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2020-08-07T15:59:50.817+0800 I CONTROL  [initandlisten]
> config = {
...    _id : "configs",
...     members : [
...         {_id : 0, host : "172.30.0.109:21000" },
...         {_id : 1, host : "172.30.0.108:21000" },
...         {_id : 2, host : "172.30.0.110:21000" }
...     ]
... }
{
        "_id" : "configs",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "172.30.0.109:21000"
                },
                {
                        "_id" : 1,
                        "host" : "172.30.0.108:21000"
                },
                {
                        "_id" : 2,
                        "host" : "172.30.0.110:21000"
                }
        ]
}
> rs.initiate(config)
{ "ok" : 1 }
configs:SECONDARY>

 

3、配置分片副本集(三台机器)

记得要在 mongo 用户下操作
su mongo

设置第一个分片副本集

配置文件(三台机器都添加)


vi /usr/local/mongodb/conf/shard1.conf

#配置文件内容
#——————————————–

pidfilepath = /data/mongodb/shard1/log/shard1.pid
dbpath = /data/mongodb/shard1/data/
logpath = /data/mongodb/shard1/log/shard1.log
logappend = true

bind_ip = 0.0.0.0
port = 27001
fork = true
 
#打开web监控
httpinterface=true
rest=true
 
#副本集名称
replSet=shard1
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#设置最大连接数
maxConns=20000

启动三台服务器的shard1 server

mongod -f /usr/local/mongodb/conf/shard1.conf

登陆任意一台服务器,初始化副本集

mongo --port 27001
#使用admin数据库
use admin
#定义副本集配置,第三个节点的 "arbiterOnly":true 代表其为仲裁节点。
config = {
    _id : "shard1",
     members : [
         {_id : 0, host : "172.30.0.109:27001" },
         {_id : 1, host : "172.30.0.108:27001" },
         {_id : 2, host : "172.30.0.110:27001" , arbiterOnly: true }
     ]
 }
#初始化副本集配置
rs.initiate(config);


[mongo@eus-image-design-mongo01:/root]$ mongo --port 27001
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:27001/
MongoDB server version: 3.4.24
Server has startup warnings:
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten]
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten]
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten]
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten]
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten]
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2020-08-07T16:37:18.528+0800 I CONTROL  [initandlisten]

> config = {
...     _id : "shard1",
...      members : [
...          {_id : 0, host : "172.30.0.109:27001" },
...          {_id : 1, host : "172.30.0.108:27001" },
...          {_id : 2, host : "172.30.0.110:27001" , arbiterOnly: true }
...      ]
...  }
{
    "_id" : "shard1",
    "members" : [
        {
            "_id" : 0,
            "host" : "172.30.0.109:27001"
        },
        {
            "_id" : 1,
            "host" : "172.30.0.108:27001"
        },
        {
            "_id" : 2,
            "host" : "172.30.0.110:27001",
            "arbiterOnly" : true
        }
    ]
}
> rs.initiate(config);
{ "ok" : 1 }


设置第二个分片副本集

配置文件

vi /usr/local/mongodb/conf/shard2.conf

#配置文件内容
#——————————————–
pidfilepath = /data/mongodb/shard2/log/shard2.pid
dbpath = /data/mongodb/shard2/data/
logpath = /data/mongodb/shard2/log/shard2.log
logappend = true

bind_ip = 0.0.0.0
port = 27002
fork = true
 
#打开web监控
httpinterface=true
rest=true
 
#副本集名称
replSet=shard2
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#设置最大连接数
maxConns=20000

启动三台服务器的shard2 server

mongod -f /usr/local/mongodb/conf/shard2.conf

登陆任意一台服务器,初始化副本集

mongo --port 27002
#使用admin数据库
use admin
#定义副本集配置
config = {
    _id : "shard2",
     members : [
         {_id : 0, host : "172.30.0.109:27002" },
         {_id : 1, host : "172.30.0.108:27002"  , arbiterOnly: true },
         {_id : 2, host : "172.30.0.110:27002" }
     ]
 }

#初始化副本集配置
rs.initiate(config);


[root@eus-image-design-mongo01:~]# su mongo
[mongo@eus-image-design-mongo01:/root]$ mongo --port 27002
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:27002/
MongoDB server version: 3.4.24
Server has startup warnings:
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten]
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten]
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten]
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten]
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten]
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2020-08-07T16:42:43.034+0800 I CONTROL  [initandlisten]
>  config = {
...     _id : "shard2",
...      members : [
...          {_id : 0, host : "172.30.0.109:27002"  , arbiterOnly: true },
...          {_id : 1, host : "172.30.0.108:27002" },
...          {_id : 2, host : "172.30.0.110:27002" }
...      ]
...  }
{
    "_id" : "shard2",
    "members" : [
        {
            "_id" : 0,
            "host" : "172.30.0.109:27002",
            "arbiterOnly" : true
        },
        {
            "_id" : 1,
            "host" : "172.30.0.108:27002"
        },
        {
            "_id" : 2,
            "host" : "172.30.0.110:27002"
        }
    ]
}
# 报错
> rs.initiate(config);
{
    "ok" : 0,
    "errmsg" : "This node, 172.30.0.109:27002, with _id 0 is not electable under the new configuration version 1 for replica set shard2",
    "code" : 93,
    "codeName" : "InvalidReplicaSetConfig"
}
> config = {
...     _id : "shard2",
...      members : [
...          {_id : 0, host : "172.30.0.109:27002"  , arbiterOnly: true },
...          {_id : 1, host : "172.30.0.108:27002" },
...          {_id : 2, host : "172.30.0.110:27002" }
...      ]
...  }
{
    "_id" : "shard2",
    "members" : [
        {
            "_id" : 0,
            "host" : "172.30.0.109:27002",
            "arbiterOnly" : true
        },
        {
            "_id" : 1,
            "host" : "172.30.0.108:27002"
        },
        {
            "_id" : 2,
            "host" : "172.30.0.110:27002"
        }
    ]
}
> rs.initiate(config);
{
    "ok" : 0,
    "errmsg" : "This node, 172.30.0.109:27002, with _id 0 is not electable under the new configuration version 1 for replica set shard2",
    "code" : 93,
    "codeName" : "InvalidReplicaSetConfig"
}
# 仲裁不能再第一条机器上,于是更换

> config = {
...     _id : "shard2",
...      members : [
...          {_id : 0, host : "172.30.0.109:27002" },
...          {_id : 1, host : "172.30.0.108:27002"  , arbiterOnly: true },
...          {_id : 2, host : "172.30.0.110:27002" }
...      ]
...  }
{
    "_id" : "shard2",
    "members" : [
        {
            "_id" : 0,
            "host" : "172.30.0.109:27002"
        },
        {
            "_id" : 1,
            "host" : "172.30.0.108:27002",
            "arbiterOnly" : true
        },
        {
            "_id" : 2,
            "host" : "172.30.0.110:27002"
        }
    ]
}
> rs.initiate(config);
{ "ok" : 1 }
shard2:SECONDARY>


设置第三个分片副本集

配置文件

vi /usr/local/mongodb/conf/shard3.conf

#配置文件内容
#——————————————–
pidfilepath = /data/mongodb/shard3/log/shard3.pid
dbpath = /data/mongodb/shard3/data
logpath = /data/mongodb/shard3/log/shard3.log
logappend = true

bind_ip = 0.0.0.0
port = 27003
fork = true
 
#打开web监控
httpinterface=true
rest=true
 
#副本集名称
replSet=shard3
 
#declare this is a shard db of a cluster;
shardsvr = true
 
#设置最大连接数
maxConns=20000

启动三台服务器的shard3 server

mongod -f /usr/local/mongodb/conf/shard3.conf

登陆任意一台服务器,初始化副本集

mongo --port 27003
#使用admin数据库
use admin
#定义副本集配置
config = {
   _id : "shard3",
    members : [
        {_id : 0, host : "172.30.0.109:27003"},
        {_id : 1, host : "172.30.0.108:27003" },
        {_id : 2, host : "172.30.0.110:27003", arbiterOnly: true  }
    ]
}

#初始化副本集配置
rs.initiate(config);

[root@eus-image-design-mongo01:~]# mongo --port 27003
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:27003/
MongoDB server version: 3.4.24
Server has startup warnings:
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten]
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten]
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten]
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten]
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten]
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2020-08-07T17:01:09.101+0800 I CONTROL  [initandlisten]
> use admin
switched to db admin
> config = {
...    _id : "shard3",
...     members : [
...         {_id : 0, host : "172.30.0.109:27003", arbiterOnly: true },
...         {_id : 1, host : "172.30.0.108:27003" },
...         {_id : 2, host : "172.30.0.110:27003" }
...     ]
... }
{
    "_id" : "shard3",
    "members" : [
        {
            "_id" : 0,
            "host" : "172.30.0.109:27003",
            "arbiterOnly" : true
        },
        {
            "_id" : 1,
            "host" : "172.30.0.108:27003"
        },
        {
            "_id" : 2,
            "host" : "172.30.0.110:27003"
        }
    ]
}
# 节点1不能设置为仲裁节点
> rs.initiate(config);
{
    "ok" : 0,
    "errmsg" : "This node, 172.30.0.109:27003, with _id 0 is not electable under the new configuration version 1 for replica set shard3",
    "code" : 93,
    "codeName" : "InvalidReplicaSetConfig"
}
> config = {
...    _id : "shard3",
...     members : [
...         {_id : 0, host : "172.30.0.109:27003"},
...         {_id : 1, host : "172.30.0.108:27003" },
...         {_id : 2, host : "172.30.0.110:27003", arbiterOnly: true  }
...     ]
... }
{
    "_id" : "shard3",
    "members" : [
        {
            "_id" : 0,
            "host" : "172.30.0.109:27003"
        },
        {
            "_id" : 1,
            "host" : "172.30.0.108:27003"
        },
        {
            "_id" : 2,
            "host" : "172.30.0.110:27003",
            "arbiterOnly" : true
        }
    ]
}
> rs.initiate(config);
{ "ok" : 1 }
4、配置路由服务器 mongos

先启动配置服务器和分片服务器,后启动路由实例启动路由实例:(三台机器)

vi /usr/local/mongodb/conf/mongos.conf

 

#内容
pidfilepath = /data/mongodb/mongos/log/mongos.pid
logpath = /data/mongodb/mongos/log/mongos.log
logappend = true

bind_ip = 0.0.0.0
port = 20000
fork = true

#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
configdb = configs/172.30.0.109:21000,172.30.0.108:21000,172.30.0.110:21000
 
#设置最大连接数
maxConns=20000

启动三台服务器的mongos server

mongos -f /usr/local/mongodb/conf/mongos.conf

5、启用分片

目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序连接到mongos路由服务器并不能使用分片机制,还需要在程序里设置分片配置,让分片生效。

登陆任意一台mongos

mongo --port 20000
#使用admin数据库
user  admin
#串联路由服务器与分配副本集
sh.addShard("shard1/172.30.0.109:27001,172.30.0.108:27001,172.30.0.110:27001")
sh.addShard("shard2/172.30.0.109:27002,172.30.0.108:27002,172.30.0.110:27002")
sh.addShard("shard3/172.30.0.109:27003,172.30.0.108:27003,172.30.0.110:27003")
#查看集群状态
sh.status()

[root@eus-image-design-mongo01:~]# su mongo
[mongo@eus-image-design-mongo01:/root]$ mongo --port 20000
MongoDB shell version v3.4.24
connecting to: mongodb://127.0.0.1:20000/
MongoDB server version: 3.4.24
Server has startup warnings:
2020-08-07T17:11:21.788+0800 I CONTROL  [main]
2020-08-07T17:11:21.788+0800 I CONTROL  [main] ** WARNING: Access control is not enabled for the database.
2020-08-07T17:11:21.788+0800 I CONTROL  [main] **          Read and write access to data and configuration is unrestricted.
2020-08-07T17:11:21.788+0800 I CONTROL  [main]
mongos> use admin
switched to db admin
mongos> sh.addShard("shard1/172.30.0.109:27001,172.30.0.108:27001,172.30.0.110:27001")
{ "shardAdded" : "shard1", "ok" : 1 }
mongos> sh.addShard("shard2/172.30.0.109:27002,172.30.0.108:27002,172.30.0.110:27002")
{ "shardAdded" : "shard2", "ok" : 1 }
mongos> sh.addShard("shard3/172.30.0.109:27003,172.30.0.108:27003,172.30.0.110:27003")
{ "shardAdded" : "shard3", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
      "_id" : 1,
      "minCompatibleVersion" : 5,
      "currentVersion" : 6,
      "clusterId" : ObjectId("5f2d117035911ba952f2d6a4")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/172.30.0.108:27001,172.30.0.109:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/172.30.0.109:27002,172.30.0.110:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/172.30.0.108:27003,172.30.0.109:27003",  "state" : 1 }
  active mongoses:
        "3.4.24" : 3
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
NaN
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:

mongos>


6、测试

目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。

#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});

#指定数据库里需要分片的集合和片键
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )

我们设置testdb的 table1 表需要分片,根据 id 自动分片到 shard1 ,shard2,shard3 上面去。要这样设置是因为不是所有mongodb 的数据库和表 都需要分片!

测试分片配置结果

mongos> db.runCommand( { enablesharding :"testdb"});
{ "ok" : 1 }
mongos> db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
{ "collectionsharded" : "testdb.table1", "ok" : 1 }
mongos> use  testdb;
switched to db testdb
mongos> show tables;
table1
mongos> for (var i = 1; i <= 100000; i++) db.table1.save({id:i,"test1":"testval1"});
WriteResult({ "nInserted" : 1 })
mongos> 
mongos> db.table1.stats();

后期运维
启动关闭

mongodb的启动顺序是,先启动配置服务器,在启动分片,最后启动mongos.

mongod -f /usr/local/mongodb/conf/config.conf
mongod -f /usr/local/mongodb/conf/shard1.conf
mongod -f /usr/local/mongodb/conf/shard2.conf
mongod -f /usr/local/mongodb/conf/shard3.conf
# 注意是mongos命令
mongos -f /usr/local/mongodb/conf/mongos.conf

关闭时,直接killall杀掉所有进程

killall mongod
killall mongos

posted @ 2020-08-07 22:14  reblue520  阅读(414)  评论(0编辑  收藏  举报