數(shù)據(jù)庫(kù)配置
安裝mongoDB,不在贅述代态,不明白的goole
創(chuàng)建數(shù)據(jù)庫(kù)日志文件夾:
在終端輸入:sudo mkdir -p /data/db
給予數(shù)據(jù)庫(kù)日志文件夾操作權(quán)限
在終端輸入:sudo chown -R 用戶(hù)名 /data/db
-
進(jìn)入mongodb 的 "bin"目錄望门,使用命令“./mongod”啟動(dòng)mongoDB server畜挥,啟動(dòng)成功后最后一行應(yīng)該是端口號(hào)甫菠,如配圖挠铲,出現(xiàn)配圖就能肯定你的Mongodb已經(jīng)安裝成了
tips: 查找bin路徑的時(shí)候,有時(shí)候會(huì)忘記具體路徑寂诱,因?yàn)槲沂怯胋rew安裝的拂苹,所以我用
brew info mongodb 查找安裝信息的時(shí)候能找到其配置文件的路徑,進(jìn)而找到bin路徑為/usr/local/bin
tip2017nov22:這里最好在.bash_profile中配置一下痰洒,具體可以看我的博客
-
新建終端標(biāo)簽瓢棒,并輸入./mongo 登陸到數(shù)據(jù)庫(kù)
-
下面你可以任意操縱這個(gè)數(shù)據(jù)庫(kù)了是不是很簡(jiǎn)單啊,如配圖
關(guān)于用戶(hù)的一些設(shè)置
- 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 :)
文章參考自
百度經(jīng)驗(yàn)
國(guó)外博客