Connect MongoDB on aliyun using the authenticationDatabase parameter

Usually, we do this by connecting MongoDB from the command line:

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

Or use the standard connection string address URI:

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

# for example
mongo mongodb://127.0.0.1:27017

It can also be used directly when connecting master-slave databases, such as the database connection mode of some cloud server providers:

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

Direct use of this generic address string is very convenient, regardless of the independent database, replica set and cluster are uniform, the format is probably as follows:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[database][?options]]

However, the mongo command line client can use it, and other tools can't be used directly, such as mongotop,mongofiles, etc.

# grid fs operation
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

After consulting the documents, the Authentication Database database purchased on Aliyun needs to be operated with the -- authentication database parameter:

The Authentication Database is described as follows:

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.

For such a database, if we want to use the family of tools that db brings with it to operate, we can do this:

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

Keywords: Go Database MongoDB Ubuntu

Added by vaaaska on Sat, 05 Oct 2019 19:12:57 +0300