MongoDB 3.4配置sharding分片
1. 建立配置服務副本集
建立資料目錄,config目錄供配置服務使用,shard目錄供分片資料節點使用
[root@MySQL01 ~]# su - mongo
[mongo@MySQL01 ~]$ cd /data/db
[mongo@MySQL01 db]$ mkdir config01
[mongo@MySQL01 db]$ mkdir config02
[mongo@MySQL01 db]$ mkdir config03
[mongo@MySQL01 db]$ mkdir shard01
[mongo@MySQL01 db]$ mkdir shard02
[mongo@MySQL01 db]$ mkdir shard03
啟動配置服務副本集的每個節點
[mongo@MySQL01 ~]$ mongod --configsvr --replSet fire --dbpath /data/db/config01 --port 10001
[mongo@MySQL01 ~]$ mongod --configsvr --replSet fire --dbpath /data/db/config02 --port 10002
[mongo@MySQL01 ~]$ mongod --configsvr --replSet fire --dbpath /data/db/config03 --port 10003
連線到配置伺服器,執行初始化操作
[mongo@MySQL01 ~]$ mongo localhost:10001
MongoDB shell version v3.4.10
connecting to: localhost:10001
MongoDB server version: 3.4.10
[mongo@MySQL01 ~]$ mongo localhost:20001
MongoDB shell version v3.4.10
connecting to: localhost:20001
MongoDB server version: 3.4.10
> rs.initiate(
... {
... _id : "flame",
... members: [
... { _id : 0, host : "MySQL01:20001" },
... { _id : 1, host : "MySQL01:20002" },
... { _id : 2, host : "MySQL01:20003" }
... ]
... }
... )
{ "ok" : 1 }
flame:OTHER> exit
bye
3. 透過 mongos 連線到分片叢集
[mongo@MySQL01 ~]$ mongos --configdb fire/MySQL01:10001,MySQL01:10002,MySQL01:10003 --port 30000
4. 增加分片到叢集
連線到 mongos,增加上面建立的副本集到叢集中
[mongo@MySQL01 ~]$ mongo localhost:30000
MongoDB shell version v3.4.10
connecting to: localhost:30000
MongoDB server version: 3.4.10
mongos> sh.enableSharding("sale")
{ "ok" : 1 }
在sale資料庫裡面建立一個集合
mongos> use sale
switched to db sale
mongos>
mongos> show collections
mongos> db.dept.insert({"id":10})
WriteResult({ "nInserted" : 1 })
mongos> db.dept.insert({"id":20})
WriteResult({ "nInserted" : 1 })
mongos> db.dept.insert({"id":30})
WriteResult({ "nInserted" : 1 })
為建立好的集合執行範圍分片
mongos> sh.shardCollection("sale.dept", { id : 1 } )
{
"proposedKey" : {
"id" : 1
},
"curIndexes" : [
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "sale.dept"
}
],
"ok" : 0,
"errmsg" : "please create an index that starts with the shard key before sharding."
}
發生報錯,需要對集合的分片欄位建立索引
mongos> db.dept.ensureIndex({"id" : 1})
{
"raw" : {
"flame/MySQL01:20001,MySQL01:20002,MySQL01:20003" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : {
"ts" : Timestamp(1512224813, 1),
"t" : NumberLong(1)
},
"electionId" : ObjectId("7fffffff0000000000000001")
}
}
},
"ok" : 1
}
對集合執行分片
mongos> sh.shardCollection("sale.dept", { id : 1 } )
{ "collectionsharded" : "sale.dept", "ok" : 1 }
建立資料目錄,config目錄供配置服務使用,shard目錄供分片資料節點使用
[root@MySQL01 ~]# su - mongo
[mongo@MySQL01 ~]$ cd /data/db
[mongo@MySQL01 db]$ mkdir config01
[mongo@MySQL01 db]$ mkdir config02
[mongo@MySQL01 db]$ mkdir config03
[mongo@MySQL01 db]$ mkdir shard01
[mongo@MySQL01 db]$ mkdir shard02
[mongo@MySQL01 db]$ mkdir shard03
啟動配置服務副本集的每個節點
[mongo@MySQL01 ~]$ mongod --configsvr --replSet fire --dbpath /data/db/config01 --port 10001
[mongo@MySQL01 ~]$ mongod --configsvr --replSet fire --dbpath /data/db/config02 --port 10002
[mongo@MySQL01 ~]$ mongod --configsvr --replSet fire --dbpath /data/db/config03 --port 10003
連線到配置伺服器,執行初始化操作
[mongo@MySQL01 ~]$ mongo localhost:10001
MongoDB shell version v3.4.10
connecting to: localhost:10001
MongoDB server version: 3.4.10
> rs.initiate(
... {
... _id: "fire",
... configsvr: true,
... members: [
... { _id : 0, host : "MySQL01:10001" },
... { _id : 1, host : "MySQL01:10002" },
... { _id : 2, host : "MySQL01:10003" }
... ]
... }
... )
{ "ok" : 1 }
fire:SECONDARY> exit
bye
2. 建立分片副本集
啟動副本集的每個節點
[mongo@MySQL01 ~]$ mongod --shardsvr --replSet flame --port 20001 --dbpath /data/db/shard01
[mongo@MySQL01 ~]$ mongod --shardsvr --replSet flame --port 20002 --dbpath /data/db/shard02
[mongo@MySQL01 ~]$ mongod --shardsvr --replSet flame --port 20003 --dbpath /data/db/shard03
連線到分片副本集的一個節點,執行副本集初始化操作... {
... _id: "fire",
... configsvr: true,
... members: [
... { _id : 0, host : "MySQL01:10001" },
... { _id : 1, host : "MySQL01:10002" },
... { _id : 2, host : "MySQL01:10003" }
... ]
... }
... )
{ "ok" : 1 }
fire:SECONDARY> exit
bye
啟動副本集的每個節點
[mongo@MySQL01 ~]$ mongod --shardsvr --replSet flame --port 20001 --dbpath /data/db/shard01
[mongo@MySQL01 ~]$ mongod --shardsvr --replSet flame --port 20002 --dbpath /data/db/shard02
[mongo@MySQL01 ~]$ mongod --shardsvr --replSet flame --port 20003 --dbpath /data/db/shard03
[mongo@MySQL01 ~]$ mongo localhost:20001
MongoDB shell version v3.4.10
connecting to: localhost:20001
MongoDB server version: 3.4.10
> rs.initiate(
... {
... _id : "flame",
... members: [
... { _id : 0, host : "MySQL01:20001" },
... { _id : 1, host : "MySQL01:20002" },
... { _id : 2, host : "MySQL01:20003" }
... ]
... }
... )
{ "ok" : 1 }
flame:OTHER> exit
bye
[mongo@MySQL01 ~]$ mongos --configdb fire/MySQL01:10001,MySQL01:10002,MySQL01:10003 --port 30000
4. 增加分片到叢集
連線到 mongos,增加上面建立的副本集到叢集中
[mongo@MySQL01 ~]$ mongo localhost:30000
MongoDB shell version v3.4.10
connecting to: localhost:30000
MongoDB server version: 3.4.10
mongos> sh.addShard( "flame/MySQL01:40001")
{ "shardAdded" : "flame", "ok" : 1 }
開啟指定資料庫的分片
為sale資料庫開啟分片功能{ "shardAdded" : "flame", "ok" : 1 }
mongos> sh.enableSharding("sale")
{ "ok" : 1 }
在sale資料庫裡面建立一個集合
mongos> use sale
switched to db sale
mongos>
mongos> show collections
mongos> db.dept.insert({"id":10})
WriteResult({ "nInserted" : 1 })
mongos> db.dept.insert({"id":20})
WriteResult({ "nInserted" : 1 })
mongos> db.dept.insert({"id":30})
WriteResult({ "nInserted" : 1 })
mongos> sh.shardCollection("sale.dept", { id : 1 } )
{
"proposedKey" : {
"id" : 1
},
"curIndexes" : [
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "sale.dept"
}
],
"ok" : 0,
"errmsg" : "please create an index that starts with the shard key before sharding."
}
mongos> db.dept.ensureIndex({"id" : 1})
{
"raw" : {
"flame/MySQL01:20001,MySQL01:20002,MySQL01:20003" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"$gleStats" : {
"lastOpTime" : {
"ts" : Timestamp(1512224813, 1),
"t" : NumberLong(1)
},
"electionId" : ObjectId("7fffffff0000000000000001")
}
}
},
"ok" : 1
}
mongos> sh.shardCollection("sale.dept", { id : 1 } )
{ "collectionsharded" : "sale.dept", "ok" : 1 }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2148195/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mongodb 分片群集(sharding cluster)MongoDB
- MongoDB Sharding(二) -- 搭建分片叢集MongoDB
- mongodb分片(sharding)搭建、應用及管理MongoDB
- 【MongoDB】分片(sharding)+副本集(replSet)叢集搭建MongoDB
- 搭建 MongoDB分片(sharding) / 分割槽 / 叢集環境MongoDB
- 【Mongodb】Sharding 叢集配置MongoDB
- mongodb複製集(replica sets)+分片(sharding)環境搭建MongoDB
- MongoDB分片MongoDB
- 【Mongodb】 Mongodb sharding 管理之二MongoDB
- 【Mongodb】Mongodb sharding 管理之一MongoDB
- MongoDB 分片管理MongoDB
- MongoDB之分片MongoDB
- mongodb分片搭建MongoDB
- mongodb分片balanceMongoDB
- mongodb 分片叢集建立分片集合MongoDB
- MongoDB分片叢集新增分片(自用)MongoDB
- 利用Mongodb的複製集搭建高可用分片,Replica Sets + Sharding的搭建過程MongoDB
- MongoDB 4.2分片叢集搭建及與3.4分片叢集搭建時的一些異同MongoDB
- 資料庫分片(Database Sharding)詳解資料庫Database
- Sharding-JDBC分片策略詳解(二)JDBC
- mongodb之shard分片MongoDB
- mongodb複製+分片MongoDB
- akka-typed(7) - cluster:sharding, 叢集分片
- MongoDB 分片叢集搭建MongoDB
- MongoDB叢集之分片MongoDB
- 搭建MongoDB分片叢集MongoDB
- MongoDB add sharding -- Just a noteMongoDB
- 【Mongodb】Sharding 手工遷移chunkMongoDB
- CentOS7.2 安裝 MongoDB 3.4CentOSMongoDB
- MongoDB升級--從3.4到3.6MongoDB
- mongodb 分片叢集設定MongoDB
- mongodb分片物理搬遷方案MongoDB
- MongoDB分片叢集常用操作MongoDB
- 【Mongodb】分片加複製集MongoDB
- mongodb簡單分片實驗MongoDB
- MongoDB分片鍵選擇指南MongoDB
- 【Mongodb】分片複製集環境新增新的分片MongoDB
- MongoDB分片叢集chunk的概念MongoDB