登录:mongo -u username -p
查看系统用户:
use admin
db.system.users.find()
{ "_id":"admin.admin_api", "userId":"", "user":"admin_api", "db":"admin", "credentials":{ "SCRAM-SHA-1":{ "iterationCount":10000, "salt":"POiQZhVlA0zArj9aRo4yqQ==", "storedKey":"nVr48uHmnboxrT7iAmRGTXCJMZU=", "serverKey":"MttCIcMabmQBYqSn39gtawbhK8c=" }, "SCRAM-SHA-256":{ "iterationCount":15000, "salt":"gQV39EFOIt8qrVk51eSvItuYeBuZ0nSyK8UUcQ==", "storedKey":"/EV8k7V3AqLi1kXrtQq2zj09Hwtx+GPcPSh5Y3gqyFY=", "serverKey":"WwMdEf+hI5nFjH9o6RYr/QzGCi+cHfpcbzxITP699oY=" } }, "roles":[ { "role":"readWriteAnyDatabase", "db":"admin" }, { "role":"root", "db":"admin" } ] }
添加用户: db.createUser( { user: "web", pwd: "123", roles: [ { role: "readWrite", db: "test" } ] } ) # 删除用户 db.system.users.remove({user:"web"})
授权db登录:mongo --host 127.0.0.1 -u web -p
show dbs use <db name> show collections db.collectionName.find()
导出: mongoexport --host 127.0.0.1 --collection hotcities --db test --username web --password 123 --out hotcities.json 导入:error mongoimport --host 127.0.0.1 --collection hotcities --db test --username web --password 123 --file /var/hotcities.json 2020-11-06T16:22:51.284+0800 Failed: error connecting to db server: server returned error on SASL authentication 2020-11-06T16:22:51.284+0800 imported 0 documents 导入: mongoimport --collection hotcities --db test --username web --password 123 --authenticationDatabase admin --file /var/hotcities.json 2020-11-06T16:26:29.654+0800 connected to: localhost 2020-11-06T16:26:29.707+0800 imported 185 documents