ITPub博客

首页 > 数据库 > > centos 7 MongoDB 4.2.0安装配置(rpm方式)

centos 7 MongoDB 4.2.0安装配置(rpm方式)

作者:浪花小雨滴 时间:2020-05-12 11:49:59 0 删除 编辑

1.安装MongoDB

  1. 下载安装包,repo.mongodb.org/yum/re
  2. 安装 rpm -ivh mongodb-org-server-4.2.0-1.el7.x86_64.rpm
  3. 修改配置文件,修改数据目录和日志目录,配置文件位于/etc/mongod.conf, 注意这里的security的authorization暂时关闭
# 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: /home/nextclass/mongodb/log/mongod.log

# Where and how to store data.
storage:
  # 更改数据目录
  dbPath: /home/nextclass/mongodb/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
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
# 修改端口
net:
  port: 17027
  # 因为127.0.0.1只能在本机访问,改后通过本机的IP就能访问服务
  bindIp: 0.0.0.0  # Enter 127.0.0.1,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

# 开启权限校验
#security:
#  authorization: enabled
  
#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

4. 修改文件夹权限,mongodb的启动脚本默认的是mongod用户,mongod组,所以需要给对应的数据目录和日志目录加上对应的权限(必须加,不然启不起来,一直报没有权限)

chown mongod:mongod /home/nextclass/mongdb -R

chown mongod:mongod /home/nextclass

不修改权限就会报这个错

● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2019-09-06 16:38:06 CST; 7s ago
     Docs: https://docs.mongodb.org/manual
  Process: 18990 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=1/FAILURE)
  Process: 18988 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 18985 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 18981 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 11508 (code=exited, status=0/SUCCESS)

Sep 06 16:38:06 mysql.mgr2 systemd[1]: Starting MongoDB Database Server...
Sep 06 16:38:06 mysql.mgr2 mongod[18990]: about to fork child process, waiting until server is ready for connections.
Sep 06 16:38:06 mysql.mgr2 mongod[18990]: forked process: 18993
Sep 06 16:38:06 mysql.mgr2 mongod[18990]: ERROR: child process failed, exited with error number 1
Sep 06 16:38:06 mysql.mgr2 mongod[18990]: To see additional information in this output, start without the "--fork" option.
Sep 06 16:38:06 mysql.mgr2 systemd[1]: mongod.service: control process exited, code=exited status=1
Sep 06 16:38:06 mysql.mgr2 systemd[1]: Failed to start MongoDB Database Server.
Sep 06 16:38:06 mysql.mgr2 systemd[1]: Unit mongod.service entered failed state.
Sep 06 16:38:06 mysql.mgr2 systemd[1]: mongod.service failed.

5. 启动mongodb, systemctl start mongod,查看状态systemctl status mongod


2. 配置权限

  1. 下载Robo 3T可视化工具,download-test.robomongo.org
  2. 无密码登录

3. 创建超级管理员

# 切换到admin数据库
use admin
# 创建root用户赋予root角色
db.createUser({user:"root", pwd: "123456", roles: ["root"]})
# 验证创建的用户
db.auth("root", "123456") 

4. 创建自己数据的管理员

# 切换/创建自己的数据库
use bigdata
# dbOwner:表示在当前数据库中可以执行任意操作
# 这里创建的clancy用户只对mall这个数据库有用
# 当然也可以在admin数据库创建用户,然后登录的时候需要admin数据库认证
# 这里我们是在bigdata数据库创建的用户,所以后面登录的时候认证的数据库是bigdata
db.createUser({user: "nextclass", pwd: "123456", roles: [{ role: "dbOwner", db: "bigdata" }]})

创建用户成功后,再用创建的账号登录

角色说明:

内建的角色
数据库用户角色:read、readWrite;
数据库管理角色:dbAdmin、dbOwner、userAdmin;
集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
备份恢复角色:backup、restore;
所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
超级用户角色:root // 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
内部角色:__system

角色说明:
Read:允许用户读取指定数据库
readWrite:允许用户读写指定数据库
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root:只在admin数据库中可用。超级账号,超级权限

5. 修改配置文件,开启权限认证,然后重启MongoDB,systemctl restart mongod

# 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: /home/nextclass/mongodb/log/mongod.log

# Where and how to store data.
storage:
  # 更改数据目录
  dbPath: /home/nextclass/mongodb/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
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
# 修改端口
net:
  port: 17027
  # 因为127.0.0.1只能在本机访问,改后通过本机的IP就能访问服务
  bindIp: 0.0.0.0  # Enter 127.0.0.1,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

# 开启权限校验
security:
  authorization: enabled
  
#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:Configuration File Options# 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: /home/nextclass/mongodb/log/mongod.log

# Where and how to store data.
storage:
  # 更改数据目录
  dbPath: /home/nextclass/mongodb/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
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
# 修改端口
net:
  port: 17027
  # 因为127.0.0.1只能在本机访问,改后通过本机的IP就能访问服务
  bindIp: 0.0.0.0  # Enter 127.0.0.1,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

# 开启权限校验
security:
  authorization: enabled
  
#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

6. 用第4步创建的账号在Robo 3T登录验证

7. 在spring boot中使用MongoDB

mongodb:  
    host: 39.97.98.20  
    port: 17027  
    # 连接bigdata数据库
    database: bigdata  
    username: nextclass  
    password: 123456
    # 如果你是在admin数据库中创建的用户,这里你就要在admin中去认证,上文中我们在bigdata创建
    authentication-database: bigdata

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3124899/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-05-16

  • 博文量
    4259
  • 访问量
    40027