Posts 使用 Docker 启动 mongodb
Post
Cancel

使用 Docker 启动 mongodb

使用 docker 启动 mongodb

1
2
$ mkdir db
$ docker run -it --rm -p 27017:27017 -v ${PWD}/db:/data/db  --name mongodb -e MONGO_INITDB_ROOT_USERNAME=root -e MONGO_INITDB_ROOT_PASSWORD=123456 mongo:4.4

安装 mongodb-shell

1
2
3
4
 $ wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
 $ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
 $ sudo apt-get update
 $ sudo apt-get install mongodb-org-shell

链接 mongodb

1
2
3
4
5
$ mongo --host 127.0.0.1 --port 27017 -u root -p 123456
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8a7be0c2-17a9-4a2e-9f43-dab0c52c02ca") }
MongoDB server version: 4.4.1

基本操作

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
> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB

> use testdb
switched to db testdb

> db.createCollection("test");
{ "ok" : 1 }

> show collections;
test

> db.test.insert({
... _id : ObjectId("507f191e810c19729de860ea"),
... title: "MongoDB Overview",
... description: "MongoDB is no sql database",
... by: "tutorials point",
... url: "http://www.tutorialspoint.com",
... tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
WriteResult({ "nInserted" : 1 })

> db.test.find()
{ "_id" : ObjectId("507f191e810c19729de860ea"), "title" : "MongoDB Overview", "description" : "MongoDB is no sql database", "by" : "tutorials point", "url" : "http://www.tutorialspoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }

> db.test.find().pretty()
{
	"_id" : ObjectId("507f191e810c19729de860ea"),
	"title" : "MongoDB Overview",
	"description" : "MongoDB is no sql database",
	"by" : "tutorials point",
	"url" : "http://www.tutorialspoint.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}

参考资料

This post is licensed under CC BY 4.0 by the author.
Contents