MongoDB5.0.5 cluster

1. Install MongoDB on Windows platform

1.1. Download MongoDB

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

1.2. Install MongoDB

Double-click "mongodb-windows-x86_64-5.0.5-signed.msi", click "Next"
insert image description here
to agree to the installation agreement, click "Next" and
insert image description here
click "Custom" Set the installation directory
insert image description here
and select the installation location, click "Next" "
insert image description here
Modify the service configuration, click "Next"
insert image description here
and uncheck "install mongoDB compass" (of course you can also choose to install it, it may take longer to install), MongoDB Compass is a graphical interface management tool, we can install it later by ourselves Go to the official website to download and install, the download address: https://www.mongodb.com/download-center/compass, click "Next".
insert image description here
Click "Install" to start the installation.
insert image description here
To test whether the test is successful, open the cmd window and enter the commandmongo
insert image description here

1.3. Add environment variables

Right-click "Computer", then select "Properties", select "Advanced System Settings", select Environment Variables, and add an environment variable in " " under the "System Variables" window of the Pathenvironment variable, the content is binthe path of the directory under the installation location, here my path is D:\Java\MongoDB\Server\5.0\bin.
insert image description here
Re-open a new cmd window, enter the command mongo, and the installation is successful.
insert image description here

1.4, service startup/shutdown command

Run cmd as administrator to start/stop the mongodb service

# 启动mongodb服务
net start mongodb

# 关闭mongodb服务
net stop mongodb

Second, Linux traditional way to install MongoDB

Download address: https://www.mongodb.com/try/download/community , select the corresponding MongoDB version according to the version of the server, here is CentOS8 as an example.
insert image description here

Version 2.1, 5.0.5

## 下载
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-5.0.5.tgz

## 解压
tar -zxvf mongodb-linux-x86_64-rhel80-5.0.5.tgz

## 重命名
mv mongodb-linux-x86_64-rhel80-5.0.5 mongodb5.0.5

## 进入mongodb
cd mongodb5.0.5

## 修改数据日志文件路径
mkdir -p /opt/home/mongodb5.0.5/data
mkdir -p /opt/home/mongodb5.0.5/log
touch /opt/home/mongodb5.0.5/log/mongod.log

## 设置权限
chmod -R 777 /opt/home/mongodb5.0.5/data
chmod -R 777 /opt/home/mongodb5.0.5/log
chmod -R 777 /opt/home/mongodb5.0.5/log/mongod.log

## 进入mongodb安装的bin目录下
cd /opt/home/mongodb5.0.5/bin


## 创建mongodb.conf文件
vim mongodb.conf

## 添加下面的文件
#数据文件存放目录
dbpath = /opt/home/mongodb5.0.5/data
#日志文件存放地址
logpath =/opt/home/mongodb5.0.5/log/mongod.log
#端口
port = 27017 
#以守护程序的方式启用,即在后台运行
fork = true 
#需要认证。如果放开注释,就必须创建MongoDB的账号,使用账号与密码才可>远程访问,第一次安装建议注释
#auth=true 
#允许远程访问,或者直接注释,127.0.0.1是只允许本地访问
bind_ip=0.0.0.0 

## 配置环境变量
vim /etc/profile

## mongodb
export PATH=/opt/home/mongodb5.0.5/bin:$PATH

## 使配置文件生效
source /etc/profile

## 启动
./mongod --config ./mongodb.conf

## 测试
./mongo
> 1+2
3

Version 2.2, 4.4.10

## 下载
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-4.4.10.tgz

## 解压
tar -zxvf mongodb-linux-x86_64-rhel80-4.4.10.tgz

## 重命名
mv mongodb-linux-x86_64-rhel80-4.4.10 mongodb4.4.10

## 配置环境变量
vim /etc/profile

# mongodb
export PATH=/opt/home/mongodb4.4.10/bin:$PATH

## 使配置文件生效
source /etc/profile

## 修改数据日志文件路径
mkdir -p /opt/home/mongodb4.4.10/data
mkdir -p /opt/home/mongodb4.4.10/log
touch /opt/home/mongodb4.4.10/log/mongod.log

## 设置权限
chmod -R 777 /opt/home/mongodb4.4.10/data
chmod -R 777 /opt/home/mongodb4.4.10/log
chmod -R 777 /opt/home/mongodb4.4.10/log/mongod.log

## 进入mongodb安装的bin目录下
cd /opt/home/mongodb4.4.10/bin

## 创建mongodb.conf文件
vim mongodb.conf

#数据文件存放目录
dbpath = /opt/home/mongodb4.4.10/data
#日志文件存放地址
logpath =/opt/home/mongodb4.4.10/log/mongod.log
#端口
port = 27017 
#以守护程序的方式启用,即在后台运行
fork = true 
#需要认证。如果放开注释,就必须创建MongoDB的账号,使用账号与密码才可>远程访问,第一次安装建议注释
#auth=true 
#允许远程访问,或者直接注释,127.0.0.1是只允许本地访问
bind_ip=0.0.0.0 

## 启动
./mongod --config ./mongodb.conf

3. Install MongoDB by Docker

## 搜索镜像
docker search mongodb
 
## 拉取镜像
docker pull mongo
 
## 查看本地所有镜像 
docker iamges 
 
## 创建并运行nginx容器
#  -p 标识来指定容器端口绑定到主机端口。
#  -v 参数挂载数据卷
docker run -itd --name mymongo -p 27017:27017 -v /data/mongo:/data/db mongo

## 查看正在运行的容器
docker ps

## 查看端口
netstat -lnp | grep 27017

## 进入容器
docker exec -it mymongo bin/bash

## 进入mongo
mongo

## 输出3
1+2

Fourth, build a replica set

## 检查端口运行情况 如果正在运行 关闭服务
netstat -lnp | grep 27017

## 切换到 /opt/home目录下
cd /opt/home/
 
## 将mongodb5.0.5复制三份
cp -r mongodb5.0.5/ mongodb1
cp -r mongodb5.0.5/ mongodb2
cp -r mongodb5.0.5/ mongodb3

## 创建数据目录
mkdir -p /data/mongodb/data1
mkdir -p /data/mongodb/data2
mkdir -p /data/mongodb/data3


## 进入mongodb1的bin目录
cd mongodb1/bin/

## 修改mongodb.conf配置文件
vim mongodb.conf 

## 修改内容如下 修改datapath、logpath目录和port端口号
#数据文件存放目录
dbpath = /data/mongodb/data1
#日志文件存放地址
logpath =/data/mongodb/log1.log
#端口
port = 27018
#以守护程序的方式启用,即在后台运行
fork = true
#需要认证。如果放开注释,就必须创建MongoDB的账号,使用账号与密码才可>远程访问,第一次安装建议注释
#auth=true 
#允许远程访问,或者直接注释,127.0.0.1是只允许本地访问
bind_ip=0.0.0.0
replSet=myreplace/[192.168.200.128:27019,192.168.200.128:27020]

## 其余两个同上 端口分别为 27018 27019 27020

Connect three MongoDBs respectively

./mongo --port 27018
./mongo --port 27019
./mongo --port 27020

Configure a replica set to connect to any node

var config = { 
		_id:"myreplace", 
		members:[
		{_id:0,host:"192.168.200.128:27018"},
		{_id:1,host:"192.168.200.128:27019"},
		{_id:2,host:"192.168.200.128:27020"}]
}
## 初始化配置 
rs.initiate(config);

## 在主节点添加一条数据
use test;
db.test.insert({_id:1, name:'java'});
db.test.find();

## 设置客户端临时可以访问 分别在从节点执行查询命令db.test.find();
# 方式一
rs.slaveOk();
# 方式二
rs.secondaryOk();

# 查询
db.test.find();

## 手动摸拟异常 关闭主节点 查看从节点是否从新选举
# 结果 从节点会从新选举主节点 主节点再次启动后会自动变更为从节点

5. Sharded cluster

For the convenience of operation, I cancel the configuration file here and change it to add parameters at startup.

5.1. Cluster planning

- Shard Server 1:27017
- Shard Repl   1:27018

- Shard Server 2:27019
- Shard Repl   2:27020

- Shard Server 3:27021
- Shard Repl   3:27022

- Config Server :27023
- Config Server :27024
- Config Server :27025

- Route Process :27026

5.2, create a data directory

## s0
mkdir -p /data/mongodb/shard/s0
mkdir -p /data/mongodb/shard/s0-repl

## s1
mkdir -p /data/mongodb/shard/s1
mkdir -p /data/mongodb/shard/s1-repl

## s2
mkdir -p /data/mongodb/shard/s2
mkdir -p /data/mongodb/shard/s2-repl

## config
mkdir -p /data/mongodb/shard/config1
mkdir -p /data/mongodb/shard/config2
mkdir -p /data/mongodb/shard/config3

mkdir -p /data/mongodb/shard/config

5.3. Start 6 shard services

## 启动 s0、r0
./mongod --port 27017 --dbpath /data/mongodb/shard/s0 --bind_ip 0.0.0.0 --shardsvr --replSet r0/123.57.80.91:27018 --fork --logpath /data/mongodb/shard/s0/s0.log

./mongod --port 27018 --dbpath /data/mongodb/shard/s0-repl --bind_ip 0.0.0.0 --shardsvr --replSet r0/123.57.80.91:27017 --fork --logpath /data/mongodb/shard/s0-repl/s0-repl.log

# 登录任意节点
./mongo --port 27017

# 选择admin库
use admin

# 在admin中执行
config = {
    
     
	_id:"r0", members:[
      {
    
    _id:0,host:"123.57.80.91:27017"},
      {
    
    _id:1,host:"123.57.80.91:27018"}
    ]
}

# 初始化
rs.initiate(config);

## 启动 s1、r1
./mongod --port 27019 --dbpath /data/mongodb/shard/s1 --bind_ip 0.0.0.0 --shardsvr --replSet r1/123.57.80.91:27020 --fork --logpath /data/mongodb/shard/s1/s1.log

./mongod --port 27020 --dbpath /data/mongodb/shard/s1-repl --bind_ip 0.0.0.0 --shardsvr --replSet r1/123.57.80.91:27019 --fork --logpath /data/mongodb/shard/s1-repl/s1-repl.log

# 登录任意节点
./mongo --port 27019

# 在admin中执行
use admin

# 执行
config = {
    
     
	_id:"r1", members:[
      {
    
    _id:0,host:"123.57.80.91:27019"},
      {
    
    _id:1,host:"123.57.80.91:27020"}
    ]
}

# 初始化
rs.initiate(config);

## 启动 s2、r2
./mongod --port 27021 --dbpath /data/mongodb/shard/s2 --bind_ip 0.0.0.0 --shardsvr --replSet r2/123.57.80.91:27022 --fork --logpath /data/mongodb/shard/s2/s2.log

./mongod --port 27022 --dbpath /data/mongodb/shard/s2-repl --bind_ip 0.0.0.0 --shardsvr --replSet r2/123.57.80.91:27021 --fork --logpath /data/mongodb/shard/s2-repl/s2-repl.log

# 登录任意节点
./mongo --port 27017

# 选择admin库
use admin

# 在admin中执行
config = {
    
     
	_id:"r2", members:[
      {
    
    _id:0,host:"123.57.80.91:27021"},
      {
    
    _id:1,host:"123.57.80.91:27022"}
    ]
}

# 初始化
rs.initiate(config);

5.4, ​​start three config services

./mongod --port 27023 --dbpath /data/mongodb/shard/config1 --bind_ip 0.0.0.0 --replSet  config/[123.57.80.91:27024,123.57.80.91:27025] --configsvr --fork --logpath /data/mongodb/shard/config1/config.log

./mongod --port 27024 --dbpath /data/mongodb/shard/config2 --bind_ip 0.0.0.0 --replSet  config/[123.57.80.91:27023,123.57.80.91:27025] --configsvr --fork --logpath /data/mongodb/shard/config2/config.log

./mongod --port 27025 --dbpath /data/mongodb/shard/config3 --bind_ip 0.0.0.0 --replSet  config/[123.57.80.91:27023,123.57.80.91:27024] --configsvr --fork --logpath /data/mongodb/shard/config3/config.log

5.5. Initialize the config server replica set

`登录任意节点 congfig server`

./mongo --port 27023

# 选择数据库
use admin 

# 在admin中执行
config = {
    
     
    _id:"config", 
    configsvr: true,
    members:[
    	{
    
    _id:0,host:"123.57.80.91:27023"},
    	{
    
    _id:1,host:"123.57.80.91:27024"},
    	{
    
    _id:2,host:"123.57.80.91:27025"}
    ]
}
# 初始化副本集配置 
rs.initiate(config);

5.6. Start the mongos routing service

./mongos --port 27026 --configdb config/123.57.80.91:27023,123.57.80.91:27024,123.57.80.91:27025 --bind_ip 0.0.0.0 --fork --logpath /data/mongodb/shard/config/config.log

5.7. Log in to the mongos service

# 1.登录 
./mongo --port 27026

# 2.选择数据库
use admin

# 3.添加分片信息
db.runCommand({
    
     addshard:"r0/123.57.80.91:27017,123.57.80.91:27018","allowLocal":true });
db.runCommand({
    
     addshard:"r1/123.57.80.91:27019,123.57.80.91:27020","allowLocal":true });
db.runCommand({
    
     addshard:"r2/123.57.80.91:27021,123.57.80.91:27022","allowLocal":true });

# 4.指定分片的数据库
db.runCommand({
    
     enablesharding:"users" });

# 5.设置库的片键信息
db.runCommand({
    
     shardcollection: "users.user", key: {
    
     _id:1}});
db.runCommand({
    
     shardcollection: "users.emp", key: {
    
     _id: "hashed"}})

5.8. Test

test user collection

# 1.登陆27026节点
./mongo --port 27026

# 2.选择数据库
use users;

# 3.插入数据
for(let i=0;i<1000;i++){
    
    
 	db.user.insert({
    
    _id:i, name:"java_"+i, age: i});
}

# 4.验证27017
./mongo --port 27017
use users;
db.user.count();

# 5.验证27019
./mongo --port 27019
use users;
db.user.count();

# 6.验证27021
./mongo --port 27021
use users;
db.user.count();

insert image description hereinsert image description here
insert image description here
test emp collection

# 1.登陆27026节点
./mongo --port 27026

# 2.选择数据库
use users;

# 3.插入数据
for(let i=0;i<1000;i++){
    
    
 	db.emp.insert({
    
    _id:i, name:"java_"+i, age: i});
}

# 4.验证27017
./mongo --port 27017
use users;
db.emp.count();


# 5.验证27019
./mongo --port 27019
use users;
db.emp.count();

# 6.验证27021
./mongo --port 27021
use users;
db.emp.count();

insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_37242720/article/details/122680686