从mongodb官方网站下载,下载前需要测试一个账号,按照提示填写信息就好
Try MongoDB Atlas Products | MongoDB
下载包名称:mongodb-linux-x86_64-rhel70-4.0.3.tgz
主机名 |
IP地址 |
shard |
config |
mongos |
mongo1 |
10.1.0.77 |
shard1:27001(主) shard2:27002(备) shard3:27003(备) |
config:21000(主) |
mongos:20000 |
mongo2 |
10.1.0.78 |
shard1:27001(备) shard2:27002(主) shard3:27003(备) |
config:21000(备) |
mongos:20000 |
mongo3 |
10.1.0.79 |
shard1:27001(备) shard2:27002(备) shard3:27003(主) |
config:21000(备) |
mongos:20000 |
把安装文件复制到mongo1机器上进行配置
把文件解压到/opt目录下
#tar -zxvf mongodb-linux-x86_64-rhel70-4.0.3.tgz -C /opt
#mv mongodb-linux-x86_64-rhel70-4.0.3 mongodb
#mkdir -p /opt/mongodb/mongos/log
#mkdir -p /opt/mongodb/{config,shard1,shard2,shard3}/{data,log}
需要创建三个shard配置文件,一个config配置文件,一个mongos配置文件
shard1配置文件
#vim /opt/mongodb/shard1/shard1.conf
#配置文件内容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/shard1/log/shard1.log
# Where and how to store data.
storage:
dbPath: /opt/mongodb/shard1/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /opt/mongodb/shard1/log/shard1.pid
# network interfaces
net:
port: 27001
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
shard2配置文件
#vim /opt/mongodb/shard2/shard2.conf
#配置文件内容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/shard2/log/shard2.log
# Where and how to store data.
storage:
dbPath: /opt/mongodb/shard2/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /opt/mongodb/shard2/log/shard2.pid
# network interfaces
net:
port: 27002
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
shard3配置文件
#vim /opt/mongodb/shard3/shard3.conf
#配置文件内容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/shard3/log/shard3.log
# Where and how to store data.
storage:
dbPath: /opt/mongodb/shard3/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20
# how the process runs
processManagement:
fork: true
pidFilePath: /opt/mongodb/shard3/log/shard3.pid
# network interfaces
net:
port: 27003
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr
config配置文件
#vim /opt/mongodb/config/configsrv.conf
#配置文件内容
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/config/log/configsrv.log
# Where and how to store data.
storage:
dbPath: /opt/mongodb/config/data
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /opt/mongodb/config/log/configsrv.pid
# network interfaces
net:
port: 21000
bindIp: 0.0.0.0
#operationProfiling:
replication:
replSetName: configs
sharding:
clusterRole: configsvr
mongos配置文件
#vim /opt/mongodb/mongos/mongos.conf
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/mongos/log/mongos.log
processManagement:
fork: true
pidFilePath: /opt/mongodb/mongos/log/mongos.pid
# network interfaces
net:
port: 20000
bindIp: 0.0.0.0
sharding:
configDB: configs/10.1.0.77:21000,10.1.0.78:21000,10.1.0.79:21000
将/opt/mongodb文件夹打包
#cd /opt
#tar -zcvf ./mongodb.tar.gz ./mongodb
将压缩文件拷贝到mongo2和mongo3两台服务器的/opt目录下,然后解压到/opt目录下
mongo1机器启动服务
#mongod -f /opt/mongodb/shard1/shard1.conf
#mongod -f /opt/mongodb/shard2/shard2.conf
#mongod -f /opt/mongodb/shard3/shard3.conf
mongo2机器启动服务
#mongod -f /opt/mongodb/shard1/shard1.conf
#mongod -f /opt/mongodb/shard2/shard2.conf
#mongod -f /opt/mongodb/shard3/shard3.conf
mongo3机器启动服务
#mongod -f /opt/mongodb/shard1/shard1.conf
#mongod -f /opt/mongodb/shard2/shard2.conf
#mongod -f /opt/mongodb/shard3/shard3.conf
mongo1机器启动服务
#mongod -f /opt/mongodb/config/configsrv.conf
mongo2机器启动服务
#mongod -f /opt/mongodb/config/configsrv.conf
mongo3机器启动服务
#mongod -f /opt/mongodb/config/configsrv.conf
使用mongo1机器连接shard1分片服务器
#mongo --port 27001
>config = {
_id : "shard1",members : [
{_id : 0, host : "10.1.0.77:27001" ,priority:1},
{_id : 1, host : "10.1.0.78:27001"},
{_id : 2, host : "10.1.0.79:27001" }]}
> rs.initiate(config)
使用mongo1机器连接shard2分片服务器
#mongo --port 27002
>config = {
_id : "shard2",
members : [{_id : 0, host : "10.1.0.77:27002"},
{_id : 1, host : "10.1.0.78:27002", priority:1},
{_id : 2, host : "10.1.0.79:27002" }]}
> rs.initiate(config)
使用mongo1机器连接shard3分片服务器
#mongo --port 27003
>config = {
_id : "shard3",
members : [
{_id : 0, host : "10.1.0.77:27003"},
{_id : 1, host : "10.1.0.78:27003"},
{_id : 2, host : "10.1.0.79:27003" ,priority:1}]}
> rs.initiate(config)
mongo1机器启动服务
#mongo --port 21000
>config = {
_id : "configs",
members : [
{_id : 0, host : "10.1.0.77:21000" ,priority:1},
{_id : 1, host : "10.1.0.78:21000"},
{_id : 2, host : "10.1.0.79:21000" }]}
> rs.initiate(config)
mongo1机器启动服务
#mongos -f /opt/mongodb/mongos/mongos.conf
mongo2机器启动服务
#mongos -f /opt/mongodb/mongos/mongos.conf
mongo3机器启动服务
#mongos -f /opt/mongodb/mongos/mongos.conf
串联路由服务器与分配副本集
#mongo --port 20000
>sh.addShard("shard1/10.1.0.77:27001,10.1.0.78:27001,10.1.0.79:27001")
>sh.addShard("shard2/10.1.0.77:27002,10.1.0.78:27002,10.1.0.79:27002")
>sh.addShard("shard3/10.1.0.77:27003,10.1.0.78:27003,10.1.0.79:27003")
查看结果
#mongo --port 20000
>sh.status()
mongos> sh.enableSharding("test")
#或者
mongos> use admin
mongos> db.runCommand( { enableSharding: "test"} )
如果partitioned 变为 “true”,则表示此数据库是分片数据库
configs> use config
configs> db.database.find()
{ "_id" : "test", "primary" : "shard2", "partitioned" : true, "version" : { "uuid" : UUID("91f550fd-0f23-4db1-81e2-7895579c83f9"), "lastMod" : 1 } }
>use test
>sh.shardCollection("test.kk", { "id": "hashed" })
>db.kk.createIndex({ "id": "hashed" })
>use test
>for (var i=1; i
db.kk.insert({"id": i, "myName" : "kk"+i, "myDate" : new Date()});
}
>db.printShardingStatus()
首先启动config服务器,然后启动shard服务器,最后启动mongos服务器