使用单机部署为副本集(开启oplog.rs)-4.4.13
环境:
OS:Centos 7
db:4.4.13
1.下载相应的版本
https://www.mongodb.com/download-center/community
我这里下载的是mongodb-linux-x86_64-rhel70-4.4.13.tgz
2.创建安装目录
[root@test services]# mkdir -p /usr/local/services
[root@test services]# mkdir -p /home/middle/mongodb/data
[root@test services]# mkdir -p /home/middle/mongodb/log
[root@test services]# mkdir -p /home/middle/mongodb/key
[root@test services]# mkdir -p /home/middle/mongodb/conf
[root@test services]# mkdir -p /home/middle/mongodb/run
3.安装数据库
[root@test soft]# tar -xvf mongodb-linux-x86_64-rhel70-4.4.13.tgz
[root@test soft]# mv mongodb-linux-x86_64-rhel70-4.4.13 /usr/local/services/mongodb
4.创建配置文件
[root@localhost conf]# more mongo_yaml.cnf
net:
bindIp: 192.168.204.53,127.0.0.1
port: 28001
maxIncomingConnections: 10000 ##设置最大连接数
storage:
journal:
enabled: true
dbPath: "/home/middle/mongodb/data"
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 6
systemLog:
destination: file
path: "/home/middle/mongodb/log/mongodb.log"
logAppend: true
logRotate: reopen
processManagement:
fork: true
pidFilePath: "/home/middle/mongodb/run/28001.pid"
replication:
oplogSizeMB: 16384
replSetName: s1
##security:
## keyFile: "/home/middle/mongodb/key/keyfile"
## authorization: "enabled"
5.产生秘钥验证
在其中一个机器上创建秘钥文件,我这里是在192.168.1.105上操作
[root@test key]# cd /home/middle/mongodb/key
[root@test key]# openssl rand -base64 741 >>keyfile
[root@test key]# chmod 700 keyfile
6.启动
/usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf
7.登录并初始化副本集
/usr/local/services/mongodb/bin/mongo 192.168.204.53:28001
use admin
config={_id:'s1',members:[{_id:0,host:'192.168.204.53:28001'}]}
rs.initiate(config)
8.创建管理员用户
[root@localhost bin]# /usr/local/services/mongodb/bin/mongo 192.168.204.53:28001
use admin
db.createUser({user:"root",pwd:"root123",roles:["root"]}); --创建用户
db.auth("root","root123"); --设置用户登陆权限,密码一定要和创建用户时输入的密码相同
show users; --查看创建的用户
9.关闭服务
/usr/local/services/mongodb/bin/mongo localhost:28001
use admin
db.shutdownServer()
10.启用验证参数
##security:
## keyFile: "/home/middle/mongodb/key/keyfile"
## authorization: "enabled"
修改为:
security:
keyFile: "/home/middle/mongodb/key/keyfile"
authorization: "enabled"
11.重新启动
/usr/local/services/mongodb/bin/mongod -f /home/middle/mongodb/conf/mongo.cnf
12.登录
/usr/local/services/mongodb/bin/mongo 192.168.204.53:28001
use admin
db.auth("root","root123");