1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| // 切换到admin库 > use admin > show collections system.version // 创建两个用户 > db.createUser({ user: "myroot", pwd: "123456", roles: ["root"]}) Successfully added user: { "user" : "myroot", "roles" : [ "root" ] } > db.createUser({ user: "myadmin", pwd: "123456", roles: [{role: "userAdminAnyDatabase", db: "admin"}] }) Successfully added user: { "user" : "myadmin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } > show collections system.users system.version // 查看创建的两个用户信息 > db.system.users.find() { "_id" : "admin.myroot", "userId" : UUID("c8365246-4cf7-42f8-9314-ad1f631fd7ae"), "user" : "myroot", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "i17znsXFbvxDIAQUqrc/hQ==", "storedKey" : "IbJzJbI3A6LkDbMv2Jwjh20MZWI=", "serverKey" : "ruaTqiKVVI5Q7ynAEr2lELAJqE4=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "HMwHwCWCJ60MA8ZfStsTtcqVBAzw5v/T2E1Lvg==", "storedKey" : "4JWWSKh3OUK+w3xob1zoPaIOlwf/oNzQ2OYEds1LgPQ=", "serverKey" : "pLEtZfd1rPS+nonvTTxTIk9tpIbXlDmrCbyzidennMw=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] } { "_id" : "admin.myadmin", "userId" : UUID("c2381b0a-51ca-48b9-9c8d-388eea823f16"), "user" : "myadmin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "XY9Cfm40eCBJeGCCME27iQ==", "storedKey" : "sZwkDmgynehLzYQGg0kM8eG02cw=", "serverKey" : "12bPEIwuAy8/PhCX6aHdtIcLpIs=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "eFKxqF7f9KAg1A6HjvL4LFNfIk7qAf+blela8A==", "storedKey" : "dpJdR44or0iTQtQkJ1mJcvOhzXrb57hhvBf/hFjOnJs=", "serverKey" : "BrNyBkx6fZhhMPAhUTwvAfv6fdFLLC6W4AJBD7hXa+k=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } // 删除用户 > db.dropUser("myadmin") true > db.system.users.find() { "_id" : "admin.myroot", "userId" : UUID("c8365246-4cf7-42f8-9314-ad1f631fd7ae"), "user" : "myroot", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "i17znsXFbvxDIAQUqrc/hQ==", "storedKey" : "IbJzJbI3A6LkDbMv2Jwjh20MZWI=", "serverKey" : "ruaTqiKVVI5Q7ynAEr2lELAJqE4=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "HMwHwCWCJ60MA8ZfStsTtcqVBAzw5v/T2E1Lvg==", "storedKey" : "4JWWSKh3OUK+w3xob1zoPaIOlwf/oNzQ2OYEds1LgPQ=", "serverKey" : "pLEtZfd1rPS+nonvTTxTIk9tpIbXlDmrCbyzidennMw=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] } // 修改密码 > db.changeUserPassword("myroot", "123456") > db.system.users.find() { "_id" : "admin.myroot", "userId" : UUID("c8365246-4cf7-42f8-9314-ad1f631fd7ae"), "user" : "myroot", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Jkjum+zf3ZyqZmT0/UbJrg==", "storedKey" : "QVhBHcs29ZgHBb19okGUApJUgeY=", "serverKey" : "dSXKuwdM8TNv6VxmK6tnJEB1X+U=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "64JxM8Cnjzmgtu1tzwTpGxA02ATrjahOjnJv5w==", "storedKey" : "OzFFQ9Is5N+vv4VDISii4ckGI+DB51Rl4vrAg2uZDAw=", "serverKey" : "iZm3CQwFAtvVHHL1H5liczL/Uo7Z4mUXP3APOrphYx0=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] } // 认证测试 > db.auth("myadmin", "asdf") Error: Authentication failed. 0 > db.auth("myroot", "abc") Error: Authentication failed. 0 > db.auth("myroot", "123456") 1 // 创建普通角色 > use articledb switched to db articledb > db.createUser({user: "denial", pwd: "123456", roles: [{role: "readWrite", db: "articledb"}]}) Successfully added user: { "user" : "denial", "roles" : [ { "role" : "readWrite", "db" : "articledb" } ] } > db.auth("denial", "123456") 1
|