切换用户; use admin db.auth("myClusterwideAdmin_user","admin123"); 建库,写入数据; use products; db.products.insertOne( { _id: 10, item: "box", qty: 20 } ); db.auth("products_admin","admin123"); db.products.save({item: "box", qty: 20 }); db.createUser( { user: "products_admin", pwd: "admin123", roles: [ "readWrite", "dbAdmin" ] } ); Successfully added user: { "user" : "products_admin", "roles" : [ "readWrite", "dbAdmin" ] } myreplSet:PRIMARY> use products switched to db products myreplSet:PRIMARY> db.createUser( ... { ... user: "products_admin", ... pwd: "admin123", ... roles: [ "readWrite", "dbAdmin" ] ... } ... ); Successfully added user: { "user" : "products_admin", "roles" : [ "readWrite", "dbAdmin" ] } myreplSet:PRIMARY> use products switched to db products myreplSet:PRIMARY> db.getRoles(); [ ] myreplSet:PRIMARY> db.getUsers(); [ { "_id" : "products.products_admin", "user" : "products_admin", "db" : "products", "roles" : [ { "role" : "readWrite", "db" : "products" }, { "role" : "dbAdmin", "db" : "products" } ] } ] myreplSet:PRIMARY> db.collection.drop() Studio 3T for MongoDB连接51.212复制集 [ #DirectConection Authentication Mode - Basic(MONGODB-CR or SCEAM-SHA-1) 192.168.2.51:27017 -u products_admin -p admin123 192.168.2.212:27017 -u products_admin -p admin123 Authentication DB - products ] [ #Replica Set Or Sharded Cluster 192.168.2.51:27017 192.168.2.212:27017 Authentication Mode - Basic(MONGODB-CR or SCEAM-SHA-1) -u products_admin -p admin123 Read Preference -Nearest Authentication DB - products C:WindowsSystem32driversetchosts + 192.168.2.51 hadoop1 192.168.2.212 hadoop2 ] show collections; db.createCollection('direct_ad_consume'); db.createCollection('sem_z_cashier');