端口192.168.115.3
192.168.115.4
1192.168.115.5
- 下载MongoDB软件包版本为4.2.14并安装 rpm -ih --force --nodeps *.rpm
2创建文件夹mkdir -p /opt/local/mongo-cluster/conf
3.在目录里创建配置文件cd /opt/local/mongo-cluster/conf vim mongod_node_config.conf
4,注释掉
#security:
# 分片集群同步密钥文件路径
#authorization: enabled
# 是否启用权限登陆(enabled:启用,disabled:禁用)该节点在成功增加root账号权限之前,请勿开启,否则会无法访问
#keyFile: /opt/local/mongo-cluster/keyfile/mongo.key
5,创建vim mongo_node_data.conf
6,创建vim mongos.conf
7,创建keyfile目录
cd /opt/local/mongo-cluster
mkdir keyfile
openssl rand -base64 756 > mongo.key
chmod 600 mongo.key
mv mongo.key keyfile
(三个主机使用同一份mongo.key)
8,创建节点目录(192.168.115.3)
WORK_DIR=/opt/local/mongo-cluster
mkdir -p $WORK_DIR/nodes/config/node1/data
mkdir -p $WORK_DIR/nodes/mongos/node1
mkdir -p $WORK_DIR/nodes/shard1/node1/data
mkdir -p $WORK_DIR/nodes/shard2/node1/data
mkdir -p $WORK_DIR/nodes/shard3/node1/data
9,启动节点(192.168.115.3)
WORK_DIR=/opt/local/mongo-cluster KEYFILE=$WORK_DIR/keyfile/mongo.key CONFFILE=$WORK_DIR/conf/mongod_node_config.conf MONGOD=mongod
$MONGOD --port 26002 --dbpath $WORK_DIR/nodes/config/node1/data --pidfilepath $WORK_DIR/nodes/config/node1/db.pid --logpath $WORK_DIR/nodes/config/node1/db.log --config $CONFFILE
10,启动完成后在二号三号机,重复一号机的操作,把mongo.key复制到二号三号机的/keyfile目录下
scp/opt/local/mongo_cluster/keyfile/mongo.keyroot@192.168.115.4:/opt/local/mongo)cluster/keyfile
11,启动二号三号机
WORK_DIR=/opt/local/mongo-cluster KEYFILE=$WORK_DIR/keyfile/mongo.key CONFFILE=$WORK_DIR/conf/mongod_node_config.conf MONGOD=mongod
二号:$MONGOD --port 26002 --dbpath $WORK_DIR/nodes/config/node2/data --pidfilepath $WORK_DIR/nodes/config/node2/db.pid --logpath $WORK_DIR/nodes/config/node2/db.log --config $CONFFILE
三号:$MONGOD --port 26002 --dbpath $WORK_DIR/nodes/config/node3/data --pidfilepath $WORK_DIR/nodes/config/node3/db.pid --logpath $WORK_DIR/nodes/config/node3/db.log --config $CONFFILE
12,选择 其中一个congfig进程执行副本初始化
mongo --port 26002 --host 192.168.115.3
> cfg={
_id:"configReplSet",
configsvr: true,
members:[
{_id:0, host:'192.168.115.3:26002'},
{_id:1, host:'192.168.115.4:26002'},
{_id:2, host:'192.168.115.5:26002'}
]};
rs.initiate(cfg);
13,使用主节点添加管理账号
use admin
db.createUser({
user:'admin',pwd:'******',
roles:[
{role:'clusterAdmin',db:'admin'},
{role:'userAdminAnyDatabase',db:'admin'},
{role:'dbAdminAnyDatabase',db:'admin'},
{role:'readWriteAnyDatabase',db:'admin'}
]})