docker+mongodb的安裝,密碼認證


下載

[root@k8s-master2 ~]# docker pull mongo:4.2.2
4.2.2: Pulling from library/mongo
5c939e3a4d10: Pull complete
c63719cdbe7a: Pull complete
19a861ea6baf: Pull complete
651c9d2d6c4f: Pull complete
85155c6d5fac: Pull complete
85fb0780fd97: Pull complete
85b3b1a901f5: Pull complete
6a882e007bb6: Pull complete
f7806503a70f: Pull complete
e23d5068c270: Pull complete
7b092576a143: Pull complete
cbac198434de: Pull complete
af4e4bd07dc4: Pull complete
Digest: sha256:772374c6b41f564d35e703078cc0bc145a2001aa026815689f2068860eebc701
Status: Downloaded newer image for mongo:4.2.2
docker.io/library/mongo:4.2.2

運行

  五賬戶密碼,不需要認證

[root@k8s-master2 ~]# docker run --name mongo --restart=always -p 27017:27017 -v /data/mongodb:/data/db -d mongo:4.2.2
8f4be7421f0b68524d795dbd00ac1eea11cce59d1df9557aa2815fa645d53193
[root@k8s-master2 ~]# docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                                                                    NAMES
8f4be7421f0b        mongo:4.2.2               "docker-entrypoint.s…"   3 seconds ago       Up 2 seconds        0.0.0.0:27017->27017/tcp                                                 elated_williamson

  有賬戶密碼,需要認證

//容器默認啟動命令是mongod,我們認證需要修改啟動命為mongod --auth
[root@mysql-129 ~]# docker run --name mongo --restart=always -p 27017:27017 -v /data/mongodb:/data/db -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=123456  -d mongo:4.2.2 mongod --auth
c18427def2059349c3a84786b99bb955ddd70f21bef833aa4ec44c7d1e19a184

 

  進入容器

[root@k8s-master2 ~]# docker exec -it 8f4be7421f0b bash
root@8f4be7421f0b:/# mongo --version
MongoDB shell version v4.2.2
git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64

  無密碼進入數據庫

root@8f4be7421f0b:/# mongo
MongoDB shell version v4.2.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1a41d6b4-a387-445d-95a6-57a608290813") }
MongoDB server version: 4.2.2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
Server has startup warnings:
2020-01-17T01:17:43.520+0000 I  STORAGE  [initandlisten]
2020-01-17T01:17:43.520+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-01-17T01:17:43.520+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-01-17T01:17:44.357+0000 I  CONTROL  [initandlisten]
2020-01-17T01:17:44.357+0000 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-01-17T01:17:44.357+0000 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-01-17T01:17:44.357+0000 I  CONTROL  [initandlisten]
2020-01-17T01:17:44.357+0000 I  CONTROL  [initandlisten]
2020-01-17T01:17:44.357+0000 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-01-17T01:17:44.357+0000 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-01-17T01:17:44.358+0000 I  CONTROL  [initandlisten]
2020-01-17T01:17:44.358+0000 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-01-17T01:17:44.358+0000 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-01-17T01:17:44.358+0000 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> help
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell
> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB

  需要密碼認證

在連接期間進行身份驗證
使用-u <username>,-p <password>和--authenticationDatabase <database>命令行選項啟動一個mongo shell:

mongo --port 27017 -u "admin" -p "123456" --authenticationDatabase "admin"

  連接后驗證
將 mongo shell 連接到 mongodb,也就是先連接,后驗證用戶身份

mongo

use admin
db.auth("admin", "123456" )

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM