OperationFailure:未授权跟踪执行命令
OperationFailure: not authorized on tracking to execute command
我做了以下事情
-- `sudo apt-get install mongodb-org`
-- go to `etc/mongod.conf` change bindIp to: `0.0.0.0`
-- sudo mkdir /data/db
-- start without auth to create user
`sudo mongod --port 27017 --dbpath /data/db`
-- open shell with : mongo --port 27017
```
> use admin
> db.createUser( { user: "useradmin", pwd: "mypassword", roles: [ { role: "root", db: "admin" } ] } )
```
-- Restart with auth required(ctrl+c the above mongod process):
`sudo mongod --auth --port 27017 --dbpath /data/db'
-- To open shell(ctrl+c above mongo shell):
`mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin`
我的mongoengine_settings.py
```PYTHON
from mongoengine import connect
DATABASE = 'tracking'
USERNAME = 'useradmin'
PASSWORD = 'mypassword'
HOST = 'mongodb://localhost/tracking'
PORT = 27017
connect(DATABASE,
username=USERNAME,
password=PASSWORD,
host=HOST,
port=PORT
)
```
现在,当我尝试使用 mongoengine
批量插入一些数据时,如果我没有启用 --auth 则工作正常,否则它会抛出以下错误:
OperationFailure(u'command SON([(\'createIndexes\', u\'order\'), (\'indexes\', [{\'unique\': True, \'background\': False, \'sparse\': False, \'key\': SON([(\'order_id\', 1)]), \'name\': u\'order_id_1\'}])]) on namespace tracking.$cmd failed: not authorized on tracking to execute command { createIndexes: "order", indexes: [ { unique: true, background: false, sparse: false, key: { order_id: 1 }, name: "order_id_1" } ] }',)
我做错了什么?
MongoDB 用户是在特定数据库而不是实例级别创建的。创建用户后,可以为不同的数据库授予不同的角色。创建用户的数据库称为 authentication database
因为用户名不是唯一的(只有用户名和身份验证数据库的组合才是),您可以在不同的数据库中使用不同的角色和密码创建两个具有相同名称的用户。这也意味着连接时需要指定身份验证数据库以及用户名和密码。
这就是为什么在 admin
数据库中创建 useradmin
用户后,您需要 运行 此命令:
mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin
将 MongoDB shell 连接到默认数据库 test
。
如果您没有明确指定身份验证数据库,则 MongoDB 假定您要连接的数据库也是身份验证数据库。所以像这样连接到管理数据库是可行的:
mongo --port 27017 -u useradmin -p mypassword admin
这三个命令实际上是相同的,都将 return 一个 "Authentication failed" 错误:
mongo --port 27017 -u useradmin -p mypassword
mongo --port 27017 -u useradmin -p my password test
mongo --port 27017 -u useradmin -p my password test --authenticationDatabase test
从 Python 连接,如果您使用 MongoClient and pass it a complete MongoDB URI, the connection string can include optional parameters. One of the options is authSource (the the database name associated with the user’s credentials) which is obviously what you need: connection options。
您的 URI 将如下所示:
MdbURI = "mongodb://useradmin:mypassword@localhost:27017/tracking?authSource=admin"
client = MongoClient(MdbURI)
下面是连接和验证 pymongo 的方法:
from pymongo import MongoClient
# MongoDB connection info
hostname = '10.20.30.40'
port = 27017
username = 'adminUserName'
password = 'secret'
databaseName = 'someDB'
# connect with authentication
client = MongoClient(hostname, port)
db = client[databaseName]
db.authenticate(username, password)
我做了以下事情
-- `sudo apt-get install mongodb-org`
-- go to `etc/mongod.conf` change bindIp to: `0.0.0.0`
-- sudo mkdir /data/db
-- start without auth to create user
`sudo mongod --port 27017 --dbpath /data/db`
-- open shell with : mongo --port 27017
```
> use admin
> db.createUser( { user: "useradmin", pwd: "mypassword", roles: [ { role: "root", db: "admin" } ] } )
```
-- Restart with auth required(ctrl+c the above mongod process):
`sudo mongod --auth --port 27017 --dbpath /data/db'
-- To open shell(ctrl+c above mongo shell):
`mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin`
我的mongoengine_settings.py
```PYTHON
from mongoengine import connect
DATABASE = 'tracking'
USERNAME = 'useradmin'
PASSWORD = 'mypassword'
HOST = 'mongodb://localhost/tracking'
PORT = 27017
connect(DATABASE,
username=USERNAME,
password=PASSWORD,
host=HOST,
port=PORT
)
```
现在,当我尝试使用 mongoengine
批量插入一些数据时,如果我没有启用 --auth 则工作正常,否则它会抛出以下错误:
OperationFailure(u'command SON([(\'createIndexes\', u\'order\'), (\'indexes\', [{\'unique\': True, \'background\': False, \'sparse\': False, \'key\': SON([(\'order_id\', 1)]), \'name\': u\'order_id_1\'}])]) on namespace tracking.$cmd failed: not authorized on tracking to execute command { createIndexes: "order", indexes: [ { unique: true, background: false, sparse: false, key: { order_id: 1 }, name: "order_id_1" } ] }',)
我做错了什么?
MongoDB 用户是在特定数据库而不是实例级别创建的。创建用户后,可以为不同的数据库授予不同的角色。创建用户的数据库称为 authentication database
因为用户名不是唯一的(只有用户名和身份验证数据库的组合才是),您可以在不同的数据库中使用不同的角色和密码创建两个具有相同名称的用户。这也意味着连接时需要指定身份验证数据库以及用户名和密码。
这就是为什么在 admin
数据库中创建 useradmin
用户后,您需要 运行 此命令:
mongo --port 27017 -u useradmin -p mypassword --authenticationDatabase admin
将 MongoDB shell 连接到默认数据库 test
。
如果您没有明确指定身份验证数据库,则 MongoDB 假定您要连接的数据库也是身份验证数据库。所以像这样连接到管理数据库是可行的:
mongo --port 27017 -u useradmin -p mypassword admin
这三个命令实际上是相同的,都将 return 一个 "Authentication failed" 错误:
mongo --port 27017 -u useradmin -p mypassword
mongo --port 27017 -u useradmin -p my password test
mongo --port 27017 -u useradmin -p my password test --authenticationDatabase test
从 Python 连接,如果您使用 MongoClient and pass it a complete MongoDB URI, the connection string can include optional parameters. One of the options is authSource (the the database name associated with the user’s credentials) which is obviously what you need: connection options。
您的 URI 将如下所示:
MdbURI = "mongodb://useradmin:mypassword@localhost:27017/tracking?authSource=admin"
client = MongoClient(MdbURI)
下面是连接和验证 pymongo 的方法:
from pymongo import MongoClient
# MongoDB connection info
hostname = '10.20.30.40'
port = 27017
username = 'adminUserName'
password = 'secret'
databaseName = 'someDB'
# connect with authentication
client = MongoClient(hostname, port)
db = client[databaseName]
db.authenticate(username, password)