本文描述如何使用docker在本地安装和部署mongo数据库。
docker for mac的安装参考: https://blog.csdn.net/catchertherye/article/details/102866355
mongo docker镜像说明文档 https://hub.docker.com/_/mongo
安装和启动过程
# 下载镜像
docker pull mongo
#启动mongo
docker run -d --name my-mongo -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret -p 27017:27017 mongo
# 测试在本地是否能访问到docker
telnet localhost 27017
# 登录docker机器, 并使用启动mongo的账号和密码登录
docker exec -it my-mongo bash
root@b044f5abd30c:/# mongo --port 27017 -u 'mongoadmin' -p 'secret'
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6a1e3113-ec41-4095-936b-96f4fe123053") }
MongoDB server version: 4.2.1
Server has startup warnings:
2019-11-02T02:34:42.996+0000 I STORAGE [initandlisten]
2019-11-02T02:34:42.996+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-11-02T02:34:42.996+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
---
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()
---
>
看到上面的说明启动成功了,可以在终端中试试mongo的功能
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use admin
switched to db admin
> db.user.find()
{ "_id" : NumberLong(1000), "firstName" : "zhanzhan", "lastName" : "qi", "age" : 31, "_class" : "com.example.webflux.domain.User" }
{ "_id" : NumberLong(1001), "firstName" : "zhanzhan", "lastName" : "qi", "age" : 31, "_class" : "com.example.webflux.domain.User" }
{ "_id" : NumberLong(1002), "firstName" : "zhanzhan", "lastName" : "qi", "age" : 31, "_class" : "com.example.webflux.domain.User" }
{ "_id" : NumberLong(1006), "firstName" : "zhanzhan", "lastName" : "qi", "age" : 31, "_class" : "com.example.webflux.domain.User" }
{ "_id" : NumberLong(1007), "firstName" : "zhanzhan", "lastName" : "qi", "age" : 31, "_class" : "com.example.webflux.domain.User" }
{ "_id" : NumberLong(1008), "firstName" : "zhanzhan", "lastName" : "qi", "age" : 31, "_class" : "com.example.webflux.domain.User" }
{ "_id" : NumberLong(1009), "firstName" : "zhanzhan", "lastName" : "qi", "age" : 31, "_class" : "com.example.webflux.domain.User" }
>