数据库配置
安装mongoDB,不在赘述,不明白的goole
创建数据库日志文件夹:
在终端输入:sudo mkdir -p /data/db
给予数据库日志文件夹操作权限
在终端输入:sudo chown -R 用户名 /data/db
-
进入mongodb 的 "bin"目录,使用命令“./mongod”启动mongoDB server,启动成功后最后一行应该是端口号,如配图,出现配图就能肯定你的Mongodb已经安装成了
tips: 查找bin路径的时候,有时候会忘记具体路径,因为我是用brew安装的,所以我用
brew info mongodb 查找安装信息的时候能找到其配置文件的路径,进而找到bin路径为/usr/local/bin
tip2017nov22:这里最好在.bash_profile中配置一下,具体可以看我的博客
-
新建终端标签,并输入./mongo 登陆到数据库
-
下面你可以任意操纵这个数据库了是不是很简单啊,如配图
关于用户的一些设置
- Start MongoDB without access control. (No need, if service already running)
$ mongod --port 27017 --dbpath /data/db1
- Connect to the instance
$ mongo --port 27017
- Create the user administrator.
Add a user with the root role. For example, the following creates the user superAdmin on the admin database:
$ use admin
$ db.createUser(
{
user: "superAdmin",
pwd: "admin123",
roles: [ { role: "root", db: "admin" } ]
})
- Re-start the MongoDB instance with access control
Add the security.authorization setting to the config file
ubuntu: $ sudo vi /etc/mongod.conf
osx with brew version: $ sudo vi /usr/local/etc/mongod.conf
It may look like this
systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1
security:
authorization: enabled
Restart mongodb
ubuntu: $ sudo service mongod restart
osx with brew version: $ brew services restart mongodb
- Connect to database instance with superAdmin access
$ mongo --port 27017 -u "superAdmin" -p "admin123" --
authenticationDatabase "admin"
- Create user access (readWrite) for specific database
$ mongo --port 27017 -u "superAdmin" -p "admin123" --authenticationDatabase "admin"
$ use myAppDb
$ db.createUser(
{
user: "myAppDbUser",
pwd: "myApp123",
roles: [ "readWrite"]
})
- Try Connecting to the specific database with limited access
$ mongo --port 27017 -u "myAppDbUser" -p "myApp123" --authenticationDatabase "myAppDb"
More user roles here Build in Roles
Source Mongodb Enable Client Access Control
Your comments and suggestions are welcome :)