前端analysis | What,Why,Who,When,Where,How

《Docker》- linux下docker环境mongo安装和配置

2020-03-04

mongo 安装

docker mongo镜像

1
$ docker pull mongo

mongo运行

1
2
3
4
5
 # 停留在当前进程
docker run -p 27017:27017 --name mdb mongo

# 守护进程形式后端运行
docker run -p 27017:27017 --name mdb -d mongo

exec mongo shell

  • docker exec -it mdb bash

  • docker exec -it mdb mongo

mongodb 命令

查看dbs

1
2
3
4
5
6
7
8
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB

切换数据库、保存数据

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
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
// 不代表创建db
> use mall
switched to db mall
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> mall.user.save({name:'zhangsan',age:'',telno:'12832233233'})
2020-03-08T07:27:07.940+0000 E QUERY [js] uncaught exception: ReferenceError: mall is not defined :
@(shell):1:1
> db.user.save({name:'zhangsan',age:'',telno:'12832233233'})
WriteResult({ "nInserted" : 1 })
// 保存数据成功,数据库肯定创建
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
mall 0.000GB
> db
mall
> db.user.find();
{ "_id" : ObjectId("5e649e8e27c905567615607d"), "name" : "zhangsan", "age" : "", "telno" : "12832233233" }
> db.user.find({name:'zhangsan'});
{ "_id" : ObjectId("5e649e8e27c905567615607d"), "name" : "zhangsan", "age" : "", "telno" : "12832233233" }
>

mongo client - web ui

mongoclient镜像

1
docker pull mongoclient/mongoclient

启动

1
2
3
4
docker run -d -p 3000:3000 mongoclient/mongoclient

# 配置持久化
docker run -d -p 3000:3000 -v <your_path>:/data/db mongoclient/mongoclient

访问mongo


* 存在数据库内容不同步,待解决

mongo client - mongoose

连接mongo

1
mongodb://mongo:27017/mdb

mongoose

1
const schema = mongoose.Schema

参考

docker
mongoclient

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏