# Docker安装MongoDB

# Docker安装MongoDB

MongoDB是一个NoSQL的非关系型数据库 ,支持海量数据存储,高性能的读写

# 安装

  1. 拉去镜像
docker pull mongo:4.4
1
  1. 创建mongo数据持久化目录
mkdir -p /docker_volume/mongodb/data
1
  1. 运行容器
docker run -itd --name mongo -v /docker_volume/mongodb/data:/data/db -p 27017:27017 mongo:4.4 --auth
1

-v: 将宿主机的/docker_volume/mongodb/data映射到容器的/data/db目录,将数据持久化到宿主机,以防止删除容器后,容器内的数据丢失
–auth:需要密码才能访问容器服务

# 创建用户

  1. 登录mongo容器,并进入到【admin】数据库
docker exec -it mongo mongo admin
1
  1. 创建一个用户,mongo 默认没有用户
db.createUser({ user:'root',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},'readWriteAnyDatabase']});

#【user:‘root’ 】:设置用户名为root
#【pwd:‘123456’】:设置密码为123456
#【role:‘userAdminAnyDatabase’】:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
#【db: ‘admin’】:可操作的数据库
#【‘readWriteAnyDatabase’】:赋予用户读写权限
1
2
3
4
5
6
7

# 连接、测试

  1. 连接mongo数据库
db.auth('root', '123456')
1
  1. 测试数据库,插入一条语句
db.user.insert({"name":"zhangsan","age":18})
1
  1. 测试数据库,查询刚才插入的语句
db.user.find()
1

# Docker-Compose安装MongoDB

docker-compose安装mongodb - 扰扰 - 博客园 (cnblogs.com) (opens new window)

[root@localhost ufs-platform-docker]# docker compose up -d ufs-mongo
[+] Running 14/14
 ✔ ufs-mongo 13 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]      0B/0B      Pulled                                                                                                116.0s 
   ✔ f08d8e2a3ba1 Pull complete                                                                                                                               29.7s 
   ✔ 3baa9cb2483b Pull complete                                                                                                                               29.8s 
   ✔ 94e5ff4c0b15 Pull complete                                                                                                                               29.9s 
   ✔ 1860925334f9 Pull complete                                                                                                                               29.9s 
   ✔ 9d42806c06e6 Pull complete                                                                                                                               29.9s 
   ✔ 31a9fd218257 Pull complete                                                                                                                               30.5s 
   ✔ 5bd6e3f73ab9 Pull complete                                                                                                                               31.2s 
   ✔ f6ae7a64936b Pull complete                                                                                                                               31.4s 
   ✔ 80fde2cb25c5 Pull complete                                                                                                                               31.4s 
   ✔ 1bec62fe62fc Pull complete                                                                                                                               31.9s 
   ✔ 2cf4970a1653 Pull complete                                                                                                                               86.9s 
   ✔ 39fac3226e16 Pull complete                                                                                                                               86.9s 
   ✔ 86bca9c64faf Pull complete                                                                                                                               86.9s 
[+] Running 1/1
 ✔ Container ufs-mongo  Started                                                                                                                                1.3s 
[root@localhost ufs-platform-docker]# docker exec -it mongo /bin/bash
Error response from daemon: No such container: mongo
[root@localhost ufs-platform-docker]# docker exec -it ufs-mongo /bin/bash
root@72688aeea651:/# mongo
MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("13580648-0bf5-4300-9405-97f099b9739b") }
MongoDB server version: 4.4.0
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2023-07-08T05:06:13.018+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2023-07-08T05:06:13.019+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2023-07-08T05:06:13.019+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
        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()
---
> use admin
switched to db admin
> db.createUser({user:"root",pwd:"root",roles:[{role:'root',db:'admin'}]})
Successfully added user: {
	"user" : "root",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
> exit
bye
root@72688aeea651:/# exit
exit
[root@localhost ufs-platform-docker]# 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66