docker安装MongoBD(超详细)

一、安装docker

推荐文章:https://blog.csdn.net/Sumuxi9797926/article/details/127313307?spm=1001.2014.3001.5502

二、创建主机挂载配置目录

data目录存放mongodb数据库文件,删除重启容器不会丢失

mkdir -p /docker/mongodb/data && cd /docker/mongodb

三、镜像拉取完成后,运行mongodb容器

输入 pwd可以看到当前目录 /docker/mongodb,,然后执行如下命令

docker pull mongo:4.2.2

看到如下图片信息,说明拉去成功了
docker安装MongoBD(超详细)_第1张图片

直接用 start.sh 文件管理mongo的启动与关闭

pwd
/docker/mongodb

vim start.sh

然后文件写入如下的文本

cat <<EOF> start.sh
#!/bin/bash
MONGODB_DIR=`pwd`
docker stop mongodb
docker rm mongodb
docker run -d \\
  --name mongo \\
  --restart always \\
  --privileged \\
  -p 27017:27017 \\
  -v \${MONGODB_DIR}/data:/data/db \\
  -e MONGO_INITDB_ROOT_USERNAME=admin \\
  -e MONGO_INITDB_ROOT_PASSWORD=123456 \\
  mongo:4.2.2 mongod --auth
EOF

键入如下命令,进入容器里面

[root@localhost mongodb]# docker exec -it mongo bash

键入如下命令,查看mongo版本

root@1b19e48e3612:/# 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

键入如下命令,登录mongo

root@1b19e48e3612:/# mongo --port 27017 -u “root” -p “admin1222” --authenticationDatabase “admin”
MongoDB shell version v4.2.2
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“4cb3dca7-5263-4dc4-8e2e-e9e860961043”) }
MongoDB server version: 4.2.2
Server has startup warnings:
2023-04-01T02:18:33.551+0000 I CONTROL [initandlisten]
2023-04-01T02:18:33.551+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.
2023-04-01T02:18:33.551+0000 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2023-04-01T02:18:33.551+0000 I CONTROL [initandlisten]
2023-04-01T02:18:33.551+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.
2023-04-01T02:18:33.551+0000 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2023-04-01T02:18:33.551+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()
show dbs # 查看库
admin 0.000GB
config 0.000GB
local 0.000GB
exit #推出
bye

四、链接mongo客户端NoSQL Manager for MongoDB Freeware

下载安装参考链接http://hz.itheima.com/news/20201210/175625.html
链接NoSQL Manager for MongoDB Freeware
docker安装MongoBD(超详细)_第2张图片
至此就可以开心的玩耍mongo了~
happy ending~~~
万里归来颜愈少,微笑,笑时犹带岭梅香。试问岭南应不好?却道:此心安处是吾乡。

你可能感兴趣的:(docker,mongodb,容器)