一般,命令行 鏈接 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