Centos7 安装 MongoDB (3.6.6版本)并设置用户密码

配置yum仓库

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

[mongodb-org]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

之前代码有空格,现重新修改

[mongodb-org]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/  
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

 

 

配置好yum仓库之后,使用yum list 进行加载

yum list

使用yum仓库安装monggodb

yum  install -y mongodb-org

修改mongodb的配置文件

vim /etc/mongod.conf

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /data/mongodb/data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  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: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.


#security:
#  authorization: enabled

开启mongodb数据库服务

systemctl start mongod.service

systemctl enable mongod.service


netstat -ntap | grep 27017
tcp        0      0 0.0.0.0:27017           0.0.0.0:*               LISTEN      17001/mongod 

 

 

若要开启账号密码验证,需先登录mongodb新建用户

执行sql,其中root为系统管理员,boos的权限可以读写创建数据库

db.createUser(
   {
     user: "root",
     pwd: "blm123",
     
     roles: [{"role":"root","db":"admin"}],
    /* All built-in Roles 
     Database User Roles: read|readWrite
     Database Admin Roles: dbAdmin|dbOwner|userAdmin
     Cluster Admin Roles: clusterAdmin|clusterManager|clusterMonitor|hostManager
     Backup and Restoration Roles: backup|restore
     All-Database Roles: readAnyDatabase|readWriteAnyDatabase|userAdminAnyDatabase|dbAdminAnyDatabase
     Superuser Roles: root 
    */
    
    // authenticationRestrictions: [ {
    //     clientSource: ["192.168.0.0"],
    //     serverAddress: ["xxx.xxx.xxx.xxx"]
    //  } ],

    //mechanisms: [ "", ... ], 

    //passwordDigestor: ""
   }
)


db.createUser(
   {
     user: "boos",
     pwd: "12345",
     
     roles: [{"role":"readWriteAnyDatabase","db":"admin"}],
    /* All built-in Roles 
     Database User Roles: read|readWrite
     Database Admin Roles: dbAdmin|dbOwner|userAdmin
     Cluster Admin Roles: clusterAdmin|clusterManager|clusterMonitor|hostManager
     Backup and Restoration Roles: backup|restore
     All-Database Roles: readAnyDatabase|readWriteAnyDatabase|userAdminAnyDatabase|dbAdminAnyDatabase
     Superuser Roles: root 
    */
    
    // authenticationRestrictions: [ {
    //     clientSource: ["192.168.0.0"],
    //     serverAddress: ["xxx.xxx.xxx.xxx"]
    //  } ],

    //mechanisms: [ "", ... ], 

    //passwordDigestor: ""
   }
)

成功后可下图

Centos7 安装 MongoDB (3.6.6版本)并设置用户密码_第1张图片

然后开启安全校验

修改mongodb的配置文件,注意空格

vim /etc/mongod.conf

security:
  authorization: enabled

重启服务

systemctl restart mongod.service

你可能感兴趣的:(mongodb,数据库,运维的一点心得)