centos7.9使用yum方式安装MongoDB 5.x

1.配置阿里云yum仓库

#vim /etc/yum.repos.d/mongodb-org-5.0.repo 

[mngodb-org]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/5.0/x86_64/
gpgcheck=0
enabled=1

2.安装

yum clean all
yum make cache
yum -y install mongodb-org

3.修改配置文件
配置文件路径:/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: /var/log/mongodb/mongod.log  # 修改为/server/mongo/log/mongod.log

# Where and how to store data.
storage: # 数据存放路径
  dbPath: /var/lib/mongo # 修改为/server/mongo/data
  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 # # 修改为/server/mongo/pid/mongod.pid
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net: # 监控ip和端口
  port: 27017
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

若是需要存储数据,日志等到其他地方,比如统一使用/server/mongo路径,除了修改配置文件外,还需要修改mongod.service文件内容

mkdir -p /server/mongo/{data,log,pid}
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb # 修改为/server/mongo/pid
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb # 修改为/server/mongo/pid
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb # 修改为/server/mongo/pid
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid # 修改为/server/mongo/pid/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings

[Install]
WantedBy=multi-user.target
systemctl daemon-reload

4.启动,停止等操作

启动mongodb :systemctl start mongod.service
停止mongodb :systemctl stop mongod.service
开机启动mongodb :systemctl stop mongod.service

5.设置超级管理员账号信息等

mongo # 开启客户端登陆
use admin # 切换admin数据库
db.createUser(
  {
    user: "adminUser",
    pwd: "adminPass",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

操作完毕后退出

6.修改配置文件,设置访问ip和开启账号验证

vim /etc/mongod.conf
net:
  port: 27017
  bindIp: 0.0.0.0
security:
  authorization: enabled

systemctl restart mongod.service

7.创建普通用户账号密码等

mongo
use admin
db.auth("adminUser","adminPass") # 老版本的是user.auth("adminUser","adminPass")

use osr_web # 切换数据库,有则切换,没有则创建
# 创建数据库关联的用户名和密码,以及读写权限等
db.createUser({
  user: 'root',
  pwd: 'root',
  roles:[{
    role: 'readWrite',
    db: 'osr_web'
  }]
})

8.可以使用软件Navicat Premium 12连接查看
centos7.9使用yum方式安装MongoDB 5.x

上一篇:LINUX 服务器安装nginx redis jdk等步聚


下一篇:MySQL 用 limit 为什么会影响性能?