红联Linux门户
Linux帮助

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

发布时间:2016-11-01 10:27:40来源:linux网站作者:昆山人在上海
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,进入命令行。
> shows db
> 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.重启mongodb,然后可以通过 test_user / test_password 访问数据库
 
7.连接URL:
mongodb://test_user:test_password@127.00.1:27017/test_db
 
本文永久更新地址:http://www.linuxdiyf.com/linux/25619.html