一、安装
1.下载安装包
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.8.tgz
2.解压
tar -zxvf mongodb-linux-x86_64-rhel70-4.2.8.tgz
3.安装config服务器(分片模式下需要,可1台或3台)
-
准备目录
sudo mkdir -p /usr/local/mongodb/mongodb4.2.14/config/data
-
准备签名文件
openssl rand -base64 756 > /usr/local/mongodb/mongodb4.2.14/keyfile/mongodbKeyFile.file
chmod 400 /usr/local/mongodb/mongodb4.2.14/keyfile/mongodbKeyFile.file>
-
修改配置文件
#mogodb pid文件
pidfilepath=/usr/local/mongodb/mongodb4.2.14/config/configsrv.pid
#数据存储目录
dbpath=/usr/local/mongodb/mongodb4.2.14/config/data
#日志存储目录
logpath=/usr/local/mongodb/mongodb4.2.14/config/configsrv.log
#以追加的方式记录日志
logappend=true
#绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
bind_ip=0.0.0.0
#端口号
port=21000
#以后台方式运行进程
fork=true
#声明这是一个集群的config服务
configsvr=true
#副本集名称
replSet=configs
#设置最大连接数
maxConns=20000
#启用验证
auth=true
#集群的私钥的完整路径,只对于Replica Set 架构有效
keyFile=/usr/local/mongodb/mongodb4.2.14/keyfile/mongodbKeyFile.file
- 启动config服务
/usr/local/mongodb/mongodb4.2.14/bin/mongod -f /usr/local/mongodb/mongodb4.2.14/conf/config.conf
-
初始化(需要先把auth设为false,添加用户后再设为true)
/usr/local/mongodb/mongodb4.2.14/bin/mongo --port 21000
config = {_id : "configs",members : [{_id : 0, host : "192.168.3.20:21000" }]}
rs.initiate(config)
4.安装mongodb服务器(分片模式需要安装多个服务)
-
准备目录
sudo mkdir -p /usr/local/mongodb/mongodb4.2.14/shard1
-
修改配置文件
#日志文件配置
systemLog:
destination: file
path: "/usr/local/mongodb/mongodb4.2.14/shard1/shard1.log"
logAppend: true
#存储配置
storage:
dbPath: "/usr/local/mongodb/mongodb4.2.14/shard1/data"
wiredTiger:
engineConfig:
cacheSizeGB: 6 #限制内存
#进程配置
processManagement:
fork: true
pidFilePath: "/usr/local/mongodb/mongodb4.2.14/shard1/shard1.pid"
net:
bindIp: 0.0.0.0
port: 23001
#分片配置
replication:
replSetName: "shard1" #片名,多台服务器相同表示集群,不同表示分片
sharding:
clusterRole: shardsvr #指定是分片服务器
security:
keyFile: "/usr/local/mongodb/mongodb4.2.14/keyfile/mongodbKeyFile.file"
authorization: enabled
-
启动服务
/usr/local/mongodb/mongodb4.2.14/bin/mongod -f /usr/local/mongodb/mongodb4.2.14/conf/shard1.conf
-
初始化分片(分片模式下需要)
/usr/local/mongodb/mongodb4.2.14/bin/mongo --port 23001
config = {_id : "shard1",members : [{_id : 0, host : "192.168.3.20:27001" }]}
rs.initiate(config)
5.安装Mongos服务器(分片模式下需要)
-
准备目录
sudo mkdir -p /usr/local/mongodb/mongodb4.2.14/mongo
-
修改配置文件
pidfilepath = /usr/local/mongodb/mongodb4.2.14/mongos/mongos.pid
logpath = /usr/local/mongodb/mongodb4.2.14/mongos/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
configdb = configs/127.0.0.1:21000
#设置最大连接数
maxConns=20000
keyFile=/usr/local/mongodb/mongodb4.2.14/keyfile/mongodbKeyFile.file
-
启动Mongos服务
/usr/local/mongodb/mongodb4.2.14/bin/mongos -f /usr/local/mongodb/mongodb4.2.14/conf/mongos.conf
-
串联分片
/usr/local/mongodb/mongodb4.2.14/bin/mongo --port 20000
use admin
sh.addShard("shard1/192.168.3.20:27001,192.168.3.22:27001")
sh.addShard("shard2/192.168.3.20:27002,192.168.3.21:27002")