mongodb 用户 权限 设置 详解
我知道的关系型数据库都是有权限控制的,什么用户能访问什么库,什么表,什么用户可以插入,更新,而有的用户只有读取权限。 例如:mysql安装配置好后,有一个自带的mysql数据库,里面有一张user表,用来存放用户,以及用户权限,而mongodb这个最像关系型的
我知道的关系型数据库都是有权限控制的,什么用户能访问什么库,什么表,什么用户可以插入,更新,而有的用户只有读取权限。
例如:mysql安装配置好后,有一个自带的mysql数据库,里面有一张user表,用来存放用户,以及用户权限,而mongodb这个最像关系型的数据库,有没有这样的表呢。
一,掌握权限,理解下面4条基本上就差不多
1,mongodb是没有默认管理员账号,所以要先添加管理员账号,在开启权限认证。
2,切换到admin数据库,添加的账号才是管理员账号。
3,用户只能在用户所在数据库登录,包括管理员账号。
4,管理员可以管理所有数据库,但是不能直接管理其他数据库,要先在admin数据库认证后才可以。这一点比较怪
二,添加管理员账号
[root@localhost zhangy]# mongo MongoDB shell version: 2.4.6 connecting to: tank > use admin //切换到admin数据库 switched to db admin > show collections; system.indexes system.users //用户表 > db.system.users.find(); //用户表没有数据 > db.addUser('tank','test'); //添加一个管理员账号 { "user" : "tank", "readOnly" : false, "pwd" : "988432606980d0695e4f668f6bbc643a", "_id" : ObjectId("529e5d543b6a4608ac833429") }
三,开启动用户权限认证
[root@localhost zhangy]# vim /etc/mongodb.conf //将auth=true前面的注释拿掉 [root@localhost zhangy]# /etc/init.d/mongod restart //重启生效
四,用户只能在用户所在数据库登录,管理员需要通过admin认证后才能管理其他数据库
[root@localhost zhangy]# mongo MongoDB shell version: 2.4.6 connecting to: tank > show dbs; //显示所有数据库失败,因为还没有认证 Wed Dec 4 06:39:50.925 listDatabases failed:{ "ok" : 0, "errmsg" : "unauthorized" } at src/mongo/shell/mongo.js:46 > db.auth('tank','test'); //认证失败,因为这个用户不属于tank这个数据库 Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } 0 > use admin //切换到admin数据库 switched to db admin > db.auth('tank','test'); //在admin数据库认证成功 1 > use tank; //切换到tank数据库 switched to db tank > show collections; //不会在提示没有权限了 contact system.indexes users
五,添加普通用启
> use tank; switched to db tank > db.addUser('tank1','test'); //为tank数据库添加了一个可读写用户tank1 { "_id" : ObjectId("529e5f8474b4c660718a70f3"), "user" : "tank1", "readOnly" : false, "pwd" : "35dd47abff098f5b4f0b567db8edeac5" } > db.addUser('tank2','test',true); //为tank数据库添加了一个只读用户tank2 { "user" : "tank2", "readOnly" : true, "pwd" : "1792916c544d247538ded52e6df7b887", "_id" : ObjectId("529e67553992b24438d5e315") } > exit //退出 bye [root@localhost zhangy]# mongo MongoDB shell version: 2.4.6 connecting to: tank > db.auth('tank1','test'); //刚添加的用户可以登录。 1
六,php客户端连接
1, 推荐方法一
$mongo = new Mongo(); $db = $mongo->selectDB('tank'); //切换到tank数据库 $db->authenticate("tank3", "test"); //认证 $users= $db->selectCollection("users"); //选取users表 $cursor = $users->find(); //读取数据 foreach ($cursor as $id => $value) { echo "$id: "; print_r($value); echo ""; }
这种方式比较好理解,根命令行下的操作过程差不多。
2,推荐方法二
$mongo = new Mongo("mongodb://tank3:test@127.0.0.1:27017/tank"); //认证用户,这里的数据库,只启认证作用 $db = $mongo->selectDB('tank'); //选取数据库 $users= $db->selectCollection("users"); $cursor = $users->find(); foreach ($cursor as $id => $value) { echo "$id: "; print_r($value); echo ""; }
上面二种方法的不同在于,一个先选数据库在认证,一个先认证在选数据库。
原文地址:mongodb 用户 权限 设置 详解, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

To set up a MongoDB user, follow these steps: 1. Connect to the server and create an administrator user. 2. Create a database to grant users access. 3. Use the createUser command to create a user and specify their role and database access rights. 4. Use the getUsers command to check the created user. 5. Optionally set other permissions or grant users permissions to a specific collection.
