基础环境搭建:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ setenforce 0 $ systemctl stop firewalld $ echo never > /sys/kernel/mm/transparent_hugepage/enabled $ echo never > /sys/kernel/mm/transparent_hugepage/defrag $ cat /etc/redhat-release # CentOS Linux release 7.5.1804 (Core) $ mongo --version MongoDB shell version v4.0.4 git version: f288a3bdf201007f3693c58e140056adf8b04839 OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013 allocator: tcmalloc modules: none build environment: distmod: rhel70 distarch: x86_64 target_arch: x86_64 MongoDB_shard_01.t4x.org 192.168.190.108 MongoDB_shard_02.t4x.org 192.168.190.109 MongoDB_shard_03.t4x.org 192.168.190.110 |
建立用户:
0 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 |
$ mongo #表示bind为127.0.0.1 若非127请配置--host指定 $ mongo --host 192.168.190.109 MongoDB shell version v4.0.4 connecting to: mongodb://192.168.190.109:27017/ > show dbs admin 0.000GB config 0.000GB local 0.000GB > db.createUser({ user: 'root', pwd: '123456', roles: [ { role:"root", db: "admin" } ] }); Successfully added user: { "user" : "root", "roles" : [ { "role" : "root", "db" : "admin" } ] } > show users { "_id" : "test.root", "user" : "root", "db" : "test", "roles" : [ { "role" : "root", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } |
开启验证:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
34 security: 35 authorization: enabled > show dbs 2018-11-16T23:17:42.896+0800 E QUERY [js] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "command listDatabases requires authentication", "code" : 13, "codeName" : "Unauthorized" } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:67:1 shellHelper.show@src/mongo/shell/utils.js:876:19 shellHelper@src/mongo/shell/utils.js:766:15 @(shellhelp2):1:1 > db.auth("root","123456") 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB |
申明:除非注明Byrd's Blog内容均为原创,未经许可禁止转载!详情请阅读版权申明!