安装
centos下使用yum安装
添加 yum源
cd /etc/yum.repos.d/
vi mongodb-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
这里可以修改 gpgcheck=0, 省去gpg验证
yum update (可选操作)
yum install -y mongodb-org
查看mongo安装位置
whereis mongod
启动、停止
systemctl start mongod.service
systemctl stop mongod.service
配置
MongoDB configuration files use the YAML format
vim /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: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
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:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
用户授权
mongodb默认不启用安全授权机制
如何创建数据库管理员
内置的root角色是超级管理员
use admin
switched to db admin
db.createUser({user:"root",pwd:"root123",roles:["root"]})
修改mongod.conf
security:
authorization: enabled
重启mongod,
systemctl restart mongod.service
现在操作会有权限认证提示
show dbs
db.auth("root","root123")
use admin
如何创建数据库用户
给应用程序建个单独的账号,这里简单点给他dbOwner角色,实际只需要读写角色
更多角色
db.createUser({user:"test",pwd:"123456",roles:[ { role: "dbOwner", db: "test" }]})
use admin
db.auth('test','123456')
use test
db.col.insert({title: '1', description: 'MongoDB 是一个 Nosql 数据库'})
db.col.find()
除了db.createUser(),下面几个函数也是常用的:
获得数据库的所有用户权限信息:db.getUsers()
获得某个用户的权限信息:db.getUser()
删除用户:db.dropUser(’’)
更多命令