您的位置:首页 > 数据库 > Mongodb

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

2016-10-31 13:38 447 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐