mongodb创建用户和密码
創(chuàng)建數(shù)據(jù)庫文件夾與日志文件
mkdir /home/mongodb/data
mkdir /home/mongodb/logs
touch(創(chuàng)建文件)
3. 啟動(dòng)mongodb
cd到mongodb目錄下的bin文件夾啟動(dòng)mongodb
//下面這個(gè)是需要權(quán)限的登錄方式, 用戶連接需要用戶名和密碼
./mongod --dbpath=/home/mongodb/data?--logpath=/home/mongodb/logs --logappend ?--auth ?--port=27017 --fork
//這個(gè)是不需要密碼的
./mongod --dbpath=/home/mongodb/data?--logpath=/home/mongodb/logs --logappend ?--port=27017 --fork
或者
在mongodb下面創(chuàng)建文件 my.cnf
touch my.cnf
里面內(nèi)容為:
.
port=27017
dbpath=/home/mongodb/data
logpath=/home/mongodb/logs/mongodb.log
pidfilepath=/home/mongodb/mongo.pid
fork=true
logappend=true
#auth=true
保存完后,回到bin目錄下,輸入
?./mongod --config /home/mongodb/my.cnf
。
5,查看進(jìn)程。
?
netstat -lanp |grep 27017
6,創(chuàng)建用戶
進(jìn)入bin目錄下, ?輸入./mongo 127.0.0.1:27017 連接到mongodb中,
輸入use test (MongoDB?use DATABASE_NAME?用于創(chuàng)建數(shù)據(jù)庫。該命令將創(chuàng)建一個(gè)新的數(shù)據(jù)庫,如果它不存在,否則將返回現(xiàn)有的數(shù)據(jù)庫。)
創(chuàng)建用戶名,密碼和角色。
?db.createUser({user:"testuse",pwd:"1qaz@wsx",roles:[{role:"readWrite",db:"picadb"}]})
至此,用戶和密碼已創(chuàng)建完畢。
7,重新啟動(dòng)mongodb。查看mongodb。
修改剛才的my.cnf文件,在內(nèi)容中添加 auth=true。 保存。
重啟mongodb,再登錄到mongodb中,
?
[root@iZ253cglmsxZ bin]# ./mongo 127.0.0.1:27017
MongoDB shell version: 3.2.9
connecting to: 127.0.0.1:27017/test
> use test
switched to db test
?
> show collections
2016-09-05T16:46:13.013+0800 E QUERY? ? [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on picadb to execute command { listCollections: 1.0, filter: {} }",
"code" : 13
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:773:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:785:19
DB.prototype.getCollectionNames@src/mongo/shell/db.js:796:16
shellHelper.show@src/mongo/shell/utils.js:754:9
shellHelper@src/mongo/shell/utils.js:651:15
@(shellhelp2):1:1
?
> db.auth("testuser","1qaz@wsx")
1
> show collections
movie
查看成功表明 mongodb用戶名和密碼創(chuàng)建成功。
轉(zhuǎn)載于:https://www.cnblogs.com/liqing1009/p/6266176.html
總結(jié)
以上是生活随笔為你收集整理的mongodb创建用户和密码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cordova使用Gradle构建下载m
- 下一篇: Sedawk笔记之awk篇:快速了解Aw