记得关注一下博主,博主每天都会更新IT技术,让你有意想不到的小收获哦^_^
文章目录
- *`记得关注一下博主,博主每天都会更新IT技术,让你有意想不到的小收获哦^_^`*
- 一、离线安装docker服务(三台MongoDB 分片服务器都要安装)
- 二、离线安装compose组件(三台MongoDB 分片服务器都要安装)
- 三、导入MongoDB docker镜像并启动(三台MongoDB 分片服务器都要操作)
- 四、登录mdb01服务器初始化config服务
- 五、登录mdb01服务器初始化shard分片
- 六、登录Mongos路由控制台添加分片
- 七、登录mongos路由控制台开启数据库分片功能并插入测试数据
- 八、MongoDB 分片集群开启用户认证
- 九、MongoDB 分片集群备份与恢复数据库指定集合数据及分片功能
- 十、MongoDB 分片集群备份与恢复整个数据库
- 常用命令列表
服务器规划
服务器类型 | 主机名称 | IP地址 | CPU核数 | 内存 | 磁盘 | 网卡数量 |
---|---|---|---|---|---|---|
MongoDB分片服务器01 | mdb01 | 192.168.91.61 | 2*4 | 16G | 512G | 1张网卡 |
MongoDB分片服务器02 | mdb02 | 192.168.91.62 | 2*4 | 16G | 512G | 1张网卡 |
MongoDB分片服务器03 | mdb03 | 192.168.91.63 | 2*4 | 16G | 512G | 1张网卡 |
每台服务器容器规划(三台服务器使用容器都一样)
容器类型 | 容器名称 | 端口 | CPU | 内存 | 磁盘 | 备注 |
---|---|---|---|---|---|---|
MongoDB配置服务容器 | rs_config_server | 27019 | 2*4 | 16GB | 512GB | |
MongoDB路由服务容器 | rs_mongos_server | 27017 | 2*4 | 16GB | 512GB | |
MongoDB分片容器1 | rs_shard_server_01 | 27118 | 2*4 | 16GB | 512GB | |
MongoDB分片容器2 | rs_shard_server_02 | 27218 | 2*4 | 16GB | 512GB | |
MongoDB分片容器3 | rs_shard_server_03 | 27318 | 2*4 | 16GB | 512GB |
软件版本
软件/系统 | 版本 |
---|---|
CentOS | 7.9 |
MongoDB | 4.4.2 |
Docker | 20.10.7 |
Docker-compose | 2.14.0 |
目录说明
目录路径 | 说明 |
---|---|
/data/db | 存放MongoDB数据库文件 |
/data/docker | 存放docker 相关包 |
一、离线安装docker服务(三台MongoDB 分片服务器都要安装)
提示:Docker离线安装包下载地址: https://download.docker.com/linux/static/stable/x86_64/docker-20.10.7.tgz
1、关闭防火墙和SElinux
systemctl stop firewalld
systemctl disable firewalld
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
setenforce 0
2、上传docker安装包
mkdir -p /data/software
[root@mdb01 ~]# ls -l /data/software/
total 68092
-rw-r--r-- 1 root root 69725147 Feb 15 16:04 docker-20.10.7.tgz
3、创建docker安装脚本
[root@mdb01 ~]# vim /data/software/install_docker.sh
#!/bin/bash
echo '解压tar包...'
sudo tar -xvf $1
echo '将docker目录移到/usr/bin目录下...'
sudo cp docker/* /usr/bin/
echo '将docker.service 移到/etc/systemd/system/ 目录...'
sudo cp docker.service /etc/systemd/system/
echo '添加文件权限...'
sudo chmod +x /etc/systemd/system/docker.service
echo '重新加载配置文件...'
sudo systemctl daemon-reload
echo '启动docker...'
sudo systemctl start docker
echo '设置开机自启...'
sudo systemctl enable docker
echo 'docker安装成功...'
docker -v
4、创建docker服务启动脚本
[root@mdb01 ~]# vim /data/software/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
5、执行docker安装脚本
chmod +x /data/software/install_docker.sh
cd /data/software/
sh install_docker.sh docker-20.10.7.tgz
docker -v
二、离线安装compose组件(三台MongoDB 分片服务器都要安装)
提示:Docker离线安装包下载地址: https://github.com/docker/compose/releases/download/v2.14.0/docker-compose-linux-x86_64
1、解压compose安装包并授权使用
mv /data/software/docker-compose-linux-x86_64 /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -v
三、导入MongoDB docker镜像并启动(三台MongoDB 分片服务器都要操作)
1、导入mongo docker镜像
mkdir -p /data/software/
cd /data/software/
docker load < mongo.tar
2、编写docker-compose.ymal启动文件
mkdir -p /data/db
cat > /data/db/docker-compose.yaml << 'EOF'
version: '3.9'
networks:
mongo-network:
external: false
services:
# 配置服务器configsvr
rs_config_server:
image: mongo:4.4.2
networks:
- mongo-network
container_name: rs_config_server
restart: always
ports:
- 27019:27019
command: --configsvr --replSet "rs_config_server" --bind_ip_all
volumes:
- ${PWD}/data/db:/data/db
- ${PWD}/data/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
# shard分片1
rs_shard_server_01:
image: mongo:4.4.2
networks:
- mongo-network
container_name: rs_shard_server_01
restart: always
ports:
- 27118:27018
command: --shardsvr --replSet "rs_shard_server_01" --bind_ip_all
volumes:
- ${PWD}/shard1/db:/data/db
- ${PWD}/shard1/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
# shard分片2
rs_shard_server_02:
image: mongo:4.4.2
networks:
- mongo-network
container_name: rs_shard_server_02
restart: always
ports:
- 27218:27018
command: --shardsvr --replSet "rs_shard_server_02" --bind_ip_all
volumes:
- ${PWD}/shard2/db:/data/db
- ${PWD}/shard2/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
# shard分片3
rs_shard_server_03:
image: mongo:4.4.2
networks:
- mongo-network
container_name: rs_shard_server_03
restart: always
ports:
- 27318:27018
command: --shardsvr --replSet "rs_shard_server_03" --bind_ip_all
volumes:
- ${PWD}/shard3/db:/data/db
- ${PWD}/shard3/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
# 配置路由 mongos
rs_mongos_server:
image: mongo:4.4.2
networks:
- mongo-network
container_name: rs_mongos_server
restart: always
ports:
- 27017:27017
# 这里的 rs_config_server 是 配置的 --replSet "rs_config_server"
# 将三台机器的configsvr都写在这个位置
command: mongos --configdb rs-config-server/192.168.91.61:27019,192.168.91.62:27019,192.168.91.63:27019 --bind_ip_all
volumes:
- ${PWD}/mongos/db:/data/db
- ${PWD}/mongos/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
EOF
3、分别登录mdb01服务器、mdb02服务器、mdb03服务器启动MongoDB 容器
cd /data/db/
docker-compose -f docker-compose.yaml up -d
docker-compose ps
4、分别登录mdb01服务器、mdb02服务器、mdb03服务器安装mongodb客户端
rpm -ivh /data/software/mongodb-mongosh-2.2.9.x86_64.rpm
mongosh -version
四、登录mdb01服务器初始化config服务
提示:只需要在mdb01服务器初始化MongoDB服务配置操作,不需要登录其它两台MongoDB分片服务器操作
### 登录config服务主节点控制台
mongosh --host 192.168.91.61 --port 27019
### 在MongoDB控制台执行初始化MongoDB服务配置
rs.initiate({
_id: "rs_config_server",
configsvr: true,
members: [
{ _id : 0, host : "192.168.91.61:27019" },
{ _id : 1, host : "192.168.91.62:27019" },
{ _id : 2, host : "192.168.91.63:27019" }
]
});
### 查看初化MongoDB服务配置状态
rs.status()
五、登录mdb01服务器初始化shard分片
提示:只需要在mdb01服务器操作,不需要登录其它两台MongoDB分片服务器操作
1、进入rs_shard_server_01分片主节点进行分片初始化操作
mongosh --host 192.168.91.61 --port 27118
### 执行初始化rs_shard_server_01分片容器
rs.initiate({
_id: "rs_shard_server_01",
members: [
{ _id : 0, host : "192.168.91.61:27118" },
{ _id : 1, host : "192.168.91.62:27118" },
{ _id : 2, host : "192.168.91.63:27118" },
]
});
### 查看rs_shard_server_01分片状态
rs.status()
2、进入rs_shard_server_02分片主节点进行分片初始化操作
mongosh --host 192.168.91.61 --port 27218
### 执行初始化rs_shard_server_02分片容器
rs.initiate({
_id: "rs_shard_server_02",
members: [
{ _id : 0, host : "192.168.91.61:27218" },
{ _id : 1, host : "192.168.91.62:27218" },
{ _id : 2, host : "192.168.91.63:27218" },
]
});
### 查看rs_shard_server_02分片状态
rs.status()
3、进入rs_shard_server_03分片主节点进行分片初始化操作
mongosh --host 192.168.91.61 --port 27318
### 执行初始化rs_shard_server_03分片容器
rs.initiate({
_id: "rs_shard_server_03",
members: [
{ _id : 0, host : "192.168.91.61:27318" },
{ _id : 1, host : "192.168.91.62:27318" },
{ _id : 2, host : "192.168.91.63:27318" },
]
});
### 查看rs_shard_server_03分片状态
rs.status()
六、登录Mongos路由控制台添加分片
### 进入rs_mongos_server路由控制
mongosh --host 192.168.91.61 --port 27017
###添加rs_shard_server_01分片
sh.addShard("rs_shard_server_01/192.168.91.61:27118,192.168.91.62:27118,192.168.91.63:27118");
###添加rs_shard_server_02分片
sh.addShard("rs_shard_server_02/192.168.91.61:27218,192.168.91.62:27218,192.168.91.63:27218");
###添加rs_shard_server_03分片
sh.addShard("rs_shard_server_03/192.168.91.61:27318,192.168.91.62:27318,192.168.91.63:27318");
### 查看分片情况
sh.status()
七、登录mongos路由控制台开启数据库分片功能并插入测试数据
1、登录mongos路由控制台开启school数据库分片功能
mongosh --host 192.168.91.61 --port 27017
### 进入到admin数据库
use admin
### 开启school数据库分片功能(默认自动分片)
db.runCommand( { enablesharding : "school" } );
### 设置school数据库student集合(表),_id为片键使用hashed哈希算法
db.runCommand({ shardcollection : "school.student",key : {"_id": "hashed"}} );
### 查看分片情况
sh.status();
2、在school数据库插入测试数据
mongosh --host 192.168.91.61 --port 27017
show dbs
use school
### 复制以下SQL语句插入测试数据
for (var i = 1; i <= 100000; i++){
db.student.insert({id:i,"001":"xiaoming"});
}
show collections;
db.student.stats().count;
3、查看分片集群状态
mongosh --host 192.168.91.61 --port 27017
### 查看分片集群状态
sh.status();
### 查询school数据库的student集合数据分布详细情况
use school
db.student.getShardDistribution()
八、MongoDB 分片集群开启用户认证
注意:登录mongos路由服务添加的管理员用户会同步到 config 配置服务中,但是不会同步到所有shard分片中,因此创建管理员用户的操作需要分别在 rs_shard_server_01分片主节点容器、rs_shard_server_02分片主节点容器、 rs_shard_server_03分片主节点容器上执行。
1、登录任意一台mongos路由控制台创建管理员
mongosh --host 192.168.91.61 --port 27017
### 进入admin数据库
use admin
### 创建管理员用户root
db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
### 验证管理员用户root账号和密码是否登录正常
db.auth("root","123456")
### 查询已创建的用户
db.system.users.find();
2、登录rs_shard_server_01分片主节点创建管理员用户
mongosh --host 192.168.91.61 --port 27118
### 进入admin数据库
use admin
### 创建管理员用户root
db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
### 验证管理员用户root账号和密码是否登录正常
db.auth("root","123456")
### 查询已创建的用户
db.system.users.find();
3、登录rs_shard_server_02分片主节点创建管理员用户
mongosh --host 192.168.91.61 --port 27218
### 进入admin数据库
use admin
### 创建管理员用户root
db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
### 验证管理员用户root账号和密码是否登录正常
db.auth("root","123456")
### 查询已创建的用户
db.system.users.find();
4、登录rs_shard_server_03分片主节点创建管理员用户
mongosh --host 192.168.91.61 --port 27318
### 进入admin数据库
use admin
### 创建管理员用户root
db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
### 验证管理员用户root账号和密码是否登录正常
db.auth("root","123456")
### 查询已创建的用户
db.system.users.find();
5、登录mdb01服务器创建keyfile并远程复制一份keyfile给mdb02服务器和mdb03服务器
### 创建keyfile
cd /data/db/
openssl rand -base64 756 > key.file
chmod 600 /data/db/key.file
chown 999 /data/db/key.file
### 远程复制一份给mdb02服务器和mdb03服务器
scp -rp /data/db/key.file root@192.168.91.62:/data/db/
scp -rp /data/db/key.file root@192.168.91.63:/data/db/
### (特别注意)分别登录mdb02服务器和mdb03服务器授权key.file属主
chown 999 /data/db/key.file
6、登录mdb01服务器、mdb02服务器、mdb03服务器修改docker-compose.yaml配置文件
提示:三台服务器docker-compose.yaml配置文件内容是一致的,命令指定keyfile文件并增加需要认证参数--auth
cp -rp /data/db/docker-compose.yaml /tmp/
cat > /data/db/docker-compose.yaml << 'EOF'
version: '3.9'
networks:
mongo-network:
external: false
services:
# 配置服务器configsvr
rs_config_server:
image: mongo:4.4.2
networks:
- mongo-network
container_name: rs_config_server
restart: always
ports:
- 27019:27019
command: --configsvr --replSet "rs_config_server" --bind_ip_all --keyFile "/data/mongodb/key.file" --auth
volumes:
- ${PWD}/data/db:/data/db
- ${PWD}/data/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
# shard分片1
rs_shard_server_01:
image: mongo:4.4.2
networks:
- mongo-network
container_name: rs_shard_server_01
restart: always
ports:
- 27118:27018
command: --shardsvr --replSet "rs_shard_server_01" --bind_ip_all --keyFile "/data/mongodb/key.file" --auth
volumes:
- ${PWD}/shard1/db:/data/db
- ${PWD}/shard1/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
# shard分片2
rs_shard_server_02:
image: mongo:4.4.2
networks:
- mongo-network
container_name: rs_shard_server_02
restart: always
ports:
- 27218:27018
command: --shardsvr --replSet "rs_shard_server_02" --bind_ip_all --keyFile "/data/mongodb/key.file" --auth
volumes:
- ${PWD}/shard2/db:/data/db
- ${PWD}/shard2/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
# shard分片3
rs_shard_server_03:
image: mongo:4.4.2
networks:
- mongo-network
container_name: rs_shard_server_03
restart: always
ports:
- 27318:27018
command: --shardsvr --replSet "rs_shard_server_03" --bind_ip_all --keyFile "/data/mongodb/key.file" --auth
volumes:
- ${PWD}/shard3/db:/data/db
- ${PWD}/shard3/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
# 配置路由 mongos
rs_mongos_server:
image: mongo:4.4.2
networks:
- mongo-network
container_name: rs_mongos_server
restart: always
ports:
- 27017:27017
# 这里的 rs_config_server 是 配置的 --replSet "rs_config_server"
# 将三台机器的configsvr都写在这个位置
command: mongos --configdb rs-config-server/192.168.91.61:27019,192.168.91.62:27019,192.168.91.63:27019 --bind_ip_all --keyFile "/data/mongodb/key.file"
volumes:
- ${PWD}/mongos/db:/data/db
- ${PWD}/mongos/configdb:/data/configdb
- ${PWD}/key.file:/data/mongodb/key.file
EOF
7、重新部署生成MonogoDB分片集群服务(三台主机都要执行)
cd /data/db/
docker-compose stop
cd /data/db/
docker-compose -f docker-compose.yaml up -d
8、登录mogos路由控制台和分片进行用户认证测试
mongosh --host 192.168.91.61 --port 27017 -u root -p 123456
sh.status()
use school
db.student.find().count()
exit
mongosh --host 192.168.91.61 --port 27118 -u root -p 123456
rs.status()
use school
db.student.find().count()
exit
mongosh --host 192.168.91.61 --port 27218 -u root -p 123456
rs.status()
use school
db.student.find().count()
exit
mongosh --host 192.168.91.61 --port 27318 -u root -p 123456
rs.status()
use school
db.student.find().count()
exit
九、MongoDB 分片集群备份与恢复数据库指定集合数据及分片功能
1、登录任意一台宿主机安装mongodump和mongorestore备份与恢复工具
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel70-x86_64-100.6.1.tgz -P /mnt
tar xf /mnt/mongodb-database-tools-rhel70-x86_64-100.6.1.tgz -C /mnt/
mv /mnt/mongodb-database-tools-rhel70-x86_64-100.6.1/bin/* /usr/local/bin/
mongodump --version
2、登录任意一台宿主机通过mogos路由容器备份MongoDB数据库指定school库的student集合(student表)
mkdir -p /backup
### 如果MongoDB数据库设置了用户认证使用以下命令进行备份
mongodump --host 192.168.91.61 --port 27017 -u "root" -p "123456" --authenticationDatabase admin -d school -c student -o /backup
### 如果MongoDB数据库没有设置用户认证使用以下命令进行备份
mongodump --host 192.168.91.61 --port 27017 -d school -c student -o /backup
3、登录任意一台mogos路由容器只删除school数据库中student集合
docker exec -it rs_mongos_server /bin/bash
mongo --host 192.168.91.61 --port 27017
show dbs
use school
show collections
db.student.stats().count;
### 查看当前school数据库中student集合是否开启了分片功能,如果sharded值为true说明使用了分片功能,sharded值为false说明没用使用分片功能
db.student.stats()
### 删除student集合
db.student.drop()
### 查询student集合是否有数据
db.student.stats().count;
### 查看当前school数据库中student集合是否开启了分片功能,如果sharded值为true说明使用了分片功能,sharded值为false说明没用使用分片功能
db.student.stats()
4、登录任意一台mogos路由容器开启school数据库中student集合的分片功能
注意:在导入MongoDB备份数据之前,必须先执行设置分片功能,否则导入的MonogoDB备份数据将无分片功能
docker exec -it rs_mongos_server /bin/bash
mongo --host 192.168.91.61 --port 27017
### 查看当前数据库和集合的分片功能详细信息
sh.status()
### 如果school数据库之前已开启过分片,这里可以不操作,因为只删除了集合没有删除库
sh.enableSharding('school')
### 设置school数据库student集合(表),_id为片键使用hashed哈希算法
sh.shardCollection('school.student', {"_id": 'hashed'})
### 查看当前数据库和集合的分片功能详细信息
sh.status()
### 查看当前school数据库中student集合分片信息和数据
use school
db.student.getShardDistribution()
5、登录任意一台宿主机通过mogos路由容器恢复MongoDB数据库指定school库的student集合(student表)
mkdir -p /backup
### 如果MongoDB数据库设置了用户认证使用以下命令进行恢复
mongorestore --host 192.168.91.61 --port 27017 -u "root" -p "123456" -d school -c student --authenticationDatabase admin --dir=/backup/school/student.bson
### 如果MongoDB数据库没有设置用户认证使用以下命令进行恢复
mongorestore --host 192.168.91.61 --port 27017 -d school -c student --dir=/backup/school/student.bson
6、登录任意一台mogos路由容器验证
docker exec -it rs_mongos_server /bin/bash
mongo --host 192.168.91.61 --port 27017
show dbs
use school
show collections
db.student.stats().count;
sh.status()
db.student.getShardDistribution()
十、MongoDB 分片集群备份与恢复整个数据库
1、登录任意一台宿主机安装mongodump和mongorestore备份与恢复工具
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel70-x86_64-100.6.1.tgz -P /mnt
tar xf /mnt/mongodb-database-tools-rhel70-x86_64-100.6.1.tgz -C /mnt/
mv /mnt/mongodb-database-tools-rhel70-x86_64-100.6.1/bin/* /usr/local/bin/
mongodump --version
2、登录任意一台宿主机通过mogos路由容器备份MongoDB数据库指定school数据库
mkdir -p /backup
### 如果MongoDB数据库设置了用户认证使用以下命令进行备份
mongodump --host 192.168.91.61 --port 27017 -u "root" -p "123456" --authenticationDatabase admin -d school -o /backup
### 如果MongoDB数据库没有设置用户认证使用以下命令进行备份
mongodump --host 192.168.91.61 --port 27017 -d school -o /backup
3、登录任意一台mogos路由容器只删除school数据库
docker exec -it rs_mongos_server /bin/bash
mongo --host 192.168.91.61 --port 27017
show dbs
use school
### 查看当前数据库和集合的分片功能详细信息
sh.status()
### 删除school整个数据库
db.dropDatabase()
### 查看当前数据库和集合的分片功能详细信息
sh.status()
4、登录任意一台mogos路由容器开启school数据库和student集合的分片功能
注意:在导入MongoDB备份数据之前,必须先执行设置分片功能,否则导入的MonogoDB备份数据将无分片功能
docker exec -it rs_mongos_server /bin/bash
mongo --host 192.168.91.61 --port 27017
### 查看当前数据库和集合的分片功能详细信息
sh.status()
### 开启school数据库分片功能
sh.enableSharding('school')
### 设置school数据库student集合(表),_id为片键使用hashed哈希算法
sh.shardCollection('school.student', {"_id": 'hashed'})
### 查看当前数据库和集合的分片功能详细信息
sh.status()
5、登录任意一台宿主机通过mogos路由容器恢复MongoDB数据库
mkdir -p /backup
### 如果MongoDB数据库设置了用户认证使用以下命令进行恢复
mongorestore --host 192.168.91.61 --port 27017 -u "root" -p "123456" -d school --authenticationDatabase admin --dir=/backup/school/
### 如果MongoDB数据库没有设置用户认证使用以下命令进行恢复
mongorestore --host 192.168.91.61 --port 27017 -d school --dir=/backup/school/
6、登录任意一台mogos路由容器验证
docker exec -it rs_mongos_server /bin/bash
mongo --host 192.168.91.61 --port 27017
show dbs
use school
show collections
db.student.stats().count;
sh.status()
db.student.getShardDistribution()
常用命令列表
# 数据库 命令
db.serverStatus().connections; //连接数查看
show collections //查看表信息
db.test_shard.find().count() //查看table1数据长度
db.test_shard.remove({}) //删除数据表
db.stats() //查看所有的分片服务器状态
db.adminCommand( { listShards: 1 } ) //分片列表
db.test_shard.find({ age: 36 }).explain() //精确查询
db.test_shard.find({ age: { $gte : 36 ,$lt : 73 } }).explain() //范围查询
# 分片 操作命令
sh.enableSharding('testdb') //开启数据库testdb分片
sh.shardCollection('testdb.users',{uid:1}) //按testdb.users的uid字段分片
sh.shardCollection("testdb.test_shard",{"age": 1}) //按ranged分片
sh.shardCollection("testdb.test_shard2",{"age": "hashed"}) //按hash分片
sh.status() //查看分片节点
sh.addShard() //向集群中添加一个 shard
sh.getBalancerState() //查看平衡器
sh.disableBalancing() //禁用平衡器
sh.enableBalancing() //启用平衡器
db.runCommand( { removeShard: "mongodb0" } ) //删除分片mongodb0,迁移数据查看命令
db.runCommand( { movePrimary: "test", to: "mongodb1" }) //将数据库test未分片mongodb0的数据,迁移到mongodb1主分片。
db.adminCommand("flushRouterConfig") //处理分片后,刷新路由数据。
use config
db.databases.find() //查看所有数据库使用分片
db.settings.save({_id:"chunksize",value:1}) //将 chunk 的大小调整为 1MB
db.serverStatus().sharding
# 副本集 操作命令
rs.status() //查看成员的运行状态等信息
rs.config() //查看配置信息
rs.slaveOk() //允许在SECONDARY节点上进行查询操作,默认从节点不具有查询功能
rs.isMaster() //查询该节点是否是主节点
rs.add({}) //添加新的节点到该副本集中
rs.remove() //从副本集中删除节点
rs.stepDown //降级节点
db.printSlaveReplicationInfo() //查看同步情况
rs.addArb("172.20.0.16:27038") //添加仲裁节点
rs.add({_id : 2, host : "192.168.1.22:26003", arbiterOnly:true}) //添加仲裁节点
# 强制加入仲裁节点:
config=rs.conf()
config.members=[config.members[0],config.members[1],{_id:5,host:"127.0.0.1:27023",priority:5,arbiterOnly:"true"}]
rs.reconfig(config,{force:true})
# 强制主节点:
cfg = rs.conf()
cfg.members[0].priority = 0.5
cfg.members[1].priority = 0.5
cfg.members[2].priority = 1
rs.reconfig(cfg)
# 备份/恢复
mongodump -h 127.0.0.1:27017 -d test -o /data/backup/
mongorestore -h 127.0.0.1:27017 -d test --dir /data/db/test