目录
环境基础
服务规划
1.创建所有目录与文件
1.1创建根shardcluster目录存放所有分片分片服务文件
1.2创建ConfigServer(服务器)文件
1.2创建shard(分片)集群文件
1.3创建mongos(路由服务)文件
1.4解压安装mongodb
1.5发送所有分片服务文件到nosql02与nosql03
1.6修改nosql02与nosql03的文件
2.启动服务
2.1启动ConfigServer
2.2启动Shard
?2.3启动mongos
?3.分片测试与基本操作
3.1分片测试
3.2安全认证
环境基础
Linux建议使用CentOS7
配置签需要关闭防火墙,两句一起执行就永久关闭了
#临时关闭(重启恢复)
systemctl stop firewalld
#开机自动关闭(重启生效)
service iptables stop
修改主机名为nosql01、nosql02、nosql03
写入主机名解析即/etc/hosts文件写入IP与对应主机名
生成安全认证文件
为了系统安全建议创建user_mongo用户,同时注意用户的切换,要保证生成的文件拥有者所属组均为user_mongo
服务规划
虚拟机 | 服务器 | IP地址 | Shard1 | Shard2 | Shard3 | mongos | Config?Server | | | 名称 | 名称 | NoSQL_1 | nosql01 | 192.168.159.131 | 27018 | 27020 | 27019 | 27021 | 27022 | | | | | 主节点 | 仲裁节点 | 副节点 | 主节点 | NoSQL_2 | nosql02 | 192.168.121.132 | 27019 | 27018 | 27020 | 27021 | 27022 | | | | | 副节点 | 主节点 | 仲裁节点 | 副节点 | NoSQL_3 | nosql03 | 192.168.121.133 | 27020 | 27019 | 27018 | | 27022 | | | | | 仲裁节点 | 副节点 | 主节点 | 副节点 |
1.创建所有目录与文件
1.1创建根shardcluster目录存放所有分片分片服务文件
mkdir -p /opt/servers/mongodb_demo/shardcluster
1.2创建ConfigServer(服务器)文件
mkdir -p /opt/servers/mongodb_demo/shardcluster/configServer/configFile
mkdir -p /opt/servers/mongodb_demo/shardcluster/configServer/data
mkdir -p /opt/servers/mongodb_demo/shardcluster/configServer/logs
touch /opt/servers/mongodb_demo/shardcluster/configServer/logs/config_server.log
touch /opt/servers/mongodb_demo/shardcluster/configServer/configFile/mongodb_config.conf
编辑mongodb_config.conf文件内容
内容中安全认证部分先为注释状态配置到安全认证步骤时在将注释去除
#编辑mongodb_config.conf
vi /opt/servers/mongodb_demo/shardcluster/configServer/configFile/mongodb_config.conf
#mongodb_config.conf文件内容
dbpath=/opt/servers/mongodb_demo/shardcluster/configServer/data
logpath=/opt/servers/mongodb_demo/shardcluster/configServer/logs/config_server.log
port=27022
bind_ip=nosql01
logappend=true
fork=true
maxConns=5000
replSet=configs
configsvr=true
#安全认证
#keyFile=/opt/servers/mongodb_demo/replicaset/key/keyfile
#auth=true
1.2创建shard(分片)集群文件
mkdir -p /opt/servers/mongodb_demo/shardcluster/shard/configFile
mkdir -p /opt/servers/mongodb_demo/shardcluster/shard/shard1_data
mkdir -p /opt/servers/mongodb_demo/shardcluster/shard/shard2_data
mkdir -p /opt/servers/mongodb_demo/shardcluster/shard/shard3_data
mkdir -p /opt/servers/mongodb_demo/shardcluster/shard/logs
touch /opt/servers/mongodb_demo/shardcluster/shard/logs/shard1.log
touch /opt/servers/mongodb_demo/shardcluster/shard/logs/shard1.log
touch /opt/servers/mongodb_demo/shardcluster/shard/logs/shard1.log
touch /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard1.conf
touch /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard2.conf
touch /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard3.conf
编辑mongodb_shard1.conf内容
#编辑mongodb_shard1.conf
vi /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard1.conf
#mongodb_shard1.conf内容
dbpath=/opt/servers/mongodb_demo/shardcluster/shard/shard1_data
logpath=/opt/servers/mongodb_demo/shardcluster/shard/logs/shard1.log
port=27018
logappend=true
fork=true
maxConns=5000
bind_ip=nosql01
shardsvr=true
replSet=shard1
#安全认证
#keyFile=/opt/servers/mongodb_demo/replicaset/key/keyfile
#auth=true
编辑mongodb_shard2.conf内容
#编辑mongodb_shard2.conf
vi /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard2.conf
#mongodb_shard2.conf内容
dbpath=/opt/servers/mongodb_demo/shardcluster/shard/shard2_data
logpath=/opt/servers/mongodb_demo/shardcluster/shard/logs/shard2.log
port=27020
logappend=true
fork=true
maxConns=5000
bind_ip=nosql01
shardsvr=true
replSet=shard2
#安全认证
#keyFile=/opt/servers/mongodb_demo/replicaset/key/keyfile
#auth=true
编辑mongodb_shard3.conf
#编辑mongodb_shard3.conf
vi /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard3.conf
#mongodb_shard3.conf内容
dbpath=/opt/servers/mongodb_demo/shardcluster/shard/shard3_data
logpath=/opt/servers/mongodb_demo/shardcluster/shard/logs/shard3.log
port=27019
logappend=true
fork=true
maxConns=5000
bind_ip=nosql01
shardsvr=true
replSet=shard3
#安全认证
#keyFile=/opt/servers/mongodb_demo/replicaset/key/keyfile
#auth=true
1.3创建mongos(路由服务)文件
mkdir -p /opt/servers/mongodb_demo/shardcluster/mongos/configFile
mkdir -p /opt/servers/mongodb_demo/shardcluster/mongos/logs
touch /opt/servers/mongodb_demo/shardcluster/mongos/logs/mongos.log
touch /opt/servers/mongodb_demo/shardcluster/mongos/configFile/mongodb_mongos.conf
编辑mongodb_mongos.conf内容
#编辑mongodb_mongos.conf
vi /opt/servers/mongodb_demo/shardcluster/mongos/configFile/mongodb_mongos.conf
#mongodb_mongos.conf文件内容
logpath=/opt/servers/mongodb_demo/shardcluster/mongos/logs/mongos.log
logappend=true
port=27021
bind_ip=nosql01
fork=true
configdb=configs/nosql01:27022,nosql02:27022,nosql03:27022
maxConns=20000
#安全认证
#keyFile=/opt/servers/mongodb_demo/replicaset/key/keyfile
1.4解压安装mongodb
?注:压缩包需要通过软件Xftp或CRT中sudo rz或其它方法上传至Linux,名称以使用版本为主
#解压安装包
tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.2.2.tgz -C /opt/servers/mongodb_demo/shardcluster/
#进入解压文件夹所在目录
cd /opt/servers/mongodb_demo/shardcluster/
#修改解压文件夹的名称
mv mongodb-linux-x86_64-rhel70-4.2.2/ mongodb
1.5发送所有分片服务文件到nosql02与nosql03
scp -r /opt/servers/mongodb_demo/shardcluster user_mongo@nosql02:/opt/servers/mongodb_demo/
scp -r /opt/servers/mongodb_demo/shardcluster user_mongo@nosql03:/opt/servers/mongodb_demo/
1.6修改nosql02与nosql03的文件
这里主要是修改相应文件中参数相应代码不在赘述
nosql02 修改nosql02 mongodb_config.conf 中主机名 修改nosql02 mongodb_shard1.conf 中主机名并将端口号改为:27019 修改nosql02 mongodb_shard2.conf 中主机名并将端口号改为:27018 修改nosql02 mongodb_shard3.conf 中主机名并将端口号改为:27020 修改nosql02 mongodb_mongos.conf 中主机名
nosql03 修改nosql03 mongodb_config.conf 中主机名 修改nosql03 mongodb_shard1.conf 中主机名并将端口号改为:27020 修改nosql03 mongodb_shard2.conf 中主机名并将端口号改为:27019 修改nosql03 mongodb_shard3.conf 中主机名并将端口号改为:27018 删除mongos文件或修改其中主机名
2.启动服务
注:启动服务、登录客户端要在解压安装的.../mongodb/bin目录下进行,或将此bin目录添加至环境变量启动即可去掉“./”在任何目录下启动
2.1启动ConfigServer
在nosql01、nosql02、nosql03上执行?
./mongod -f /opt/servers/mongodb_demo/shardcluster/configServer/configFile/mongodb_config.conf
登录nosql01ConfigServer客户端
./mongo --host nosql01 --port 27022
初始化副本集
rs.initiate()
添加副节点
rs.add('nosql02:27022')
rs.add('nosql03:27022')
2.2启动Shard
?在nosql01、nosql02、nosql03上执行
./mongod -f /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard1.conf
./mongod -f /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard2.conf
./mongod -f /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard3.conf
?登录客户端添加节点
#登录Shard1客户端
./mongo --host nosql01 --port 27018
#初始化副本集
rs.initiate()
#添加副节点
rs.add('nosql02:27019')
#添加仲裁节点
rs.addArb('nosql03:27020')
#登录Shard2客户端
./mongo --host nosql02 --port 27018
#初始化副本集
rs.initiate()
#添加副节点
rs.add('nosql03:27019')
#添加仲裁节点
rs.addArb('nosql01:27020')
#登录Shard3客户端
./mongo --host nosql03 --port 27018
#初始化副本集
rs.initiate()
#添加副节点
rs.add('nosql01:27019')
#添加仲裁节点
rs.addArb('nosql02:27020')
?2.3启动mongos
在nosql01、nosql02、或nosql03上执行
./mongos -f /opt/servers/mongodb_demo/shardcluster/mongos/configFile/mongodb_mongos.conf
?登录mongos客户配置分片集群
#登录mongos客户端
./mongo --host nosql01 --port 27021
#切换数据库
use gateway
#添加分片集群至路由
sh.addShard("shard1/nosql01:27018,nosql02:27019,nosql03:27020")
sh.addShard("shard2/nosql01:27020,nosql02:27018,nosql03:27019")
sh.addShard("shard3/nosql01:27019,nosql02:27020,nosql03:27018")
?3.分片测试与基本操作
3.1分片测试
登录mongos客户端
./mongo --host nosql01 --port 27021
设置chunk
#切换到数据库config
use config
#设置块大小为1M
db.settings.save({"_id":"chunksize","value":1})
模拟写入数据
#切换至school数据库
use school
#写入测试数据
for(i=1;i<=50000;i++){db.user.insert({"id":1,"name":"jack"+i})}
对数据库进行分片操作
#切换到数据库gateway
use gateway
#实现数据库school分片
sh.enableSharding("school")
对集合进行分片操作
#切换到数据库school
use school
#以id作为索引
db.user.createIndex({"id":1})
#切换到数据库gateway
use gateway
#以id作为分片键对集合user进行分片
sh.shardCollection("school.user",{"id":1})
查看分片信息
sh.status()
3.2安全认证
创建用户
#切换至数据库admin
use admin
#创建全局用户itcastAdmin
db.createUser({user:"itcastAdmin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"},{role:"readWriteAnyDatabase",db:"admin"},{role:"dbAdminAnyDatabase",db:"admin"}]})
#创建root权限用户
db.createUser({user:"admin",pwd:"123456",roles:[{role:"root",db:"admin"}]})
关闭服务
注:这里关闭的顺序要留意。ps -ef | grep ...为查询进程号的命令,查询后需通过(kill -2 进程号)关闭。
#关闭分片集群
#首先要关闭平衡器(Balancer)
use gateway
sh.stopBalancer()
#关闭mongos
ps -ef |grep mongodb_mongos
#关闭shard
ps -ef |grep mongodb_shard
#关闭Config Server
ps -ef |grep mongodb_config
修改所有conf文件添加安全认证部分内容(这里为去除“#”注释符)
vi /opt/servers/mongodb_demo/shardcluster/configServer/configFile/mongodb_config.conf
vi /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard1.conf
vi /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard2.conf
vi /opt/servers/mongodb_demo/shardcluster/shard/configFile/mongodb_shard3.conf
vi /opt/servers/mongodb_demo/shardcluster/mongos/configFile/mongodb_mongos.conf
启动分片集群
顺序为:
启动Config Server->启动shard->启动mongos? 命令这里不在赘述
登录mongos客户端测试安全认证
#登录mongos客户端
./mongo --host nosql01 --port 27021
#尝试读取文件
use school
db.user.find().limit(10)
#全局登录用户
use admin
db.auth("itcastAdmin","123456")
#再次读取文件
use school
db.user.find().limit(10)
#尝试查看状态
sh.status()
#root用户登录
use admin
db.auth("admin","123456")
#再次查看状态
sh.status()
|