Windows

下载

https://www.mongodb.com/try/download/community

install-mongodb-on-windows

https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-windows/

image-20220503092132342

image-20220503092218302

image-20220503092243822

Install mongosh

https://www.mongodb.com/try/download/shell

image-20220503095810497

连接MongoDB

https://www.mongodb.com/docs/mongodb-shell/connect/#std-label-mdb-shell-connect

默认端口27017

连接本地Server
1
2
3
4
5
PS C:\Users\Qingyuan_Qu> mongosh
Current Mongosh Log ID: 62708c7078e29ade98bc9a22
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.3.1
Using MongoDB: 5.0.8
Using Mongosh: 1.3.1
连接远程Server

在MongoDB官网创建Serverhttps://cloud.mongodb.com/v2/6282ff0bc14e6e66f7e35b2b#clusters

1
2
3
4
5
6
7
8
9
10
11
12
13
PS C:\Users\Qingyuan_Qu> mongosh "mongodb+srv://cluster0.0excx.mongodb.net/myFirstDatabase" --apiVersion 1 --username quqingyuan
Enter password: **********
Current Mongosh Log ID: 6283008702ad5651115e0c21
Connecting to: mongodb+srv://cluster0.0excx.mongodb.net/myFirstDatabase?appName=mongosh+1.3.1
Using MongoDB: 5.0.8 (API Version 1)
Using Mongosh: 1.3.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Atlas atlas-wighfk-shard-0 [primary] myFirstDatabase> show dbs;
admin 287 kB
local 1.76 GB
Atlas atlas-wighfk-shard-0 [primary] myFirstDatabase>

Linux

1
2
# MongoDB支持YUM、RPM、压缩包等方式进行安装。此文档通过YUM方式安装。
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-red-hat/

下载

1
2
# 下载Centos7, tgz
https://www.mongodb.com/try/download/community

安装

1
2
3
4
5
6
(python37) [zhangsan@node0 bigdata]$ mkdir mongodb

# 上传到安装目录
# 解压
# 软连接
#
软件目录结构
1
2
3
4
5
(python37) [zhangsan@node0 bin]$ ll -h
-rwxr-xr-x. 1 zhangsan zhangsan 15K Apr 22 05:15 install_compass
-rwxr-xr-x. 1 zhangsan zhangsan 56M Apr 22 06:01 mongo # Client
-rwxr-xr-x. 1 zhangsan zhangsan 105M Apr 22 06:02 mongod # Server
-rwxr-xr-x. 1 zhangsan zhangsan 73M Apr 22 05:40 mongos # Router

启动

mongod命令介绍

查看mongod帮助:mongod -h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--logpath arg
# Log file to send write to instead of stdout - has to be a file, not directory
# (指定记录日志的文件,不要指定文件夹!)

--dbpath arg
# Directory for datafiles - defaults to /data/db
#(指定文件夹存储数据。默认/data/db)

--fork
# Fork server process
#(fork一个服务(子)进程,fork成功后,本(父)进程退出。)

--bind_ip_all
# 允许所有ip访问Server

$MONGODB_HOME中创建一个日志(logs)存放目录和数据(data)存放目录。

1
2
(python37) [zhangsan@node0 default]$ mkdir data
(python37) [zhangsan@node0 default]$ mkdir logs
服务端启动
1
2
3
4
(python37) [zhangsan@node0 default]$ bin/mongod --fork --dbpath data/ --logpath logs/log
about to fork child process, waiting until server is ready for connections.
forked process: 3415
child process started successfully, parent exiting
客户端连接
1
(python37) [zhangsan@node0 default]$ bin/mongo

停止服务器

1
2
3
4
5
6
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
> exit
bye

副本集

相当于HDFSReplication

1
https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set/
配置文件

MongoDB 配置文件使用YAML格式 [ 1 ]

mongod.yaml

1
2
3
4
5
6
7
8
9
10
11
12
systemLog:
destination: file
path: "/opt/bigdata/mongodb/default/logs/mongod.log"
storage:
dbPath: "/opt/bigdata/mongodb/default/data/"
processManagement:
fork: true
net:
bindIp: 0.0.0.0
port: 27017
replication:
replSetName: "rs0"

如果允许任意IP地址访问,请输入0.0.0.0

启动
1
bin/mongod --config conf/mongod.yaml
初始化

进入客户端 bin/mongo

1
2
3
4
5
6
7
8
9
var init_config = {
_id : "rs0",
members: [
{ _id: 0, host: "node1:27017" },
{ _id: 1, host: "node2:27017" },
{ _id: 2, host: "node3:27017" }
]
}
rs.initiate(init_config)
确定主从
1
rs.status()
从节点访问
1
rs0:SECONDARY> rs.secondaryOk()
主节点离线

KillPrimary,查看副本集集群状态。

分片集群

分片介绍:

1
https://www.mongodb.com/docs/manual/sharding/

部署分片集群

1
https://www.mongodb.com/docs/manual/tutorial/deploy-shard-cluster/

概述

本教程涉及创建一个新的分片集群,该集群由三个 mongos配置服务器副本集和两个分片副本集组成。

步骤

创建 Config Server Replica Set

1. 启动config server副本集中的每个成员

配置文件

vim conf/config-server.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
systemLog:
destination: file
path: "/opt/bigdata/mongodb/default/logs/config-server.log"
storage:
dbPath: "/opt/bigdata/mongodb/default/data/configdb"
sharding:
clusterRole: configsvr
replication:
replSetName: myConfigReplSet
net:
bindIp: 0.0.0.0
port: 27019
processManagement:
fork: true

创建文件夹configdbdata/configdblogs

启动
1
bin/mongod --config conf/config-server.yaml

2. 连接其中一个 config server

1
$ bin/mongo --host node1 --port 27019

3. 初始化副本集

1
2
3
4
5
6
7
8
9
10
var config_server = {
_id: "myConfigReplSet",
configsvr: true,
members: [
{ _id : 0, host : "node1:27019" },
{ _id : 1, host : "node2:27019" },
{ _id : 2, host : "node3:27019" }
]
}
rs.initiate(config_server)
确定主从
1
2
myReplSet:PRIMARY> rs.status()
# "configsvr" : true,

此时,配置服务器副本集中会创建三个数据库:adminconfiglocal

创建Shard Replica Sets

1. 启动shard副本集中的每个成员

配置文件

shard-server.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
systemLog:
destination: file
path: "/opt/bigdata/mongodb/default/logs/shard-server.log"
storage:
dbPath: "/opt/bigdata/mongodb/default/data/sharddb"
sharding:
clusterRole: shardsvr
replication:
replSetName: myShardReplSet
net:
bindIp: 0.0.0.0
port: 27018
processManagement:
fork: true

创建文件夹。

1
(python37) [zhangsan@node1 default]$ mkdir data/sharddb
启动
1
bin/mongod --config conf/shard-server.yaml

2. 连接其中一台shard replica set

1
bin/mongo --host node1 --port 27018

3. 初始化副本集

1
2
3
4
5
6
7
8
9
var shard_server = {
_id : "myShardReplSet",
members: [
{ _id : 0, host : "node1:27018" },
{ _id : 1, host : "node2:27018" },
{ _id : 2, host : "node3:27018" }
]
}
rs.initiate(shard_server)
确定主从
1
myReplSet:SECONDARY> rs.status()

启动一个路由服务 mongos

配置文件

vim conf/router-server.yaml

1
2
3
4
5
6
7
8
9
10
systemLog:
destination: file
path: "/opt/bigdata/mongodb/default/logs/router-server.log"
sharding:
configDB: myConfigReplSet/node1:27019,node2:27019,node3:27019
net:
bindIp: 0.0.0.0
port: 27017
processManagement:
fork: true
启动mongos
1
bin/mongos --config conf/router-server.yaml

连接分片集群

1
bin/mongo --host node1 --port 27017
1
mongos> show dbs;

添加分片到集群

1
mongos> sh.addShard( "myShardReplSet/node1:27018,node2:27018,node3:27018")

重复这些步骤,直到集群包含所有所需的碎片。

为数据库启用分片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mongos> use mydb;

mongos> sh.enableSharding("mydb")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1653368824, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1653368824, 2)
}

一旦为一个数据库启用了分片,MongoDB就会为该数据库分配一个主分片,MongoDB将所有数据存储在该数据库中。

对集合分片

在对Collection进行分片之前,必须首先为Collection 所在的Database启用分片。

要对Collection 进行分片,请将mongosh连接到mongos,使用sh.shardCollection()方法操作。

MongoDB提供了两种策略对Collection 进行分片。

散列分片使用单个字段的散列索引作为分片键,跨分片集群对数据进行分区。

1
#sh.shardCollection("<database>.<collection>", { <shard key field> : "hashed" } )

例子:

1
2
3
sh.shardCollection("mydb.hashedShardColl", { code : "hashed" })
for(var i =1;i<=100;i++){db.hashedShardColl.insertOne({code:i})}
db.hashedShardColl.getShardDistribution()

基于范围的分片可以使用多个字段作为shard key,根据shard key的值将数据划分为连续的范围。

1
sh.shardCollection("<database>.<collection>", { <shard key field> : 1, ... } )

例子:

1
2
3
sh.shardCollection("mydb.rangeBasedShardColl", { code : 1 })
for(var i =1;i<=100;i++){db.rangeBasedShardColl.insertOne({code:i})}
db.rangeBasedShardColl.getShardDistribution()

分片集群状态

1
2
3
4
5
6
7
8
9
use config;
db.databases.find()
db.shards.find()
db.chunks.find()


sh.status()
db.hashedShardColl.getShardDistribution()

新增Shard Replica Sets

略。

可视化管理

compass

1
https://www.mongodb.com/try/download/compass?tck=docs_compass

WSL-MongoDB

1
https://docs.microsoft.com/zh-cn/windows/wsl/tutorials/wsl-database#install-mongodb