mongodb sharding 的安装( replica set + sharding)
os: centos 7.4 
monggo: 3.6.6
mongo 作为一款优秀的nosql产品,sharding功能是必须的,其包含如下组件。
config server: 
 存储集群的元数据(metadata),即数据的哪一部分放在哪一个shard上,router将会利用这些元数据将请求分发到对应的shards上,shards上chunk的迁移也是config server来控制的。
router 路由节点 
mongos实例,在一个集群中直接为应用程序提供服务,利用config server上的元数据来制定最佳的查询计划。
shard 数据节点 
存储数据,可以是单个的mongod,也可以是replica set。在生产环境中,为了提高高可用性,都会使用replica set。存储在mongod上的数据以chunk为基本单位,默认的大小为64M,后面会介绍shard上数据的分裂(split)与迁移(migration)
今天搭个mongos的测试环境,规划如下:
192.168.56.101 node1 configserver replset(27017、27018、27019)
192.168.56.102 node2 mongos(27017、27018、27019)
192.168.56.103 node3  shard1 replset(27017、27018、27019) 
192.168.56.104 node4  shard2 replset(27017、27018、27019) 
192.168.56.105 node5  shard3 replset(27017、27018、27019)
使用replset来保证每个环节都是高可用的,测试环境,放在一台机器上。
os设置
配置dns
vi /etc/resolv.conf 
nameserver 8.8.8.8 
nameserver 8.8.4.4禁止selinux
# vi /etc/selinux/config
SELINUX=disabled
node1 configserver 创建相关目录
# mkdir -p /var/lib/{mongodbc1,mongodbc2,mongodbc3}
# mkdir -p /var/log/mongodb
# chown -R mongodb:mongodb /var/lib/mongodbc*
# chown -R mongodb:mongodb /var/log/mongodbnode2 mongos 创建相关目录
# mkdir -p /var/lib/{mongodbs1,mongodbs2,mongodbs3}
# mkdir -p /var/log/mongodb
# chown -R mongodb:mongodb /var/lib/mongodbs*
# chown -R mongodb:mongodb /var/log/mongodbnode3、node4、node5 数据节点创建相关目录
# mkdir -p /var/lib/{mongodb1,mongodb2,mongodb3}
# mkdir -p /var/log/mongodb
# chown -R mongodb:mongodb /var/lib/mongodb*
# chown -R mongodb:mongodb /var/log/mongodb安装mongodb
在 node1 ~ node5 节点上都安装mongodb 
下载 tgz  
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.6.tgz
或者使用yum安装 
配置yum源 
https://docs.mongodb.com/v3.6/tutorial/install-mongodb-on-red-hat/
# vi /etc/yum.repos.d/mongodb-org-3.6.repo
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc# yum install -y mongodb-org-3.6.6 mongodb-org-server-3.6.6 mongodb-org-shell-3.6.6 mongodb-org-mongos-3.6.6 mongodb-org-tools-3.6.6mongodb configserver
node1配置节点的配置文件 
configserver也是一个副本集,对应mongoc1.conf,mongoc2.conf,mongoc3.conf 三个配置文件
# vi /etc/mongoc1.conf
systemLog:
    destination: file
    path: /var/log/mongodb/mongoc1.log
    logAppend: true
    logRotate: reopen
storage:
    ##journal配置
    journal:
        enabled: true
    ##数据文件存储位置
    dbPath: /var/lib/mongodbc1
    ##是否一个库一个文件夹
    directoryPerDB: true
    ##数据引擎
    engine: wiredTiger
    ##WT引擎配置
    wiredTiger:
        engineConfig:
            ##WT最大使用cache(根据服务器实际情况调节,32G的独享服务器,设置为24G)
            cacheSizeGB: 1
            ##是否将索引也按数据库名单独存储
            directoryForIndexes: true
        ##表压缩配置(数据量不是很大,使用snappy减少资源占用)
        collectionConfig:
            blockCompressor: snappy
        ##索引配置
        indexConfig:
            prefixCompression: true
##端口配置
net:
    port: 27017
    bindIp: 0.0.0.0
##进程管理    
processManagement:
    fork: true    
##复制集配置
replication:
    ##oplog大小
    oplogSizeMB: 1024
    ##复制集名称
    replSetName: conf
##分片
sharding:
    clusterRole: configsvr    
##安全认证
security:
    authorization: enabled
    keyFile: /var/lib/mongodbc1/mongoDB_keyfilemongodb shared(数据节点)
node3、node4、node5数据节点的配置文件 
其中 node3 节点上的 replSetName 为 shared1 
其中 node4 节点上的 replSetName 为 shared2 
其中 node5 节点上的 replSetName 为 shared3
# vi /etc/mongod1.conf
systemLog:
    destination: file
    path: /var/log/mongodb/mongod1.log
    logAppend: true
    logRotate: reopen
storage:
    ##journal配置
    journal:
        enabled: true
    ##数据文件存储位置
    dbPath: /var/lib/mongodb1
    ##是否一个库一个文件夹
    directoryPerDB: true
    ##数据引擎
    engine: wiredTiger
    ##WT引擎配置
    wiredTiger:
        engineConfig:
            ##WT最大使用cache(根据服务器实际情况调节,32G的独享服务器,设置为24G)
            cacheSizeGB: 1
            ##是否将索引也按数据库名单独存储
            directoryForIndexes: true
        ##表压缩配置(数据量不是很大,使用snappy减少资源占用)
        collectionConfig:
            blockCompressor: snappy
        ##索引配置
        indexConfig:
            prefixCompression: true
##端口配置
net:
    port: 27017
    bindIp: 0.0.0.0
##进程管理
processManagement:
    fork: true
##复制集配置
replication:
    ##oplog大小
    oplogSizeMB: 1024
    ##复制集名称
    replSetName: shard1   
##分片
sharding:
    clusterRole: shardsvr    
##安全认证
security:
    authorization: enabled
    keyFile: /var/lib/mongodb1/mongoDB_keyfilemongodb mongos
node2路由节点的配置文件
# vi /etc/mongos1.conf
systemLog:
    destination: file
    path: /var/log/mongodb/mongos1.log
    logAppend: true
    logRotate: reopen
##端口配置
net:
    port: 27017
    bindIp: 0.0.0.0
    maxIncomingConnections: 10000
##进程管理
processManagement:
    fork: true
    pidFilePath: /var/lib/mongodbs1/mongos1.pid
##分片
sharding:
    configDB: conf/192.168.56.101:27017,192.168.56.101:27018,192.168.56.101:27019
##安全认证
security:
    keyFile: /var/lib/mongodbs1/mongoDB_keyfile
    clusterAuthMode: "keyFile"
启动mongodb各组件
config,mongos,shared 的 keyfile 的内容必须保持一致
node1节点启动 config
创建keyfile
$ vi /var/lib/mongodbc1/mongoDB_keyfile
This is mongos mongodb key file DO NOT DELETE IT
$ vi /var/lib/mongodbc2/mongoDB_keyfile
This is mongos mongodb key file DO NOT DELETE IT
$ vi /var/lib/mongodbc3/mongoDB_keyfile
This is mongos mongodb key file DO NOT DELETE IT
$ chmod 600 /var/lib/mongodbc1/mongoDB_keyfile
$ chmod 600 /var/lib/mongodbc2/mongoDB_keyfile
$ chmod 600 /var/lib/mongodbc3/mongoDB_keyfile启动mongodb,启动前先注销配置文件的 authorization 和 keyFile
$ mongod --config  /etc/mongoc1.conf
$ mongod --config  /etc/mongoc2.conf
$ mongod --config  /etc/mongoc3.conf初始化config的副本集
> config = {
    _id : "conf",
    members : [
      {_id : 0, host : "192.168.56.101:27017"},
      {_id : 1, host : "192.168.56.101:27018"},
      {_id : 2, host : "192.168.56.101:27019"}
    ]
  }
> rs.initiate(config)初始化完毕之后,创建admin用户
> use admin;
> db.getUsers();
> use admin
switched to db admin
> db.createUser(
{ user:"root",
  pwd:"rootroot",
  roles:[{role:"readWriteAnyDatabase",db:"admin"},
         {role:"dbAdminAnyDatabase",db:"admin"},
         {role:"userAdminAnyDatabase",db:"admin"},
         { role: "clusterAdmin", db: "admin" }
         ]
}
)关闭mongodb
$ mongod --config  /etc/mongoc1.conf --shutdown
$ mongod --config  /etc/mongoc2.conf --shutdown
$ mongod --config  /etc/mongoc3.conf --shutdown配置文件启用 authorization 和 keyFile,再次启动
$ mongod --config  /etc/mongoc1.conf
$ mongod --config  /etc/mongoc2.conf
$ mongod --config  /etc/mongoc3.conf至此 mongodb config 配置完毕
node3、node4、node5节点启动shared
创建keyfile
$ vi /var/lib/mongodb1/mongoDB_keyfile
This is mongos mongodb key file DO NOT DELETE IT
$ vi /var/lib/mongodb2/mongoDB_keyfile
This is mongos mongodb key file DO NOT DELETE IT
$ vi /var/lib/mongodb3/mongoDB_keyfile
This is mongos mongodb key file DO NOT DELETE IT
$ chmod 600 /var/lib/mongodb1/mongoDB_keyfile
$ chmod 600 /var/lib/mongodb2/mongoDB_keyfile
$ chmod 600 /var/lib/mongodb3/mongoDB_keyfile同样启动前先注销配置文件的 authorization 和 keyFile
$ mongod --config  /etc/mongod1.conf
$ mongod --config  /etc/mongod2.conf
$ mongod --config  /etc/mongod3.conf初始化shard副本集,不同节点执行时注意id、ip
> config = {
    _id : "shard3",
    members : [
      {_id : 0, host : "192.168.56.105:27017"},
      {_id : 1, host : "192.168.56.105:27018"},
      {_id : 2, host : "192.168.56.105:27019"}
    ]
  }
> rs.initiate(config)剩下的参考config的replicate set 启动好就可以了。
目前拥有3个shard,均为副本集
node2节点启动 mongos
至此,config 及 shard 都起来了,接下来启动 mongos
创建keyfile
$ vi /var/lib/mongodbs1/mongoDB_keyfile
This is mongos mongodb key file DO NOT DELETE IT
$ vi /var/lib/mongodbs2/mongoDB_keyfile
This is mongos mongodb key file DO NOT DELETE IT
$ vi /var/lib/mongodbs3/mongoDB_keyfile
This is mongos mongodb key file DO NOT DELETE IT
$ chmod 600 /var/lib/mongodbc1/mongoDB_keyfile
$ chmod 600 /var/lib/mongodbc2/mongoDB_keyfile
$ chmod 600 /var/lib/mongodbc3/mongoDB_keyfile需要使用 mongos 命令启动
$ mongos --config  /etc/mongos1.conf
$ mongos --config  /etc/mongos2.conf
$ mongos --config  /etc/mongos3.conf设置 mongodb sharding
node2 登录 mongos
$ mongo --port 27017
MongoDB shell version v3.6.6
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.6.6
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
mongos> use admin
mongos> db.auth('root','rootroot')
mongos> show dbs
mongos> sh.status()
mongos> sh.addShard("shard1/192.168.56.103:27017,192.168.56.103:27018,192.168.56.103:27019");
mongos> sh.addShard("shard2/192.168.56.104:27017,192.168.56.104:27018,192.168.56.104:27019");
mongos> sh.addShard("shard3/192.168.56.105:27017,192.168.56.105:27018,192.168.56.105:27019");
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5b55bba0b4856e5663e0a7ad")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/192.168.56.103:27017,192.168.56.103:27018,192.168.56.103:27019",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/192.168.56.104:27017,192.168.56.104:27018,192.168.56.104:27019",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/192.168.56.105:27017,192.168.56.105:27018,192.168.56.105:27019",  "state" : 1 }
  active mongoses:
        "3.6.6" : 3
  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 }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard1  1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0) 现在,config、mongos、shard都已经启动了。
测试
我们搭建 mongodb sharding,目的是让数据分散在各个datanode上。
登录mongos
$ mongo --port 27017
mongos> use testdb;
mongos> for (var i = 1; i <= 1000000; i++) db.table1.save({id:i,"test1":"testval1"});
mongos> db.table1.stats();
db.table1.stats();
{
    "sharded" : false,
    "primary" : "shard3",
    "ns" : "testdb.table1",可以看到,默认是没有shard,搭建 sharding 就是为了分片,但是搭建好后默认不分片,有点想不通为什么默认不分片。
分片设置
mongos> sh.enableSharding("testdb");
mongos> sh.shardCollection("testdb.table2", {"_id": 1})
mongos> for (var i = 1; i <= 1000000; i++) db.table2.save({id:i,"test1":"testval1"});
mongos> db.table2.stats()
{
    "sharded" : true,
    "capped" : false,
    "ns" : "testdb.table2",
    "count" : 1000000,
可以看到已经sharding了,但是由于MongoDB 默认的 chunk size 为64MB,所以还是放在一个shard上。
参考: 
https://docs.mongodb.com/v3.6/sharding/ 
http://www.mongoing.com/
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号