温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

docker中如何使用mongodb数据库

发布时间:2022-05-25 14:45:19 来源:亿速云 阅读:868 作者:iii 栏目:大数据

这篇文章主要介绍“docker中如何使用mongodb数据库”,在日常操作中,相信很多人在docker中如何使用mongodb数据库问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”docker中如何使用mongodb数据库”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

第一步

获得mongo镜像,命令如下,

# 拉取mongo镜像
docker pull mongo
# 查看本地库mongo镜像
docker images mongo

第二步

启动mongo镜像,如下

# 启动mongo镜像
docker run -itd -p --name mongo-server mongo bash
# 查看mongo进程
docker ps -a

第三步

进入容器并启动mongo,如下

# 进入mongo容器,其中‘mongo-server'是mongo容器别名
docker exec -it mongo-server bash
# 启动mongo服务
mongod &
# 在容器中查看mongo进程
ps -ef | grep mongo

启动以后会看到如下日志(最后几行),

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

第四步

本地验证测试,输入mongo启动mongo命令行客户端,如下内容

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" } }

接着输入测试命令,

> show dbs;
admin 0.000gb
local 0.000gb
>

第五步

在局域网访问docker中的mongo,由于启动的时候是使用的随机端口映射,意思是宿主机产生一个随机端口去映射mongo的27017端口,通过‘docker ps -a'命令查看宿主机随机端口,如下,

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

如上,宿主机随机端口为32773,通过配置robomongo客户端连接mongo,如下,

docker中如何使用mongodb数据库

结果正常。

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

# 登入mongo
mongo
# 选择使用admin库,在其他的库不行
use admin
# 关闭mongo
db.shutdownserver()

通过‘ps -ef | grep momgo'查看mongo进程是否存在,如果不存在说明关闭成功。

到此,关于“docker中如何使用mongodb数据库”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI