溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

docker中如何使用mongodb數(shù)據(jù)庫

發(fā)布時(shí)間:2022-05-25 14:45:19 來源:億速云 閱讀:880 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“docker中如何使用mongodb數(shù)據(jù)庫”,在日常操作中,相信很多人在docker中如何使用mongodb數(shù)據(jù)庫問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”docker中如何使用mongodb數(shù)據(jù)庫”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

第一步

獲得mongo鏡像,命令如下,

# 拉取mongo鏡像
docker pull mongo
# 查看本地庫mongo鏡像
docker images mongo

第二步

啟動(dòng)mongo鏡像,如下

# 啟動(dòng)mongo鏡像
docker run -itd -p --name mongo-server mongo bash
# 查看mongo進(jìn)程
docker ps -a

第三步

進(jìn)入容器并啟動(dòng)mongo,如下

# 進(jìn)入mongo容器,其中‘mongo-server'是mongo容器別名
docker exec -it mongo-server bash
# 啟動(dòng)mongo服務(wù)
mongod &
# 在容器中查看mongo進(jìn)程
ps -ef | grep mongo

啟動(dòng)以后會(huì)看到如下日志(最后幾行),

2017-06-13t07:25:56.318+0000 i ftdc  [initandlisten] initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-06-13t07:25:56.328+0000 i index [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2017-06-13t07:25:56.328+0000 i index [initandlisten]   building index using bulk method; build may temporarily use up to 500 megabytes of ram
2017-06-13t07:25:56.329+0000 i index [initandlisten] build index done. scanned 0 total records. 0 secs
2017-06-13t07:25:56.329+0000 i command [initandlisten] setting featurecompatibilityversion to 3.4
2017-06-13t07:25:56.329+0000 i network [thread1] waiting for connections on port 27017

第四步

本地驗(yàn)證測(cè)試,輸入mongo啟動(dòng)mongo命令行客戶端,如下內(nèi)容

root@01ff7ed6377c:/# mongo
mongodb shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
2017-06-13t07:34:00.289+0000 i network [thread1] connection accepted from 127.0.0.1:55650 #2 (1 connection now open)
2017-06-13t07:34:00.290+0000 i network [conn2] received client metadata from 127.0.0.1:55650 conn2: { application: { name: "mongodb shell" }, driver: { name: "mongodb internal client", version: "3.4.4" }, os: { type: "linux", name: "pretty_name="debian gnu/linux 8 (jessie)"", architecture: "x86_64", version: "kernel 3.10.0-327.28.3.el7.x86_64" } }

接著輸入測(cè)試命令,

> show dbs;
admin 0.000gb
local 0.000gb
>

第五步

在局域網(wǎng)訪問docker中的mongo,由于啟動(dòng)的時(shí)候是使用的隨機(jī)端口映射,意思是宿主機(jī)產(chǎn)生一個(gè)隨機(jī)端口去映射mongo的27017端口,通過‘docker ps -a'命令查看宿主機(jī)隨機(jī)端口,如下,

docker ps -a 
container id  image    command     created    status    ports      names
01ff7ed6377c  mongo    "docker-entrypoint.sh" 15 minutes ago  up 15 minutes  0.0.0.0:32773->27017/tcp mongo-server

如上,宿主機(jī)隨機(jī)端口為32773,通過配置robomongo客戶端連接mongo,如下,

docker中如何使用mongodb數(shù)據(jù)庫

結(jié)果正常。

如果要停止mongo可以使用如下命令,

# 登入mongo
mongo
# 選擇使用admin庫,在其他的庫不行
use admin
# 關(guān)閉mongo
db.shutdownserver()

通過‘ps -ef | grep momgo'查看mongo進(jìn)程是否存在,如果不存在說明關(guān)閉成功。

到此,關(guān)于“docker中如何使用mongodb數(shù)據(jù)庫”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI