使用 authenticationDatabase 参数链接 aliyun 上的 MongoDB

一般,命令行 链接 MongoDB 咱们是这么作的:mongodb

mongo -u <user> -p <pass> --host <host> --port 28015

或者使用标准的链接字符串地址URI:数据库

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[database][?options]]

# 例如
mongo mongodb://127.0.0.1:27017

当链接主从数据库时候也能够直接使用,例如某些云服务器提供商的数据库链接方式:ubuntu

mongo mongodb://root:password@dds-0xi1234.mongodb.rds.aliyuncs.com:3717,dds-0xi5678.mongodb.rds.aliyuncs.com:3717/admin

直接使用这种通用的地址字符串是很是方便的,无论是独立数据库、副本集以及集群都是统一的,格式大概是这样的:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[database][?options]]服务器

可是,mongo 命令行客户端能够使用,其余的一些工具就没有办法直接用了,好比mongotop,mongofiles等等:app

# grid fs 操做
root@server:~# mongofiles mongodb://ubuntu:password@IP_ADDR:27017/admin
2019-08-12T20:39:52.764+0800    'mongodb://ubuntu:password@IP_ADDR:27017/admin' is not a valid command
2019-08-12T20:39:52.765+0800    try 'mongofiles --help' for more information

# mongo top
root@server:~# mongotop mongodb://ubuntu:password@IP_ADDR:27017/admin
2019-08-12T20:44:39.874+0800    invalid sleep time: mongodb://ubuntu:password@IP_ADDR:27017/admin

查了一下文档,阿里云上购买的这种叫作 Authentication Database¶ 的数据库,须要使用 --authenticationDatabase 参数来操做:ide

Authentication Database 的说明是这样的:工具

Authentication Database¶
When adding a user, you create the user in a specific database. This database is the authentication database for the user.

A user can have privileges across different databases; that is, a user’s privileges are not limited to their authentication database. By assigning to the user roles in other databases, a user created in one database can have permissions to act on other databases. For more information on roles, see Role-Based Access Control.

The user’s name and authentication database serve as a unique identifier for that user. [1] That is, if two users have the same name but are created in different databases, they are two separate users. If you intend to have a single user with permissions on multiple databases, create a single user with roles in the applicable databases instead of creating the user multiple times in different databases.

对于这样的数据库,若是咱们要使用 db 自带那一族工具来操做的话能够这样:阿里云

mongofiles --host dds-0xi1234.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -d xxx list
mongotop --host dds-0xi1234.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin
mongostat --host dds-0xi1234.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin
相关文章
相关标签/搜索