MongoDB Service Configuration Series issues

Source: Internet
Author: User
Tags auth mongoclient mongodb
MongoDB Service Configuration series issues To Configure a service by configuring a file:
Dbpath=f:\program\data\mongo\db #数据存储路径
logpath=f:\program\data\mongo\logs\mongo.log #日志存储路劲
logappend= True #日志写入方式: Append
port=27017 #端口
#auth =true #是否认证
bind_ip=0.0.0.0 
serviceName = MongoDB #服务名称
Servicedisplayname = MongoDB
1, install the configuration file:

Execute the following command as an administrator in Cmd under Mongdb's Bin directory:

After you execute this command, you can see the service name MongoDB in ' This computer-management-services and applications-services '. 2, shutdown, start, delete services:

net stop MongoDB #停止一个服务
net start MongoDB #开启一个服务
sc Delete MongoDB #删除一个服务 sc delete "service name"  (if there is a space between the service name, you need to To enclose the quotation mark before and after)
turn on or off username and password login:

Auth = True or Auth = False says whether the connection database requires password authentication, corresponding username and password.

Let's talk about how to turn on password validation:

Admin database is the administrator database, record user information, we need to first create an admin user in this database. The user of that database is then added, modified, or deleted from the other database by this Admin user.

Use admin
db.createuser (
...   ..) {
... ...     User: "* * *",
...     PWD: "* * *",
...     Roles: [{role: "root", DB: "Admin"}] # ' Root ' has the highest privileges.
... ...   }
... ... )

To close the service:

To modify a configuration file:

Dbpath=f:\program\data\mongo\db #数据存储路径
logpath=f:\program\data\mongo\logs\mongo.log #日志存储路劲
logappend =true #日志写入方式: Append
port=27017 #端口
auth=true #把false to True
bind_ip=0.0.0.0 
serviceName = MongoDB # Service name
Servicedisplayname = MongoDB

Reinstall configuration file:

Mongod--install-f F:\Program\DATA\Mongo\mongodb.conf

At this time, into the database to operate the time, found no corresponding permissions, the next landing.

Use admin
db.auth (' username ', ' password ') to use
test1 #切换到数据库test1
#为test1创建一个用户
db.createuser (
...     {
...       User: "TestUser",
...       PWD: "Testpassword",
...       Roles: [{role: "read", DB: "Test1"}]
...     }
to connect MongoDB in Pymongo:
Import Pymongo
client = Pymongo. Mongoclient (' localhost ', connect=true) client[' test1 '].authenticate (' testuser ', ' Testpassword ', mechanism= ')
DEFAULT "). #这一步的作用是登陆. Login successfully returns TRUE.
db = client[' test1 '] #登陆成功后, and then connect to the database.
db.test.find_one () #查询
db.test.insert_one ({' B ': 2}) #插入. #role决定了用户的权限. If you are a read-only user, you cannot do this step. For
I in Db.test.find ():
    print (i)
client[' Test '].logout () #退出数据库.

MongoDB opened the certification, I found that the new database could not be created through Pymongo.

#举例:
client = Pymongo. Mongoclient (' localhost ', connect=false)
db = client[' Jianshu ']
db.table1.insert ({' A ': 1}) #这一步报错, prompting no authentication login. But this is a new database, have not yet written MongoDB, how to authenticate.

This time there are two ways: first, in the MONGO terminal to establish a database, set up a good user, in the Pymongo through the user name and password connection. The second is to turn off authentication. How to turn off authentication:

#关闭MongoDB服务
net stop MongoDB
#修改配置文件
auth = False
#安装配置文件
mongod--install-f F:\Program\DATA\ mongo\mongodb.conf
#开启服务:
net start MongoDB
user Authorization related actions

Under which library the user is created, which library the user belongs to, and only has access to the library.

Mongodb's authorization takes a role-licensing approach, with each role including a set of permissions.

database User Role : Read, ReadWrite;

database Management Role : Dbadmin, Dbowner, useradmin;

Super User Role : root//Here are a few more roles indirectly or directly provide system Superuser access (Dbowner, Useradmin, useradminanydatabase) 1, create a user

Db.createuser (
...     ..) {
...       User: "Test1",
...       PWD: "Test1",
...       Roles: [{role: "Dbowner", DB: "Test1"}]
...     }
2, modify the password
Db.changeuserpassword (' testuser ', ' 1234 ') #把数据库test的密码改为1234
3. Add User Rights
Db.grantrolestouser (  "TestUser",  [    {role: "read", DB: "admin"}  ])
4. Reclaim User Rights
Db.revokerolesfromuser ("TestUser", [    {role: "read", DB: "admin"}  ])
5, delete the user
Db.dropuser ("TestUser")
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.