search
HomeDatabaseMysql TutorialMongoDB的授权和权限

MongoDB的授权和权限

Jun 07, 2016 pm 02:53 PM
mongodbturn onAuthorizePermissions

MongoDB的授权和权限 1. 在开启MongoDB 服务时不添加任何参数时,可以对数据库任意操作,而且可以远程访问数据库。如果启动的时候指定auth参数,可以对数据库进行用户验证。 www.2cto.com $ ./mongod --auth mongodb.log 开启 ./mongo MongoDB shell version


MongoDB的授权和权限

 

1. 在开启MongoDB 服务时不添加任何参数时,可以对数据库任意操作,而且可以远程访问数据库。如果启动的时候指定—auth参数,可以对数据库进行用户验证。  www.2cto.com  

           $ ./mongod --auth >> mongodb.log & 开启 

           ./mongo 

MongoDB shell version: 1.8.1 

connecting to: test 

>show dbs 

admin   (empty) 

local   (empty) 

 

2. 添加用户 

在刚安装完毕的时候MongoDB都默认有一个admin数据库,而admin.system.users中将会保存比在其它数据库中设置的用户权限更大的用户信息。 

当admin.system.users中一个用户都没有时,即使mongod启动时添加了--auth参数,如果没有在admin数据库中添加用户,此时不进行任何认证还是可以做任何操作,直到在admin.system.users中添加了一个用户。 

 

下面创建数据库tage,并给tage创建用户: 

> use tage   

switched to db tage 

> db.addUser("tage","123") 

         "user" : "tage", 

         "readOnly" : false, 

         "pwd" : "1f66d5c4223029536080d41febe0ec33" 

在admin库中创建root用户: 

> use admin 

switched to db admin 

> db.addUser("root","123456") 

         "user" : "root", 

         "readOnly" : false, 

         "pwd" : "34e5772aa66b703a319641d42a47d696" 

3. 验证用户: 

> db.auth("root","123") 

0                    密码错误,返回0,验证失败 

> db.auth("root","123456")  

1                                            验证成功,返回1 

 

下面试验用户权限设置: 

$ ./mongo       登录时不加用户名与密码 

MongoDB shell version: 1.8.1 

connecting to: test 

> use tage 

switched to db tage 

> db.system.users.find() 

error: { 

         "$err" : "unauthorized db:tage lock type:-1 client:127.0.0.1", 

         "code" : 10057 


 

4. 以上验证说明,登录时不指定用户名与密码,就会报错。下面指定用户与密码 

$ ./mongo -uroot -p123456  指定用户与密码,但是不指定库名 

MongoDB shell version: 1.8.1 

connecting to: test 

Wed Aug  3 21:30:42 uncaught exception: login failed 

exception: login failed 

mongodb登录时默认连接test库,如果登录时不指定库名,就会报错 

 

5. 下面以tage库的用户名登录进行验证: 

$ ./mongo tage -utage -p123 

MongoDB shell version: 1.8.1 

connecting to: tage 

> db.system.users.find()    对所属自己的库进行操作,有权限 

{ "_id" : ObjectId("4e394c696b50a56254359088"), "user" : "tage", "readOnly" : false, "pwd" : "1f66d5c4223029536080d41febe0ec33" } 

> use admin 

switched to db admin 

> db.system.users.find() 对其他库操作,没有权限 

error: { 

         "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1", 

         "code" : 10057 

6. 下面以admin库下的root用户登录进行验证: 

./mongo admin -uroot -p123456 

MongoDB shell version: 1.8.1 

connecting to: admin 

> db.system.users.find() 

{ "_id" : ObjectId("4e394caf6b50a56254359089"), "user" : "root", "readOnly" : false, "pwd" : "34e5772aa66b703a319641d42a47d696" } 

> use tage 

switched to db tage 

> db.system.users.find()   对其他库进行操作,有权限 

{ "_id" : ObjectId("4e394c696b50a56254359088"), "user" : "tage", "readOnly" : false, "pwd" : "1f66d5c4223029536080d41febe0ec33" } 

 

7. mongodb的远程用户连接 

语法结构:mongo –uusername –ppwd ServerIP:port/dbname 

其中port默认为27017 

$ ./mongo  -uroot -p123456 192.168.2.150/admin 

MongoDB shell version: 1.8.1 

connecting to: 192.168.2.150/admin 

> db.system.users.find() 

{ "_id" : ObjectId("4e394caf6b50a56254359089"), "user" : "root", "readOnly" : false, "pwd" : "34e5772aa66b703a319641d42a47d696" }
 

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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.