安装MongoDB 3.2 并通过账户密码来访问指定数据库

1. 安装: https://docs.mongodb.com/v3.0/tutorial/install-mongodb-on-red-hat/

在/etc/yum.repos.d/目录下创建mongodb-org-3.0.repo, 内容如下:

[mongodb-org-3.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/
gpgcheck=0
enabled=1
2. yum 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: /opt/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /opt/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

# network interfaces
net:
  port: 27017
#  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

#security:
#  authorization: "enabled"

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

4. 启动mongodb:

mongod --config /etc/mongod.conf

5. 控制台运行mongo,进入命令行。

> show dbs

> use test_db

db.createUser(  

  { user: "test_user",  

    customData: {description:"superuser"},

    pwd: "test_password",  

    roles: [ 

        { role: "readWrite", db: "test_db" },

        { role: "dbAdmin", db: "test_db" } 

     ]  

  }  

)

6.修改配置,把下面2行注释去掉

#security:
#  authorization: "enabled"


7. 重启mongodb,然后可以通过 test_user / test_password 访问数据库


8.连接URL:

mongodb://test_user:test_password@127.00.1:27017/test_db



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值