Home >Database >Mysql Tutorial >Mainstream NoSQL database--detailed explanation of MongoDB permission settings
Read this article for:
Those who are not familiar with MongoDB permission settings
MongoDB developers
Other developers interested in MongoDB
People who are interested in technology
MongoDB is a distributed file storage based on The database is currently the mainstream NoSQL database and has been applied to many high-performance large-scale systems. If you have never come into contact with MongoDB before, you can visit the MongoDB official website to quickly understand its main features and application scenarios. If your English level is really limited, you can read the MongoDB Baidu Encyclopedia to learn more. However, I personally recommend reading the English information directly, because sooner or later you will have to read English. Technical Information.
The environment of the example in this article is based on CentOS 7.3 64-bit, and the MongoDB version is 3.2.4.
On the server, execute the yum installation with super user privileges. The command is as follows:
[username@xxx]# su [username] -- username 为你登陆服务器的用户名 [username@xxx]# yum -y install mongodb-server mongodb 如果安装成功,你会得到如下类似的结果: Installed: mongodb-server.x86_64 0:2.6.12-4.el7 Complete!
Found MongoDB installation directory
As can be seen from the above results, mongo is installed under the current /usr/bin, enter this directory.
[username@xxx]# find -name mongo /etc/sysconfig/mongod /usr/bin/mongod
Add configuration mongodb.conf. Place the configuration files, data and log files in the directory /usr/local/mongodb, and create the data and date directories in the /usr/local/mongodb directory: data and logs
[username@xxx]# cd /usr/local [username@xxx]# mkdir mongodb [username@xxx]# cd mongodb [username@xxx]# mkdir data [username@xxx]# mkdir logs [username@xxx]# vi mongodb.conf 在打开的文件中添加以下内容,然后保存: dbpath = /usr/local/mongodb/data logpath = /usr/local/mongodb/logs/mongod.log port = 27017 fork = true nohttpinterface = true
Start the mongo service
[username@xxx]# /usr/bin/mongod --config /usr/local/mongodb/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 9516 child process started successfully, parent exiting
Connect to MongoDB
MongoDB has been installed and started now. And can connect as a client on the server.
[username@xxx]# mongo 127.0.0.1:27017 MongoDB shell version: 2.6.12 connecting to: 127.0.0.1:27017/test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user
Create a root user and grant super administrator permissions (root), super Administrators can manage all libraries under MongoDB as well as operations such as permissions, backups, and clusters. Note that in the code block area below, the bolded part is the MongoDB shell command, and the italicized part is the result after executing the shell command.
use admin switched to db admin db.createUser({user:"root", pwd: "123456", roles: ["root"]}) Successfully added user: { "user" : "root", "roles" : [ "root" ] }
Create an admin user for the admin library and grant administrator rights. <strong> </strong>
use admin switched to db admin db.createUser({user:"admin", pwd:"admin", roles: [{role:"userAdminAnyDatabase", db:"admin"}]}) Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ]}
Create a new library biz, add a user demo and password demo to the library, And grant read, write and administrator rights.
use biz switched to db biz db.createUser({user:"demo", pwd:"demo", roles:["readWrite", "dbAdmin"]}) Successfully added user: { "user" : "demo", "roles" : [ "readWrite", "dbAdmin" ] }
Authenticate the created user, this step is very important. After the authentication is completed, exit the shell.
db.auth('demo', 'demo') 1 # 1 - 表示认证成功,0 - 表示失败 exit bye
Modify the configuration file /usr/local/mongodb/mongodb.conf, add the following content, and save it. auth=true
// 查看mongo进程信息 [username@xxx]# ps -def|grep mongo root 9516 1 0 15:08 ? 00:00:30 /usr/bin/mongod --config mongodb.conf root 9759 9614 0 16:55 pts/0 00:00:00 grep --color=auto mongo // 杀死进程 [username@xxx]# kill -4 9516 // 启动MongoDB [username@xxx]# /usr/bin/mongod --config /usr/local/mongodb/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 9783 child process started successfully, parent exiting
show collections, and a verification failure message will be prompted
not authorized for query on biz.system.namespaces. The corresponding command is as follows:
[username@xxx]# /usr/bin/mongo 127.0.0.1:27017/biz MongoDB shell version: 2.6.12 connecting to: 127.0.0.1:27017/biz show collections 2017-04-15T17:04:20.662+0800 error: { "$err" : "not authorized for query on biz.system.namespaces", "code" : 13 } at src/mongo/shell/query.js:131
show collections to create a collection at the same time users, and insert a piece of test data, and found that the correct results can be obtained.
##{ "_id" : ObjectId("58f1e4aff754011ea2e23238"), "name" : "aa" }<br><em></em>
[username@xxx]# /usr/bin/mongo 127.0.0.1:27017/biz -u demo -p demo MongoDB shell version: 2.6.12 connecting to: 127.0.0.1:27017/biz show collections db.users.insert({name:"aa"}) WriteResult({ "nInserted" : 1 }) db.users.find()
【Related recommendations】
1. Free
free mysql online video tutorialMySQL latest manual tutorialThose things about database designThe above is the detailed content of Mainstream NoSQL database--detailed explanation of MongoDB permission settings. For more information, please follow other related articles on the PHP Chinese website!