一、搭建MongoDB复制集
1.创建目录
for i in 27017 27018 27019
do
mkdir -p /mongodb/$i/conf
mkdir -p /mongodb/$i/data
mkdir -p /mongodb/$i/log
done
2.创建配置文件
cat >>/mongodb/27017/conf/mongod.conf<<'EOF'
systemLog:
destination: file
path: /mongodb/27017/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/27017/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
# cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
net:
port: 27017
replication:
oplogSizeMB: 2048
replSetName: my_repl
EOF
2.1复制配置文件
for i in 27018 27019
do
\cp /mongodb/27017/conf/mongod.conf /mongodb/$i/conf/
done
2.2修改配置文件
for i in 27018 27019
do
sed -i "s#27017#$i#g" /mongodb/$i/conf/mongod.conf
done
3.开启服务
for i in 27017 27018 27019
do
mongod -f /mongodb/$i/conf/mongod.conf
done
4.配置复制集
进入mongo命令 mongo --port 27017
初始化配置
config = {_id: 'my_repl', members: [
{_id: 0, host: '127.0.0.1:27017'},
{_id: 1, host: '127.0.0.1:27018'},
{_id: 2, host: '127.0.0.1:27019'}]
}
rs.initiate(config)
分别登录从节点mongo执行
rs.slaveOk()
二、搭建MongoDB分片集群
1.创建程序所需的目录
for i in 17 18 19 20 21 22 23 24 25 26
do
mkdir -p /mongodb/280$i/conf
mkdir -p /mongodb/280$i/data
mkdir -p /mongodb/280$i/log
done
2.编辑shard集群配置文件
cat > /mongodb/28021/conf/mongod.conf <<'EOF'
systemLog:
destination: file
path: /mongodb/28021/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/28021/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 127.0.0.1
port: 28021
replication:
oplogSizeMB: 2048
replSetName: sh1
sharding:
clusterRole: shardsvr
processManagement:
fork: true
EOF
复制shard集群配置文件
for i in 22 23 24 25 26
do
\cp /mongodb/28021/conf/mongod.conf /mongodb/280$i/conf/
done
修改配置文件端口
for i in 22 23 24 25 26
do
sed -i "s#28021#280$i#g" /mongodb/280$i/conf/mongod.conf
done
修改配置文件复制集名称(replSetName)
for i in 24 25 26
do
sed -i "s#sh1#sh2#g" /mongodb/280$i/conf/mongod.conf
done
启动shard集群
for i in 21 22 23 24 25 26
do
mongod -f /mongodb/280$i/conf/mongod.conf
done
配置复制集1
mongo --host 127.0.0.1 --port 28021 admin
config = {_id: 'sh1', members: [
{_id: 0, host: '127.0.0.1:28021'},
{_id: 1, host: '127.0.0.1:28022'},
{_id: 2, host: '127.0.0.1:28023',"arbiterOnly":true}]
}
# 初始化配置
rs.initiate(config)
配置复制集2
mongo --host 127.0.0.1 --port 28024 admin
config = {_id: 'sh2', members: [
{_id: 0, host: '10.0.0.152:28024'},
{_id: 1, host: '10.0.0.152:28025'},
{_id: 2, host: '10.0.0.152:28026',"arbiterOnly":true}]
}
# 初始化配置
rs.initiate(config)
2.config集群配置
cat > /mongodb/28018/conf/mongod.conf <<'EOF'
systemLog:
destination: file
path: /mongodb/28018/log/mongodb.conf
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/28018/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 127.0.0.1
port: 28018
replication:
oplogSizeMB: 2048
replSetName: configReplSet
sharding:
clusterRole: configsvr
processManagement:
fork: true
EOF
for i in 19 20
do
\cp /mongodb/28018/conf/mongod.conf /mongodb/280$i/conf/
done
for i in 19 20
do
sed -i "s#28018#280$i#g" /mongodb/280$i/conf/mongod.conf
done
启动config server集群
for i in 18 19 20
do
mongod -f /mongodb/280$i/conf/mongod.conf
done
配置config server复制集
mongo --host 127.0.0.1 --port 28018 admin
config = {_id: 'configReplSet', members: [
{_id: 0, host: '127.0.0.1:28018'},
{_id: 1, host: '127.0.0.1:28019'},
{_id: 2, host: '127.0.0.1:28020'}]
}
# 初始化配置
rs.initiate(config)
3.mongos节点配置
cat > /mongodb/28017/conf/mongos.conf <<'EOF'
systemLog:
destination: file
path: /mongodb/28017/log/mongos.log
logAppend: true
net:
bindIp: 127.0.0.1
port: 28017
sharding:
configDB: configReplSet/127.0.0.1:28018,127.0.0.1:28019,127.0.0.1:28020
processManagement:
fork: true
EOF
启动mongos
mongos -f /mongodb/28017/conf/mongos.conf
登陆到mongos
mongo 127.0.0.1:28017/admin
添加分片节点
db.runCommand( { addshard : "sh1/127.0.0.1:28021,127.0.0.1:28022,127.0.0.1:28023",name:"shard1"} )
db.runCommand( { addshard : "sh2/127.0.0.1:28024,127.0.0.1:28025,127.0.0.1:28026",name:"shard2"} )
4.将数据分片
db.runCommand( { enablesharding : "test" } )
mongos> use test
mongos> db.test.ensureIndex( { id: hashed } )
mongos> use admin
mongos> sh.shardCollection( "test.test", { id: "hashed" } )