docker安裝mongodb數(shù)據(jù)庫測試環(huán)境

1、下載mongo的image

~ ? docker pull mongo:4      

4: Pulling from library/mongo
35b42117c431: Pull complete
ad9c569a8d98: Pull complete
293b44f45162: Pull complete
0c175077525d: Pull complete
4e73525b52ba: Pull complete
a22695a3f5e9: Pull complete
c5175bcf2977: Pull complete
3e320da07793: Pull complete
01c6db6b2b5a: Pull complete
3bd6e9d03e78: Pull complete
e03dcf51513f: Pull complete
c1956a9e136a: Pull complete
4c35cf22b1d5: Pull complete
Digest: sha256:71600e081274550f00647655db8b85e3103f763507c55a0636dcab1efc126630

2、啟動mongo

docker run -p 27017:27017 -v    /data/docker/mongodb:/data/db   -v /data/docker/mongodb:/data/configdb   -d mongo:4  --auth 

這里數(shù)據(jù)存放在 /data/docker/mongodb 文件夾,這里注意mac下需要sharing下文件夾。
--auth表示開啟權限相關

3、驗證

本地沒有mongo的客戶端,使用容器里面的客戶端鏈接執(zhí)行
第一次使用host報錯

/data/docker/mongodb ? docker exec -it 25f796b9b97c mongo --host 172.17.28.85     
MongoDB shell version v4.0.10
connecting to: mongodb://172.17.28.85:27017/?gssapiServiceName=mongodb
2019-07-23T07:44:40.439+0000 E QUERY    [js] Error: couldn't connect to server 172.17.28.85:27017, connection attempt failed: SocketException: Error connecting to 172.17.28.85:27017 :: caused by :: No route to host :
connect@src/mongo/shell/mongo.js:344:17
@(connect):2:6
exception: connect failed

第二次使用localhost成功

/data/docker/mongodb ? docker exec -it 25f796b9b97c mongo --host localhost     
MongoDB shell version v4.0.10
connecting to: mongodb://localhost:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("01ae73d0-7a39-4303-8ca8-00f1fcfbb9d0") }
MongoDB server version: 4.0.10
Server has startup warnings:
2019-07-23T07:38:42.440+0000 I CONTROL  [initandlisten]
2019-07-23T07:38:42.441+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-23T07:38:42.441+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-07-23T07:38:42.441+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()
---

使用本機IP不通的問題可能是和防火墻有關系,網(wǎng)上說使用localhost可行,我試了試確實可以。

https://forums.docker.com/t/no-route-to-host-network-request-from-container-to-host-ip-port-published-from-other-container/39063/10

4、權限處理

剛才加了--auth后需要添加mongo的賬號,使用下面命令登錄即可:

docker exec -it 21aeff91b61e6ebfe2b347e0a7549510abcd4eeb8b095d51dfbb1360951864a5 mongo admin

然后使用mongo的命令create user,授權等;具體命令查看mongo文檔。

不加--auth,測試環(huán)境裸奔一下也無妨。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容