您好,登錄后才能下訂單哦!
Mongodb分片集群介紹
分片(sharding)是指將數據庫拆分,將其分散在不同的機器上的過程。將數據分散到不同的機器上,不需要功能強大的服務器就可以存儲更多的數據和處理更大的負載。基本思想就是將集合切成小塊,這些塊分散到若干片里,每個片只負責總數據的一部分,最后通過一個均衡器來對各個分片進行均衡(數據遷移)。通過一個名為mongos的路由進程進行操作,mongos知道數據和片的對應關系(通過配置服務器)。大部分使用場景都是解決磁盤空間的問題,對于寫入有可能會變差,查詢則盡量避免跨分片查詢。使用分片的時機:
1,機器的磁盤不夠用了。使用分片解決磁盤空間的問題。
2,單個mongod已經不能滿足寫數據的性能要求。通過分片讓寫壓力分散到各個分片上面,使用分片服務器自身的資源。
3,想把大量數據放到內存里提高性能。和上面一樣,通過分片使用分片服務器自身的資源。
服務器規劃
下載 Mongodb
https://www.mongodb.com/download-center/community
wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.8.tgz
解壓包 Mongodb 包
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.7.tgz -C /usr/local/mongodb
添加環境變量
export MONGO_HOME=/usr/local/mongodb export PATH=$PATH:$MONGO_HOME/bin
集群準備
新建 Mongodb 需要的目錄
在192.168.2.177服務器上操作,以下配置操作均在192.168.2.177上進行
mkdir -p /wdata/mongodb/{data,logs,config,keys} mkdir -p /wdata/mongodb/data/{mongosrv,shard1}
將新建的目錄發送到另外兩臺服務器
for i in 178 180; do scp -r /wdata/mongodb root@192.168.2.$i; done
生成分片集群的key文件
openssl rand -base64 90 -out /wdata/mongodb/keys/keyfile
修改key文件屬性
chmod 600 /wdata/mongodb/keys/keyfile
注意:此處必須修改,否則可能報錯
編輯 mongos.conf 文件
systemLog: destination: file #日志存儲位置 path: /wdata/mongodb/logs/mongos.log logAppend: true processManagement: #fork and run in background fork: true pidFilePath: /wdata/mongodb/data/mongos.pid #端口配置 net: port: 30000 maxIncomingConnections: 500 unixDomainSocket: enabled: true #pathPrefix: /tmp filePermissions: 0700 bindIp: 0.0.0.0 security: keyFile: /wdata/mongodb/keys/keyfile #將 confige server 添加到路由 sharding: configDB: configs/192.168.2.177:21000,192.168.2.178:21000,192.168.2.180:21000
編輯 mongosrv1.conf 文件
systemLog: destination: file logAppend: true path: /wdata/mongodb/logs/mongosrv.log storage: dbPath: /wdata/mongodb/data/mongosrv journal: enabled: true wiredTiger: engineConfig: directoryForIndexes: true processManagement: # fork and run in background fork: true # location of pidfile pidFilePath: /wdata/mongodb/data/mongosrv/mongosrv.pid net: port: 21000 #bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. bindIpAll: true maxIncomingConnections: 65535 unixDomainSocket: enabled: true filePermissions: 0700 security: keyFile: /wdata/mongodb/keys/keyfile authorization: enabled replication: replSetName: configs sharding: clusterRole: configsvr
說明:該文件在此環境需要3個,分布在3臺服務器,這里僅配置1個
編輯 shard1.conf 文件
systemLog: destination: file logAppend: true path: /wdata/mongodb/logs/shard1.log storage: dbPath: /wdata/mongodb/data/shard1 journal: enabled: true wiredTiger: engineConfig: directoryForIndexes: true processManagement: # fork and run in background fork: true pidFilePath: /wdata/mongodb/data/mongosrv/mongosrv.pid # location of pidfile # timeZoneInfo: /usr/share/zoneinfo net: port: 27001 # bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting. bindIpAll: true maxIncomingConnections: 65535 unixDomainSocket: enabled: true # pathPrefix: /tmp/mongod1 filePermissions: 0700 security: keyFile: /wdata/mongodb/keys/keyfile authorization: enabled replication: replSetName: shard1 sharding: clusterRole: shardsvr
說明:shard在一臺服務器上也可以配置多個,建不同的目錄充當分片服務器,看需求而定
將配置文件到其他兩臺服務器
for i in 178 180; do scp -r /wdata/mongodb/config root@192.168.2.$i:/wdata/mongodb/; done
啟動集群
在192.168.1.177上啟動 mongod -f /wdata/mongodb/config/mongosrv1.conf mongod -f /wdata/mongodb/config/shard1.conf mongos -f /wdata/mongodb/config/mongos.conf 在192.168.1.178上啟動 mongod -f /wdata/mongodb/config/mongosrv1.conf mongod -f /wdata/mongodb/config/shard1.conf mongos -f /wdata/mongodb/config/mongos.conf 在192.168.1.180上啟動 mongod -f /wdata/mongodb/config/mongosrv1.conf mongod -f /wdata/mongodb/config/shard1.conf mongos -f /wdata/mongodb/config/mongos.conf
分片集群創建副本集
在192.168.2.177上操作
[root@localhost mongodb]# mongo --port 27001 MongoDB shell version v4.0.7 connecting to: mongodb://127.0.0.1:27001/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("b56157d2-fbc7-4226-aeb4-4de0b79dfcda") } MongoDB server version: 4.0.7 > use admin switched to db admin > config = {_id: 'shard1', members: [{_id: 0, host: '192.168.2.177:27001'},{_id: 1, host: '192.168.2.178:27001'},{_id: 2, host:'192.168.2.180:27001'}]} { "_id" : "shard1", "members" : [ { "_id" : 0, "host" : "192.168.2.177:27001" }, { "_id" : 1, "host" : "192.168.2.178:27001" }, { "_id" : 2, "host" : "192.168.2.180:27001" } ] } > rs.initiate(config) { "ok" : 1 } shard1:PRIMARY> rs.status() { "set" : "shard1", "date" : ISODate("2019-04-03T10:08:16.477Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1554286090, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1554286090, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1554286090, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1554286090, 1), "t" : NumberLong(1) } }, "lastStableCheckpointTimestamp" : Timestamp(1554286040, 2), "members" : [ { "_id" : 0, "name" : "192.168.2.177:27001", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 2657, "optime" : { "ts" : Timestamp(1554286090, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2019-04-03T10:08:10Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1554286040, 1), "electionDate" : ISODate("2019-04-03T10:07:20Z"), "configVersion" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.2.178:27001", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 67, "optime" : { "ts" : Timestamp(1554286090, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1554286090, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2019-04-03T10:08:10Z"), "optimeDurableDate" : ISODate("2019-04-03T10:08:10Z"), "lastHeartbeat" : ISODate("2019-04-03T10:08:16.259Z"), "lastHeartbeatRecv" : ISODate("2019-04-03T10:08:15.440Z"), "pingMs" : NumberLong(6), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.2.177:27001", "syncSourceHost" : "192.168.2.177:27001", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 }, { "_id" : 2, "name" : "192.168.2.180:27001", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 67, "optime" : { "ts" : Timestamp(1554286090, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1554286090, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2019-04-03T10:08:10Z"), "optimeDurableDate" : ISODate("2019-04-03T10:08:10Z"), "lastHeartbeat" : ISODate("2019-04-03T10:08:16.219Z"), "lastHeartbeatRecv" : ISODate("2019-04-03T10:08:15.440Z"), "pingMs" : NumberLong(4), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.2.177:27001", "syncSourceHost" : "192.168.2.177:27001", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 } ], "ok" : 1 } shard1:PRIMARY> exit bye
創建分片集群數據庫和用戶
[root@localhost mongodb]# mongo --port 27001 MongoDB shell version v4.0.7 connecting to: mongodb://127.0.0.1:27001/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("a4c12af4-85e2-49e3-9a02-dd481307bcda") } MongoDB server version: 4.0.7 shard1:PRIMARY> use admin switched to db admin shard1:PRIMARY> db.createUser({user:"admin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]}) Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } shard1:PRIMARY> db.auth("admin","123456") 1 shard1:PRIMARY> use test switched to db test shard1:PRIMARY> db.createUser({user:"root",pwd:"123456",roles:[{role:"dbOwner",db:"test"}]}) Successfully added user: { "user" : "root", "roles" : [ { "role" : "dbOwner", "db" : "test" } ] } shard1:PRIMARY> exit bye
配置 mongosrv 副本集
[root@localhost config]# mongo --port 21000 MongoDB shell version v4.0.7 connecting to: mongodb://127.0.0.1:21000/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("f9896034-d90f-4d52-bc55-51c3dc85aae9") } MongoDB server version: 4.0.7 > config = {_id: 'configs', members: [{_id: 0, host: '192.168.2.177:21000'},{_id: 1, host: '192.168.2.178:21000'},{_id: 2, host: '192.168.2.180:21000'}]} { "_id" : "configs", "members" : [ { "_id" : 0, "host" : "192.168.2.177:21000" }, { "_id" : 1, "host" : "192.168.2.178:21000" }, { "_id" : 2, "host" : "192.168.2.180:21000" } ] } > rs.initiate(config) { "ok" : 1, "$gleStats" : { "lastOpTime" : Timestamp(1554288851, 1), "electionId" : ObjectId("000000000000000000000000") }, "lastCommittedOpTime" : Timestamp(0, 0) } configs:SECONDARY> rs.status(config) { "set" : "configs", "date" : ISODate("2019-04-03T10:54:43.283Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "configsvr" : true, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1554288879, 4), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1554288879, 4), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1554288879, 4), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1554288879, 4), "t" : NumberLong(1) } }, "lastStableCheckpointTimestamp" : Timestamp(1554288864, 1), "members" : [ { "_id" : 0, "name" : "192.168.2.177:21000", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 5454, "optime" : { "ts" : Timestamp(1554288879, 4), "t" : NumberLong(1) }, "optimeDate" : ISODate("2019-04-03T10:54:39Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1554288863, 1), "electionDate" : ISODate("2019-04-03T10:54:23Z"), "configVersion" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.2.178:21000", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 31, "optime" : { "ts" : Timestamp(1554288879, 4), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1554288879, 4), "t" : NumberLong(1) }, "optimeDate" : ISODate("2019-04-03T10:54:39Z"), "optimeDurableDate" : ISODate("2019-04-03T10:54:39Z"), "lastHeartbeat" : ISODate("2019-04-03T10:54:41.362Z"), "lastHeartbeatRecv" : ISODate("2019-04-03T10:54:41.906Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.2.177:21000", "syncSourceHost" : "192.168.2.177:21000", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 }, { "_id" : 2, "name" : "192.168.2.180:21000", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 31, "optime" : { "ts" : Timestamp(1554288879, 4), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1554288879, 4), "t" : NumberLong(1) }, "optimeDate" : ISODate("2019-04-03T10:54:39Z"), "optimeDurableDate" : ISODate("2019-04-03T10:54:39Z"), "lastHeartbeat" : ISODate("2019-04-03T10:54:41.362Z"), "lastHeartbeatRecv" : ISODate("2019-04-03T10:54:41.597Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.2.177:21000", "syncSourceHost" : "192.168.2.177:21000", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1554288879, 4), "$gleStats" : { "lastOpTime" : Timestamp(1554288851, 1), "electionId" : ObjectId("7fffffff0000000000000001") }, "lastCommittedOpTime" : Timestamp(1554288879, 4), "$clusterTime" : { "clusterTime" : Timestamp(1554288879, 4), "signature" : { "hash" : BinData(0,"VVM2Bsa9KiZy8Sew9Oa8CsbDBPU="), "keyId" : NumberLong("6675619852301893642") } } } configs:PRIMARY>
到此mongodb分片集群部署完成后,后面附上 Mongodb 常見操作指令
show dbs #查看所有數據庫 db #查看當前所在的數據庫 sh.status() #查看集群的信息 sh.enableSharding("dba") #對數據庫啟用分片 db.help() #查看幫助 db.account.stats() #查看數據集合的分布情況 db.createUser() #新建用戶 use databasename #進入或者新建數據庫 db.shutdownServer() #關閉數據庫 db.dropDatabase() #刪除數據庫(必須先切換到該數據庫)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。