Docker:docker部署mongoDB数据库

1. docker下载mongodb 5.0.5镜像

## 下载命令
docker pull mongo:5.0.5

[root@mongodb mongodb-5.0.5]# docker pull mongo:5.0.5
Trying to pull repository docker.io/library/mongo ... 
5.0.5: Pulling from docker.io/library/mongo
Digest: sha256:517f98e5b796844475a985b78a3abbc72cbadd7497d0ee689cf8b8c9ad34f89e
Status: Downloaded newer image for docker.io/mongo:5.0.5
[root@mongodb mongodb-5.0.5]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/mongo     5.0.5               dfda7a2cf273        27 hours ago        693 MB
docker.io/mongo     latest              dfda7a2cf273        27 hours ago        693 MB
docker.io/mongo     3.6.23              2f21415cb85f        7 months ago        453 MB

2. 编写mongodb配置文件

# 创建 mongod.conf 文件
vi mongod.conf

# 写入下面内容
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  ## 将本机访问改成支持远程访问
  bindIp: 0.0.0.0


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

3. 启动mongodb容器

## 容器启动
docker run --name mongodb -p 27017:27017 -v /home/mongodb/db:/data/db/ -v /home/mongodb/config/mongod.conf:/etc/mongod.conf.orig -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=sc@123456 --privileged=true --restart always -td mongo:5.0.5

## /home/mongodb/db:/data/db/   mongodb数据挂载
## /home/mongodb/config/mongod.conf:/etc/mongod.conf.orig   更新配置文件

4. 登录mongodb验证

## 登录mongodb容器
docker exec -it mongodb /bin/bash

## 查看版本
db.version();

## 指定库
use admin;

## 查看所有库
show dbs

## 创建数据库mydb,没有会自动创建
use mydb

## 查看库内的集合
show collections

 

posted @ 2023-12-01 14:44  怒吼的萝卜  阅读(236)  评论(0编辑  收藏  举报