1、添加官方Yum源
vi /etc/yum.repos.d/mongodb-org-4.4.repo
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.
2、安装MongoDB
yum install -y mongodb-org
3、修改数据存储路径及监听IP地址
# 创建数据存储目录
cd /ssd
mkdir mongodb
chown -R mongod:mongod mongodb
chmod 0755 mongodb
rm -rf /var/run/mongodb
# 修改数据存储目录、PID文件目录及监听IP地址
vi /etc/mongod.conf
# Where and how to store data.
storage:
dbPath: /ssd/mongodb
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /ssd/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,10.255.200.1
# 修改服务配置对应的数据目录信息
vi /usr/lib/systemd/system/mongod.service
# ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
# ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
# ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/ssd/mongodb/mongod.pid
# 启动服务
systemctl daemon-reload
systemctl start mongod
systemctl enable mongod
4、副本集配置
# 生成认证key,复制到集群其它节点并修改权限
cd /ssd/mongodb
openssl rand -base64 756 > replica.key
chmod 400 replica.key
chown -R mongod:mongod replica.key
# 修改配置文件
vi /etc/mongod.conf
security:
authorization: enabled
keyFile: /ssd/mongodb/replica.key
#operationProfiling:
replication:
replSetName: devops
# 重启服务
systemctl restart mongod
# 初始化集群
mongo
rs.initiate(
{
_id : "devops",
members: [
{ _id : 0, host : "10.255.200.1:27017" },
{ _id : 1, host : "10.255.200.2:27017" },
{ _id : 2, host : "10.255.200.3:27017" }
]
}
)
rs.status()
5、创建管理员账号
mongo
use admin
db.createUser(
{
user: "hbtv",
pwd: passwordPrompt(),
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
MongoDB集群搭建(三副本模式)