配置MongoDB分片群集以及如何进行分片管理?

配置MongoDB分片群集以及如何进行分片管理?

前言

可以看我之前的博客
链接: https://blog.csdn.net/m0_47219942/article/details/108552702.
链接: https://blog.csdn.net/m0_47219942/article/details/108552982.

实操

配置复制集群

tar zxvf mongodb-linux-x86_64-3.2.1.tgz -C /opt/
cd /opt
[root@localhost opt]# ls
mongodb-linux-x86_64-3.2.1  rh
[root@localhost opt]# mv mongodb-linux-x86_64-3.2.1/ /usr/local/mongodb
[root@localhost opt]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  mongodb  sbin  share  src
[root@localhost local]# cd mongodb/
[root@localhost mongodb]# ls
bin  GNU-AGPL-3.0  MPL-2  README  THIRD-PARTY-NOTICES
[root@localhost mongodb]# cd bin/
[root@localhost bin]# ls
bsondump  mongod     mongoexport  mongoimport  mongoperf     mongos     mongotop
mongo     mongodump  mongofiles   mongooplog   mongorestore  mongostat
[root@localhost bin]# ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongo  '做路径优化'
[root@localhost bin]# ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongod
[root@localhost bin]# mkdir -p /data/mongodb/mongodb{
     1,2,3,4}
[root@localhost bin]# cd /data/mongodb/
[root@localhost mongodb]# ls
mongodb1  mongodb2  mongodb3  mongodb4
[root@localhost mongodb]# mkdir logs
[root@localhost mongodb]# cd logs/
[root@localhost logs]# touch mongodb{
     1,2,3,4}.log
[root@localhost logs]# chmod 777 *.log
[root@localhost logs]# ls
mongodb1.log  mongodb2.log  mongodb3.log  mongodb4.log
'进程和打开文件数量进行放大,优化一下,避免缓存卡死'
[root@localhost logs]# ulimit -u 25000   
[root@localhost logs]# ulimit -n 25000
[root@localhost logs]# cd /usr/local/mongodb/bin/

配置服务器

[root@localhost bin]# vim mongodb1.conf
port=37017
dbpath=/data/mongodb/mongodb1
logpath=/data/mongodb/logs/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
configsvr=true

某节点内存不足时,从其他节点分配内存

sysctl -w vm.zone_reclaim_mode=0
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

[root@localhost bin]# ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongo  做路径优化
[root@localhost bin]# ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongod

[root@localhost bin]# mongod -f mongodb1.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 13225
child process started successfully, parent exiting

配置节点服务器

[root@localhost bin]# cp -p mongodb1.conf mongodb2.conf 
[root@localhost bin]# vim mongodb2.conf 
port=47017   '修改端口'
dbpath=/data/mongodb/mongodb2  ‘修改’
logpath=/data/mongodb/logs/mongodb2.log  ‘修改’
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true    ‘修改为分片服务器’
[root@localhost bin]# cp -p mongodb2.conf mongodb3.conf 
[root@localhost bin]# vim mongodb3.conf 
port=47018   
dbpath=/data/mongodb/mongodb3 
logpath=/data/mongodb/logs/mongodb3.log  
shardsvr=true 
[root@localhost bin]# mongod -f mongodb2.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 13354
child process started successfully, parent exiting
[root@localhost bin]# mongod -f mongodb3.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 13373
child process started successfully, parent exiting
[root@localhost bin]# ./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 20.0.0.51:37017 --chunkSize 1
mongo
mongos> show dbs
mongos> sh.status()  'shards下为空,没有分片服务器'
mongos> sh.addShard("20.0.0.51:47017")	'添加分片服务器'
mongos> sh.addShard("20.0.0.51:47018")
mongos> sh.status()

shards:
	{
       " id" : "shard0000""host" : "20.0.0.51:47017" }
	{
       "id" : "shard0001""host" : "20.0.0.51:47018" }

分片功能

./mongoimport -d kevin -c users --file /opt/testdb.txt

mongos> use kevin
mongos> for(var i=1;i<=10000;i++)db.users.insert({
     "id":i,"name":"jack"+i})

mongos> show dbs
config 0.031GB
kevin  0.078GB

mongos> use kevin
mongos> show collections
system.indexes
users


mongos> db.users.find().limit(5)
mongos> sh.status()        '查看数据库分片信息'
databases:
	{
     "id":"kevin""primary" : "shard0000",
"partitioned" : false }

mongos>sh. enableSharding("kevin")  '启用数据库分片'

mongos> sh.status()
databases:
	{
     "id" : "kevin"" primary" : "shard0000",
partitioned": true }

mongos>db.users.createIndex( {
     "id":1})   '对users表创建索引'
mongos> sh.shardCollection("kevin.users",{
     "id":1})    '表分片'
mongos> sh.status()

分片管理

mongos> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mongos> use kevin
switched to db kevin
mongos> for(var i=1;i<=50000;i++)db.users2.insert({
     "id":i,"name":"tom"+1})
WriteResult({
      "nInserted" : 1 })
mongos> db.users2.count()
50000
mongos> db.users2.find().limit(10)  '查看前10行'
mongos> db.users2.createIndex({
     "id":1})  '创建索引'
{
     
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
mongos> sh.shardCollection("kevin.users2",{
     "id":1})
mongos> db.users.stats()

添加标签

mongos> sh.addShardTag("shard0000","sales00")
mongos> sh.addShardTag("shard0001","sales01")

连接配置服务器

mongo --port 37017

configsvr> use config

configsvr> show collections/show tables

...
collections
chunks
databases
...

configsvr> db.chunks.findOne()	查看分片

configsvr> db.collections.find()

configsvr>db.databases.find()

'当添加新的分片,原有数据量会怎么样?'
[root@localhost bin]# cp -p mongodb2.conf mongodb4.conf 
[root@localhost bin]# vim mongodb4.conf 
port=47018   修改
dbpath=/data/mongodb/mongodb4 修改
logpath=/data/mongodb/logs/mongodb4.log  修改
[root@localhost bin]# mongod -f mongodb4.conf
[root@localhost bin]# mongo

mongos> sh.addShard("20.0.0.51:47019")
mongos> sh.status()

chunks:
	shard0000 4
	shard0001 4
	shard0002 3
'当你再添加一个新的分片,会自动的将原有数据量均匀分片'

删除分片节点

mongos> use admin
mongos> db.runCommand({
     "removeshard":"20.0.0.51:47019"})
'当删除后,又会自动的平均分配'

你可能感兴趣的:(缓存,mongodb,云计算)