(我这里是安装在3个节点上,分别为:node1,node2,node3)
1. 安装 mongodb到node1,2,3节点
https://blog.csdn.net/ZZPJackie/article/details/107141382
2. 在node1,node2,node3创建一个mongo目录
mkdir /opt/mongo
mongos, config, shard1,shard2,shard3分别对应三个子目录用来存 data,log, run (后面会创建)
3. 在node1,node2,node3先创建config server
1.分别在多台机器上创建mongo config server对应的目录
mkdir -p /opt/mongo/config/{
log,data,run}
- 分别在多台机器上修改config server的配置文件
cat >> /opt/mongo/config/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: /opt/mongo/config/log/mongod.log
storage:
dbPath: /opt/mongo/config/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /opt/mongo/config/run/mongod.pid
net:
port: 24000
replication:
replSetName: config
sharding:
clusterRole: configsvr
EOF
3. 启动所有的mongo config server服务
mongod --config /opt/mongo/config/mongod.conf
若出现如下错误:
Failed global initialization: BadValue: Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly. locale::facet::_S_create_c_locale name not valid
解决办法:
手动设置变量,在命令行里敲只是临时的重启后没有用了,这需要在环境变量配置文件里添加一行。
vi /etc/profile
export LC_ALL=C
source /etc/profile
再执行 : mongod --config /opt/mongo/config/mongod.conf
运行结果如上图
4, 测试登录node1,2,3中一台的config server, 以创建配置并激活:
(以登录node1中的mongo config server为例) : mongo --port 24000
5. 运行配置:
#创建配置
config = {
_id : "config",
members : [
{
_id : 0, host : "192.168.121.200:24000" },
{
_id : 1, host : "192.168.121.201:24000" },
{
_id : 2, host : "192.168.121.202:24000" }
]
}
注意: _id: config 必须与前面的 config server配置文件中的 replSetName: config 一致. host分别为node1,2,3的地址
6. 初始化副本集配置:
rs.initiate(config)
- 查看分区状态:
rs.status()
4. 给node1,2,3 各创建第一个分片和副本集
- 修改mongo shard1 server的配置文件:
mkdir -p /opt/mongo/shard1/{
log,data,run}
- 分别在多台机器上修改shard1 server的配置文件:
cat >> /opt/mongo/shard1/mongod.conf << EOF
systemLog:
destination: file
logAppend: true
path: