时间:2021-07-01 10:21:17 帮助过:5人阅读
$ cat /etc/redhat-release CentOS Linux release 7.0.1406 (Core) $ uname -a Linux zhaopin-2-201 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux $ mongo --version MongoDB shell version: 3.0.6
$ sudo mkdir -p /data/mongodb/{data/{sh0,sh1},backup/{sh0,sh1},log/{sh0,sh1},conf/{sh0,sh1}} 
第一个分片:
$ sudo vim /data/mongodb/conf/sh0/mongodb.conf # base port = 27010 maxConns = 800 filePermissions = 0700 fork = true noauth = true directoryperdb = true dbpath = /data/mongodb/data/sh0 pidfilepath = /data/mongodb/data/sh0/mongodb.pid oplogSize = 10 journal = true # security nohttpinterface = true rest = false # log logpath = /data/mongodb/log/sh0/mongodb.log logRotate = rename logappend = true slowms = 50 replSet = sh0 shardsvr = true
第二个分片:
$ sudo vim /data/mongodb/conf/sh1/mongodb.conf # base port = 27011 maxConns = 800 filePermissions = 0700 fork = true noauth = true directoryperdb = true dbpath = /data/mongodb/data/sh1 pidfilepath = /data/mongodb/data/sh1/mongodb.pid oplogSize = 10 journal = true # security nohttpinterface = true rest = false # log logpath = /data/mongodb/log/sh1/mongodb.log logRotate = rename logappend = true slowms = 50 replSet = sh1 shardsvr = true
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh0/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 41492 child process started successfully, parent exiting $ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh1/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 41509 child process started successfully, parent exiting $ ps aux | grep mongo | grep -v grep root 41492 0.5 0.0 518016 54604 ? Sl 10:09 0:00 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh0/mongodb.conf root 41509 0.5 0.0 516988 51824 ? Sl 10:09 0:00 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh1/mongodb.conf $ mongo --port 27010 MongoDB shell version: 3.0.6 connecting to: 127.0.0.1:27010/test > bye $ mongo --port 27011 MongoDB shell version: 3.0.6 connecting to: 127.0.0.1:27011/test > bye
$ sudo mkdir -p /data/mongodb/{data/{cf0,cf1,cf2},backup/{cf0,cf1,cf2},log/{cf0,cf1,cf2},conf/{cf0,cf1,cf2}} 
第一个配置服务器:
$ sudo vim /data/mongodb/conf/cf0/config.conf # base port = 27000 maxConns = 800 filePermissions = 0700 fork = true noauth = true directoryperdb = true dbpath = /data/mongodb/data/cf0 pidfilepath = /data/mongodb/data/cf0/config.pid oplogSize = 10 journal = true # security nohttpinterface = true rest = false # log logpath = /data/mongodb/log/cf0/config.log logRotate = rename logappend = true slowms = 50 configsvr = true
第二个配置服务器:
$ sudo vim /data/mongodb/conf/cf1/config.conf # base port = 27001 maxConns = 800 filePermissions = 0700 fork = true noauth = true directoryperdb = true dbpath = /data/mongodb/data/cf1 pidfilepath = /data/mongodb/data/cf1/config.pid oplogSize = 10 journal = true # security nohttpinterface = true rest = false # log logpath = /data/mongodb/log/cf1/config.log logRotate = rename logappend = true slowms = 50 configsvr = true
第三个配置服务器:
$ sudo vim /data/mongodb/conf/cf2/config.conf # base port = 27002 maxConns = 800 filePermissions = 0700 fork = true noauth = true directoryperdb = true dbpath = /data/mongodb/data/cf2 pidfilepath = /data/mongodb/data/cf2/config.pid oplogSize = 10 journal = true # security nohttpinterface = true rest = false # log logpath = /data/mongodb/log/cf2/config.log logRotate = rename logappend = true slowms = 50 configsvr = true
$ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf0/config.conf about to fork child process, waiting until server is ready for connections. forked process: 41759 child process started successfully, parent exiting $ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf1/config.conf about to fork child process, waiting until server is ready for connections. forked process: 41774 child process started successfully, parent exiting $ sudo /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf2/config.conf about to fork child process, waiting until server is ready for connections. forked process: 42170 child process started successfully, parent exiting $ ps aux | grep mongo | grep -v grep root 41492 0.3 0.0 518016 54728 ? Sl 10:09 0:06 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh0/mongodb.conf root 41509 0.3 0.0 518016 54760 ? Sl 10:09 0:06 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh1/mongodb.conf root 41855 0.4 0.0 467828 51684 ? Sl 10:25 0:03 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf0/config.conf root 41870 0.4 0.0 467828 52312 ? Sl 10:25 0:03 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf1/config.conf root 42170 0.9 0.0 467824 52392 ? Sl 10:37 0:00 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf2/config.conf
$ sudo mkdir -p /data/mongodb/{data/{ms0,ms1},backup/{ms0,ms1},log/{ms0,ms1},conf/{ms0,ms1}} 
第一个配置服务器:
$ sudo vim /data/mongodb/conf/ms0/mongos.conf # base port = 30000 maxConns = 800 filePermissions = 0700 fork = true pidfilepath = /data/mongodb/data/ms0/mongos.pid # log logpath = /data/mongodb/log/ms0/mongos.log logRotate = rename logappend = true configdb = 172.30.2.201:27000,172.30.2.201:27001,172.30.2.201:27002
第二个配置服务器:
$ sudo vim /data/mongodb/conf/ms1/mongos.conf # base port = 30000 maxConns = 800 filePermissions = 0700 fork = true pidfilepath = /data/mongodb/data/ms1/mongos.pid # log logpath = /data/mongodb/log/ms1/mongos.log logRotate = rename logappend = true configdb = 172.30.2.201:27000,172.30.2.201:27001,172.30.2.201:27002
$ sudo /opt/mongodb/bin/mongos --config /data/mongodb/conf/ms0/mongos.conf about to fork child process, waiting until server is ready for connections. forked process: 42233 child process started successfully, parent exiting $ sudo /opt/mongodb/bin/mongos --config /data/mongodb/conf/ms1/mongos.conf about to fork child process, waiting until server is ready for connections. forked process: 42286 child process started successfully, parent exiting $ ps aux | grep mongo | grep -v grep root 41492 0.3 0.0 518016 54728 ? Sl 10:09 0:06 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh0/mongodb.conf root 41509 0.3 0.0 518016 54760 ? Sl 10:09 0:07 /opt/mongodb/bin/mongod --config /data/mongodb/conf/sh1/mongodb.conf root 41855 0.4 0.0 546724 37812 ? Sl 10:25 0:03 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf0/config.conf root 41870 0.4 0.0 546724 38188 ? Sl 10:25 0:03 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf1/config.conf root 42170 0.6 0.0 546720 41960 ? Sl 10:37 0:00 /opt/mongodb/bin/mongod --config /data/mongodb/conf/cf2/config.conf root 42233 0.5 0.0 233536 10188 ? Sl 10:38 0:00 /opt/mongodb/bin/mongos --config /data/mongodb/conf/ms0/mongos.conf root 42286 0.4 0.0 241728 9056 ? Sl 10:38 0:00 /opt/mongodb/bin/mongos --config /data/mongodb/conf/ms1/mongos.conf
配置副本集的好处是为了高可用,配置单节点是我自己为了节省时间,后续添加节点和副本集的操作一样,分片的配置不需要修改
分片一:
$ mongo --port 27010
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:27010/test
> use admin
switched to db admin
> cfg={_id:"sh0", members:[ {_id:0,host:"172.30.2.201:27010"}] }
{
        "_id" : "sh0",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "172.30.2.201:27010"
                }
        ]
}
> rs.initiate( cfg );
{ "ok" : 1 }
sh0:OTHER> rs.status();
{
        "set" : "sh0",
        "date" : ISODate("2015-10-09T02:45:39.178Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.30.2.201:27010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 2197,
                        "optime" : Timestamp(1444358731, 1),
                        "optimeDate" : ISODate("2015-10-09T02:45:31Z"),
                        "electionTime" : Timestamp(1444358731, 2),
                        "electionDate" : ISODate("2015-10-09T02:45:31Z"),
                        "configVersion" : 1,
                        "self" : true
                }
        ],
        "ok" : 1
}
sh0:PRIMARY>
bye 
分片二:
$ mongo --port 27011
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:27011/test
> cfg={_id:"sh1", members:[ {_id:0,host:"172.30.2.201:27011"}] }
{
        "_id" : "sh1",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "172.30.2.201:27011"
                }
        ]
}
> rs.initiate( cfg );
{ "ok" : 1 }
sh1:OTHER> rs.status();
{
        "set" : "sh1",
        "date" : ISODate("2015-10-09T02:46:32.961Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "172.30.2.201:27011",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 2244,
                        "optime" : Timestamp(1444358790, 1),
                        "optimeDate" : ISODate("2015-10-09T02:46:30Z"),
                        "electionTime" : Timestamp(1444358790, 2),
                        "electionDate" : ISODate("2015-10-09T02:46:30Z"),
                        "configVersion" : 1,
                        "self" : true
                }
        ],
        "ok" : 1
}
sh1:PRIMARY>
bye 
$ mongo --port 30000
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:30000/test
mongos> use admin;
switched to db admin
mongos> sh.addShard("sh0/172.30.2.201:27010");
{ "shardAdded" : "sh0", "ok" : 1 }
mongos> sh.addShard("sh1/172.30.2.201:27011");
{ "shardAdded" : "sh1", "ok" : 1 }
mongos> use mydb;
switched to db mydb
mongos> db.createCollection("test");
{
        "ok" : 1,
        "$gleStats" : {
                "lastOpTime" : Timestamp(1444358911, 1),
                "electionId" : ObjectId("56172a4bc03d9b1667f8e928")
        }
}
mongos> sh.enableSharding("mydb");
{ "ok" : 1 }
mongos> sh.shardCollection("mydb.test", {"_id":1});
{ "collectionsharded" : "mydb.test", "ok" : 1 }
mongos> sh.status();
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("561728b4030ea038bcb57fa0")
}
  shards:
        {  "_id" : "sh0",  "host" : "sh0/172.30.2.201:27010" }
        {  "_id" : "sh1",  "host" : "sh1/172.30.2.201:27011" }
  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" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "mydb",  "partitioned" : true,  "primary" : "sh0" }
                mydb.test
                        shard key: { "_id" : 1 }
                        chunks:
                                sh0     1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : sh0 Timestamp(1, 0) 
可见分片已经配置完成了
 
 
MongoDB分片搭建
标签: