1.创建目录,用户,软件包下载
所有节点执行
useradd mongo
mkdir /mongodb
chown mongo:mongo /mongodb
su - mongo
vi .bash_profile
export PATH=/mongodb/mongodb-4.2.15/bin/:$PATH
cd /mongodb/
tar -zxvf mongodb-linux-x86_64-rhel70-4.2.15.tgz
mv mongodb-linux-x86_64-rhel70-4.2.15 mongodb-4.2.15
2. config节点:启动,初始化(建立集群),增加删除节点
(1) 启动所有config --每个config节点挨个执行
mkdir /mongodb/config
mongod --bind_ip=0.0.0.0 --replSet=config --dbpath=/mongodb/config --logpath=/mongodb/config/config.log --port 27019 --fork --configsvr --wiredTigerCacheSizeGB 1
vi /mongodb/mongodb.conf
bind_ip=0.0.0.0
replSet=config
dbpath=/mongodb/config
logpath=/mongodb/config/config.log
port=27019
fork=true
configsvr=true
wiredTigerCacheSizeGB=1
mongod -f /mongodb/mongodb.conf
vi /etc/hosts —添加所有config节点
ip 主机名 config1
ip 主机名 configN
(2)初始化建立config集群 —选择任意一个节点执行初始化命令,
mongo --host config1:27019
rs.initiate({
_id:“config”,
“members”:
[
{
“_id”:0,
“host”:“config1:27019”
},
{
“_id”:1,
“host”:“config2:27019”
},
{
“_id”:2,
“host”:“config3:27019”
}
]
});
(3)增删config节点
config:PRIMARY> rs.add(“config2:27019”);
config:PRIMARY> rs.remove(“config2:27019”);
2. shard节点:启动,初始化(建立集群),增加删除节点
(1) 启动所有shard1节点 —shard1集群中,每个节点挨个执行
vi /etc/hosts
ip 主机名 shard11
ip 主机名 shard1N
mkdir /mongodb/data
mkdir /mongodb/shard
vi /mongodb/mongodb.conf
bind_ip=0.0.0.0
replSet=shard1
dbpath=/mongodb/data
logpath=/mongodb/shard/shard1.log
port=27010
fork=true
shardsvr=true
wiredTigerCacheSizeGB=16
mongod -f /mongodb/mongodb.conf
mongod --bind_ip=0.0.0.0 --replSet=shard1 --dbpath=/mongodb/data --logpath=/mongodb/shard/shard1.log --port 27010 --fork --shardsvr --wiredTigerCacheSizeGB 1
(2) 初始化建立shard1分片集群
mongo --host shard11:27010
rs.initiate({
_id:“shard1”,
“members”:
[
{
“_id”:0,
“host”:“shard11:27010”
},
{
“_id”:1,
“host”:“shard12:27010”
},
{
“_id”:2,
“host”:“shard13:27010”
}
]
});
(3) 增加或删除shard1的副本集
mongo --host shard11:27010
shard1:PRIMARY> rs.remove(“ip:27010”);
shard1:PRIMARY> rs.add(“ip:27010”);
—添加shard2,…shardN节点,与上面相同
3. 建立集群连接(mongos连接config,shard)
3.1 启动mongos节点(mongos连接config)
(1) 更改/etc/hosts
mongos1节点添加:
ip 主机名 mongos1
ip 主机名 config1
ip 主机名 configN
mongos2节点添加: —与mongos1原理相同
所有config节点添加:
ip 主机名 mongos1
ip 主机名 mongosN
(2) 每个mongos节点启动,分别指向config集群
mkdir /mongodb/mongos
mongos --bind_ip=0.0.0.0 --logpath=/mongodb/mongos/mongod.log --port 8635 --fork --configdb config/config1:27019,config2:27019,config3:27019
vi /mongodb/mongodb.conf
bind_ip=0.0.0.0
logpath=/mongodb/mongos/mongod.log
port=8635
fork=true
configdb=config/config1:27019,config2:27019,config3:27019
mongos -f /mongodb/mongodb.conf
3.2 通过mongos,添加或删除shard分片集群
(1) /etc/hosts**
所有config节点添加:
ip 主机名 shard11
ip 主机名 shard1N
所有shard1节点添加:
ip 主机名 config1
ip 主机名 configN
(2) 添加分片集群 --其中一个mongos节点执行
mongo --host mongos1:8635
mongos> sh.addShard(“shard1/shard11:27010,shard12:27010,shard13:27010”);
mongos> sh.status()
(3) 删除分片集群
mongos> use admin
mongos> db.runCommand({removeShard:“shard2”});
4./etc/hosts总结
(1)config节点
所有config,mongos,shard节点信息
(2)mongos节点
所有config,当前mongos节点信息,所有shard节点
(3)shard节点
所有config,所有分片集(包含主,备节点)节点信息