|
下载
[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" )
来源:https://www.cnblogs.com/linyouyi/p/12204258.html |