1、Config Server:配置服务器存储集群的元数据和配置设置。
2、Shard:数据分片,每个分片包含分片数据的一个子集。 每个分片都可以部署为一个副本集。
3、Mongos:mongos 充当查询路由器,提供客户端应用程序和分片集群之间的接口。 从 MongoDB 4.4 开始,mongos 可以支持对冲读取以最小化延迟。
openssl rand -base64 128 > /home/app/keyFile chmod 600 /home/app/keyFile
1、创建config server数据目录
mkdir -p /home/app/mongo44-config-0/db mkdir -p /home/app/mongo44-config-0/configdb chmod -R 777 /home/app/mongo44-config-0/db chmod -R 777 /home/app/mongo44-config-0/configdb
2、初始化config server配置文件
cat > /home/app/mongo44-config-0/configdb/mongod.conf <<EOF net: port: 21000 bindIpAll: true #security: #keyFile: /data/configdb/keyFile #authorization: enabled #replication: # replSetName: rs-conf sharding: clusterRole: configsvr archiveMovedChunks: false storage: wiredTiger: engineConfig: cacheSizeGB: 0.5 EOF
cp /home/app/keyFile /home/app/mongo44-config-0/configdb/keyFile
3、启动config server
docker run -d \ --network host \ --restart=always \ -v /home/app/mongo44-config-0/db:/data/db \ -v /home/app/mongo44-config-0/configdb:/data/configdb \ -v /etc/localtime:/etc/localtime:ro \ --name mongo44-config-0 mongo:4.4 mongod \ -f /data/configdb/mongod.conf
4、配置管理员账号
#进入容器 docker exec -it mongo44-config-0 /bin/bash #进入mongo mongo --port 21000 #切换到admin数据库 use admin #创建管理员账号 db.createUser( { user: "admin", pwd: "123456", roles: [ { role: "root", db: "admin" } ] } );
5、修改配置文件,重启mongo
net: port: 21000 bindIpAll: true security: keyFile: /data/configdb/keyFile authorization: enabled replication: replSetName: rs-conf sharding: clusterRole: configsvr archiveMovedChunks: false storage: wiredTiger: engineConfig: cacheSizeGB: 0.5
6、初始化config server的复制集
#进入容器 docker exec -it mongo44-config-0 /bin/bash #进入mongo mongo --port 21000 #切换到admin数据库 use admin #创建管理员账号 db.auth('admin', '123456') #初始confi server复制集配置 rs.initiate( { _id: "rs-conf", members: [ { _id : 0, host : "192.168.2.127:21000"} ] } )
1、创建Shard-0数据目录
mkdir -p /home/app/mongo44-shard-0/db mkdir -p /home/app/mongo44-shard-0/configdb chmod -R 777 /home/app/mongo44-shard-0/db chmod -R 777 /home/app/mongo44-shard-0/configdb
2、初始化Shard-0配置文件
cat > /home/app/mongo44-shard-0/configdb/mongod.conf <<EOF net: port: 22000 bindIpAll: true #security: #keyFile: /data/configdb/keyFile #authorization: enabled #replication: # replSetName: rs-shard-0 sharding: clusterRole: shardsvr archiveMovedChunks: false storage: wiredTiger: engineConfig: cacheSizeGB: 0.5 EOF
cp /home/app/keyFile /home/app/mongo44-shard-0/configdb/keyFile
3、启动shard-0
docker run -d \ --network host \ --restart=always \ -v /home/app/mongo44-shard-0/db:/data/db \ -v /home/app/mongo44-shard-0/configdb:/data/configdb \ -v /etc/localtime:/etc/localtime:ro \ --name mongo44-shard-0 mongo:4.4 mongod \ -f /data/configdb/mongod.conf
4、配置管理员账号
#进入容器 docker exec -it mongo44-shard-0 /bin/bash #进入mongo mongo --port 22000 #切换到admin数据库 use admin #创建管理员账号 db.createUser( { user: "admin", pwd: "123456", roles: [ { role: "root", db: "admin" } ] } );
5、修改配置文件,重启mongo
net: port: 22000 bindIpAll: true security: keyFile: /data/configdb/keyFile authorization: enabled replication: replSetName: rs-shard-0 sharding: clusterRole: shardsvr archiveMovedChunks: false storage: wiredTiger: engineConfig: cacheSizeGB: 0.5
6、初始化shard-0复制集
#进入容器 docker exec -it mongo44-shard-0 /bin/bash #进入mongo mongo --port 22000 #切换到admin数据库 use admin #创建管理员账号 db.auth('admin', '123456') #初始confi server复制集配置 rs.initiate( { _id: "rs-shard-0", members: [ { _id : 0, host : "192.168.2.127:22000"} ] } )
1、创建shard-1数据目录
mkdir -p /home/app/mongo44-shard-1/db mkdir -p /home/app/mongo44-shard-1/configdb chmod -R 777 /home/app/mongo44-shard-1/db chmod -R 777 /home/app/mongo44-shard-1/configdb
2、初始化shard-1配置文件
cat > /home/app/mongo44-shard-1/configdb/mongod.conf <<EOF net: port: 23000 bindIpAll: true #security: #keyFile: /data/configdb/keyFile #authorization: enabled #replication: # replSetName: rs-shard-1 sharding: clusterRole: shardsvr archiveMovedChunks: false storage: wiredTiger: engineConfig: cacheSizeGB: 0.5 EOF
cp /home/app/keyFile /home/app/mongo44-shard-1/configdb/keyFile
3、启动shard-1
docker run -d \ --network host \ --restart=always \ -v /home/app/mongo44-shard-1/db:/data/db \ -v /home/app/mongo44-shard-1/configdb:/data/configdb \ -v /etc/localtime:/etc/localtime:ro \ --name mongo44-shard-1 mongo:4.4 mongod \ -f /data/configdb/mongod.conf
4、配置管理员账号
#进入容器 docker exec -it mongo44-shard-1 /bin/bash #进入mongo mongo --port 23000 #切换到admin数据库 use admin #创建管理员账号 db.createUser( { user: "admin", pwd: "123456", roles: [ { role: "root", db: "admin" } ] } );
5、修改配置文件,重启mongo
net: port: 23000 bindIpAll: true security: keyFile: /data/configdb/keyFile authorization: enabled replication: replSetName: rs-shard-1 sharding: clusterRole: shardsvr archiveMovedChunks: false storage: wiredTiger: engineConfig: cacheSizeGB: 0.5
6、初始化shard-1复制集
#进入容器 docker exec -it mongo44-shard-1 /bin/bash #进入mongo mongo --port 23000 #切换到admin数据库 use admin #创建管理员账号 db.auth('admin', '123456') #初始confi server复制集配置 rs.initiate( { _id: "rs-shard-1", members: [ { _id : 0, host : "192.168.2.127:23000"} ] } )
1、创建mongos-0数据目录
mkdir -p /home/app/mongos-0/db mkdir -p /home/app/mongos-0/configdb chmod -R 777 /home/app/mongos-0/db chmod -R 777 /home/app/mongos-0/configdb
2、初始化mongos-0配置文件
cat > /home/app/mongos-0/configdb/mongos.conf <<EOF net: port: 24000 bindIpAll: true security: keyFile: /data/configdb/keyFile sharding: configDB: rs-conf/192.168.2.127:21000 EOF
cp /home/app/keyFile /home/app/mongos-0/configdb/keyFile #如果启动mongos报bad file,执行 chown 999 /home/app/mongos-0/configdb/keyFile
3、启动mongos-0
docker run -d \ --network host \ --restart=always \ -v /home/app/mongos-0/db:/data/db \ -v /home/app/mongos-0/configdb:/data/configdb \ -v /etc/localtime:/etc/localtime:ro \ --name mongos-0 mongo:4.4 mongod \ -f /data/configdb/mongos.conf
4、登录mongos-0,进行分片配置
#进入容器 docker exec -it mongos-0 /bin/bash #进入mongo mongo --port 24000 #切换到admin数据库 use admin #登录 db.auth('admin','123456') #添加分片 sh.addShard("rs-shard-0/192.168.2.127:22000"); sh.addShard("rs-shard-1/192.168.2.127:23000"); #对指定库进行分片 sh.enableSharding("testdb") # 使用hash分片键 sh.shardCollection('testdb.collection', {'field':'hashed'})
5、分片相关命令,在mongos上使用
# 启用数据库分片: sh.enableSharding("<database>") # 使用hash分片键 sh.shardCollection('db.collection', {'field':'hashed'}) # 使用递增分片键 sh.shardCollection('db.collection', { field: 1}) # 查看分片是否成功 db.collection.stats().sharded # 查看数据分布 db.collection.getShardDistribution()