1 准备3台服务器
3台服务器地址为rs0,rs1,rs2
2 在三台服务器中安装mongodb
2.1 添加mongo依赖到yum源中
sudo touch /etc/yum.repos.d/mongodb-org-4.2.repo
sudo vim /etc/yum.repos.d/mongodb-org-4.2.repo(未安装vim:sudo yum install vim)
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
2.2 安装mongodb
安装最新稳定版本
sudo yum install -y mongodb-org
安装指定版本
sudo yum install -y mongodb-org-4.2.0 mongodb-org-server-4.2.0 mongodb-org-shell-4.2.0 mongodb-org-mongos-4.2.0 mongodb-org-tools-4.2.0
2.3 mongodb配置文件
vim /etc/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/log/mongodb/mongo.log
# Where and how to store data.
storage:
dbPath: /data/lib/mongodb
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: rs0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
security:
authorization: enabled
keyFile: /data/keys/mongodb-keyfile
#operationProfiling:
replication:
replSetName: "rs"
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
暂时不开启权限验证
2.4 开启mongodb
sudo mongod --config /etc/mongod.conf // 启动
sudo mongod --config /etc/mongod.conf --shutdown // 暂停
3 集群配置
连接任意一台服务器的mongodb
rs.initiate({
_id : "rs",
members: [
{ _id: 0, host: "rs0:27017", priority: 1 },
{ _id: 1, host: "rs1:27017", priority: 0.5 },
{ _id: 2, host: "rs2:27017", priority: 0.5 }
]
})
4 创建系统用户
use admin
db.createUser(
{
user: "admin",
pwd: "xxx",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
db.createUser(
{
user: "root",
pwd: "xxx",
roles: [ { role: "root", db: "admin" } ]
}
)