Home  >  Article  >  Database  >  Mainstream NoSQL database--detailed explanation of MongoDB permission settings

Mainstream NoSQL database--detailed explanation of MongoDB permission settings

零下一度
零下一度Original
2017-05-06 14:57:241486browse

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.

Installation of MongoDB

The environment of the example in this article is based on CentOS 7.3 64-bit, and the MongoDB version is 3.2.4.

  1. On the server, execute the yum installation with super user privileges. The command is as follows:

  2. [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!
  3. Found MongoDB installation directory

    As can be seen from the above results, mongo is installed under the current /usr/bin, enter this directory.

  4. [username@xxx]# find -name mongo
    /etc/sysconfig/mongod
    /usr/bin/mongod
  5. 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

  6. [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
  7. Start the mongo service

  8. [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
  9. Connect to MongoDB

    MongoDB has been installed and started now. And can connect as a client on the server.

  10. [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

MongoDB permission settings

  1. 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.

  2. use admin
    switched to db admin
    db.createUser({user:"root", pwd: "123456", roles: ["root"]})
    Successfully added user: { "user" : "root", "roles" : [ "root" ] }
  3. Create an admin user for the admin library and grant administrator rights.
    <strong> </strong>

  4. 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"
    }
    ]}
  5. Create a new library biz, add a user demo and password demo to the library, And grant read, write and administrator rights.

  6. use biz
    switched to db biz
    db.createUser({user:"demo", pwd:"demo", roles:["readWrite", "dbAdmin"]})
    Successfully added user: { "user" : "demo", "roles" : [ "readWrite", "dbAdmin" ] }
  7. Authenticate the created user, this step is very important. After the authentication is completed, exit the shell.

  8. db.auth(&#39;demo&#39;, &#39;demo&#39;)
    1
     # 1 - 表示认证成功,0 - 表示失败
    exit
    bye

Enable verification and restart the MongoDB service.

  1. Modify the configuration file /usr/local/mongodb/mongodb.conf, add the following content, and save it.
    auth=true

  2. ##Kill and restart the service


  3. // 查看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
Verify permission setting successfully

  1. Try to anonymously connect to the biz database. After connecting, execute the command

    show collections, and a verification failure message will be prompted not authorized for query on biz.system.namespaces. The corresponding command is as follows:

  2. [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
  3. Use the added user and password to connect to the biz library, and execute

    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()
  4. Permissions set successfully!
  5. Summary

In the MongoDB permission setting process, after adding a user, you must perform corresponding user verification, otherwise the user's permissions will be invalid and take effect. This article only covers a few simple permissions. For more built-in permissions, please refer to the official documentation. If you encounter any problems during the configuration process, you can leave a message to me for discussion and communication.

【Related recommendations】

1. Free

free mysql online video tutorial

2.

MySQL latest manual tutorial

3.

Those things about database design

The 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn