mongodb集群搭建
mongodb 分片集群搭建(三台主机 使用yaml格式配置文件)
一 下载
从mongodb官方网站下载,下载前需要测试一个账号,按照提示填写信息就好
https://www.mongodb.com/download-center?initial=true#community 下载包名称: mongodb-linux-x86_64-rhel70-4.0.3.tgz
二 集群环境
主机名 IP地址 shard config mongos
mongo1 10.8.31.13 shard1:27001(主) shard2:27002(备) shard3:27003(备) config:21000(主) mongos:20000
mongo2 10.8.13.233 shard1:27001(备) shard2:27002(主) shard3:27003(备) config:21000(备) mongos:20000
mongo3 10.8.31.7 shard1:27001(备) shard2:27002(备) shard3:27003(主) config:21000(备) mongos:20000
三 安装
把安装文件复制到mongo1机器上进行配置
3.1 解压
把文件解压到/opt目录下
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.3.tgz -C /opt
3.2 重命名文件夹
mv mongodb-linux-x86_64-rhel70-4.0.3 mongodb
3.3 创建目录
mkdir -p /opt/mongodb/mongos/log
mkdir -p /opt/mongodb/{config,shard1,shard2,shard3}/
3.4 创建配置文件
需要创建三个shard配置文件,一个config配置文件,一个mongos配置文件
shard配置文件模板说明
keyFile和auth选项要在集群配置好后,并且添加了验证用户后在启用
参数说明:
dbpath #存放数据目录
logpath #日志数据目录
pidfilepath #pid文件
logappend #日志追加方式存放
oplogSize #设置oplog的大小(MB)
bind_ip #mongodb绑定的ip地址
port #端口
fork #守护进程运行,创建进程
auth #是否开启验证
replSet #Replica Set的名字
maxConns #最大连接数,默认2000
moprealloc #是否禁用数据文件预分配(往往影响性能)
directoryperdb #数据库是否分目录存放
keyFile #节点之间用于验证文件,内容必须保持一致,权限600,仅Replica Set 模式有效
shard1配置文件
vim /opt/mongodb/shard1/shard1.conf
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/shard1/log/shard1.log
storage:
dbPath: /opt/mongodb/shard1/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
processManagement:
fork: true
pidFilePath: /opt/mongodb/shard1/log/shard1.pid
net:
port: 27001
bindIp: 0.0.0.0
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
shard2配置文件
cat /opt/mongodb/shard2/shard2.conf
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/shard2/log/shard2.log
storage:
dbPath: /opt/mongodb/shard2/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
processManagement:
fork: true
pidFilePath: /opt/mongodb/shard2/log/shard2.pid
net:
port: 27002
bindIp: 0.0.0.0
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
shard3配置文件
cat /opt/mongodb/shard3/shard3.conf
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/shard3/log/shard3.log
storage:
dbPath: /opt/mongodb/shard3/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
processManagement:
fork: true
pidFilePath: /opt/mongodb/shard3/log/shard3.pid
net:
port: 27003
bindIp: 0.0.0.0
replication:
replSetName: shard3
sharding:
clusterRole: shardsvr
config配置文件
cat /opt/mongodb/config/configsrv.conf
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/config/log/configsrv.log
storage:
dbPath: /opt/mongodb/config/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/mongodb/config/log/configsrv.pid
# network interfaces
net:
port: 21000
bindIp: 0.0.0.0
replication:
replSetName: configs
sharding:
clusterRole: configsvr
mongos配置文件
cat /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
net:
port: 20000
bindIp: 0.0.0.0
sharding:
configDB: configs/192.168.79.128:21000,192.168.79.129:21000,192.168.79.130:21000
3.5 打包
将/opt/mongodb文件夹打包
cd /opt
tar -zcvf ./mongodb.tar.gz ./mongodb
3.6 拷贝解压
将压缩文件拷贝到mongo2和mongo3两台服务器的/opt目录下,然后解压到/opt目录下
启动shard和config服务
4.1 启动shard服务器
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
4.2 启动config服务器
mongo1机器启动服务
mongod -f /opt/mongodb/config/configsrv.conf
mongo2机器启动服务
mongod -f /opt/mongodb/config/configsrv.conf
mongo3机器启动服务
mongod -f /opt/mongodb/config/configsrv.conf
五. 配置shard和config集群
5.1 配置shard集群
使用mongo1机器连接shard1分片服务器
注意3点:1.默认主机的优先级是1,需要手动指定优先级
2.如果A主机的27001分片角色为仲裁者,那么不能用A主机来设置初始化27001的分片
3,配置完角色分配之后,无论改主机是什么角色,都会显示为secondry,需要退出之后,再次登录才会显示 再次登录也许不会马上修改 需要等待一段时间.
#mongo --port 27001
use admin
>config = {
_id : "shard1",members : [
{_id : 0, host : "10.8.31.13:27001",priority:2},
{_id : 1, host : "10.8.13.233:27001",},
{_id : 2, host : "10.8.13.7:27001",arbiterOnly:true}]}
> rs.initiate(config)
使用mongo2机器连接shard2分片服务器
#mongo --port 27002
>config = {
_id : "shard2",members : [
{_id : 0, host : "10.8.31.13:27002",arbiterOnly:true },
{_id : 1, host : "10.8.13.233:27002",priority:2},
{_id : 2, host : "10.8.13.7:27002",priority:1}]}
> rs.initiate(config)
使用mongo3机器连接shard3分片服务器
#mongo --port 27003
>config = {
_id : "shard3",members : [
{_id : 0, host : "10.8.31.13:27003",priority:1 },
{_id : 1, host : "10.8.13.233:27003",arbiterOnly:true},
{_id : 2, host : "10.8.13.7:27003",priority:2 }]}
> rs.initiate(config)
5.2 配置config集群
mongo1机器启动服务
#mongo --port 21000
>config = {
_id : "configs",
members : [
{_id : 0, host : "10.8.31.13:21000" ,priority:1},
{_id : 1, host : "10.8.13.233:21000"},
{_id : 2, host : "10.8.31.7:21000" }]}
> rs.initiate(config)
六. 启动mongos服务器
mongo1机器启动服务
mongos -f /opt/mongodb/mongos/mongos.conf
mongo2机器启动服务
mongos -f /opt/mongodb/mongos/mongos.conf
mongo3机器启动服务
mongos -f /opt/mongodb/mongos/mongos.conf
七. 配置mongos服务器
串联路由服务器与分配副本集
mongo --port 20000
sh.addShard("shard1/10.8.31.13:27001,10.8.13.233:27001,10.8.31.7:27001")
sh.addShard("shard2/10.8.31.13:27002,10.8.13.233:27002,10.8.31.7:27002")
sh.addShard("shard3/10.8.31.13:27003,10.8.13.233:27003,10.8.31.7:27003")
查看结果
mongo --port 20000
sh.status()
八. 测试分片功能
8.1 启用数据库分片(一定要)
mongos> sh.enableSharding("test")
或者
mongos> use admin
mongos> db.runCommand( { enableSharding: "test"} )
8.2 查看数据库分区情况
如果partitioned 变为 “true”,则表示此数据库是分片数据库
configs> use config
configs> db.databases.find()
8.3 启用集合分片(一定要 不启动只会落在shard1上)
use test
sh.shardCollection("test.kk", { "id": "hashed" })
8.4 创建集合索引
db.kk.createIndex({ "id": "hashed" })
8.5 插入数据
use test
for (var i=1; i<10000; i++) {
db.kk.insert({"id": i, "myName" : "kk"+i, "myDate" : new Date()});
}
8.6 检查结果
db.kk.stats(); 可以看到分片情况
db.printShardingStatus()
集群的账号密码设置
使用mongos登录
mongo --port 20000
use admin
db.createUser(
{
user:"admin",
pwd:"N$nIpms1",
roles:[{role:"root",db:"admin"}]
}
)
killall mongod
killall mongos
生成密钥文件
openssl rand -base64 756 > /data/mongodb/testKeyFile.file
openssl rand -base64 756 > /opt/mongodb/KeyFile.file
然后把这个文件拷贝到另外2台主机上去
修改后的配置文件
shard2
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/shard2/log/shard2.log
storage:
dbPath: /opt/mongodb/shard2/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
processManagement:
fork: true
pidFilePath: /opt/mongodb/shard2/log/shard2.pid
net:
port: 27002
bindIp: 0.0.0.0
replication:
replSetName: shard2
sharding:
clusterRole: shardsvr
security:
authorization: enabled
clusterAuthMode: keyFile
keyFile: /opt/mongodb/KeyFile.file
config
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/config/log/configsrv.log
storage:
dbPath: /opt/mongodb/config/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/mongodb/config/log/configsrv.pid
# network interfaces
net:
port: 21000
bindIp: 0.0.0.0
replication:
replSetName: configs
sharding:
clusterRole: configsvr
security:
authorization: enabled
clusterAuthMode: keyFile
keyFile: /opt/mongodb/KeyFile.file
mongos*
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/mongos/log/mongos.log
processManagement:
fork: true
pidFilePath: /opt/mongodb/mongos/log/mongos.pid
net:
port: 20000
bindIp: 0.0.0.0
sharding:
configDB: configs/192.168.79.128:21000,192.168.79.129:21000,192.168.79.130:21000
security:
clusterAuthMode: keyFile
keyFile: /opt/mongodb/KeyFile.file
注意重新启动的启动顺序是先启动
1
mongod -f /opt/mongodb/config/configsrv.conf
2
mongod -f /opt/mongodb/shard1/shard1.conf
mongod -f /opt/mongodb/shard2/shard2.conf
mongod -f /opt/mongodb/shard3/shard3.conf
3
mongos -f /opt/mongodb/mongos/mongos.conf
如果需要每块shard 加密码。需要把security注释,然后重启,加密码(每个shard上加)最后再去掉注释,再重启