IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> Ansible_Mongodb分片搭建.sh -> 正文阅读

[大数据]Ansible_Mongodb分片搭建.sh

#Ansible搭建Mongodb分片集群整个过程记录
#分片计划c
mongodb_db1 xxx.xxx.xxx.100
mongodb_db2 xxx.xxx.xxx.120
mongodb_db3 xxx.xxx.xxx.130
|---------------------------------------------------------------------------------------  
|mongodb_db1 	|				mongodb_db2 		|			mongodb_db3       |       |
|Primary    				|			  Secondary 						|		  Secondary               |shard1 |
|Secondary 					|				Primary   						|			Secondary               |shard2 |
|Secondary  				|				Secondary  						|			Primary                 |shard3 |
|Config             |       Config                |     Config                  |       | 
|mongos							|				mongos                |     mongos 					        |       |
-----------------------------------------------------------------------------------------   

#ansible配置
[all]
xxx.xxx.xxx.100 ansible_ssh_user=root
xxx.xxx.xxx.120 ansible_ssh_user=root
xxx.xxx.xxx.130 ansible_ssh_user=root
1、关闭防火墙
ansible all -m shell -a "systemctl stop firewalld.service"
ansible all -m shell -a "systemctl disable firewalld.service"
ansible all -m shell -a "systemctl status firewalld.service"

ansible all -m shell -a "setenforce 0"
ansible all -m shell -a "sed -i '/^SELINUX=/s/enforcing/disabled/' /etc/selinux/config"
ansible all -m shell -a "getenforce"

2、设置hostname
vim /etc/hosts	
xxx.xxx.xxx.100
127.0.0.1 mongodb_db1
hostnamectl set-hostname mongodb_db1
xxx.xxx.xxx.120
127.0.0.1 mongodb_db2
hostnamectl set-hostname mongodb_db2
xxx.xxx.xxx.130
127.0.0.1 mongodb_db3
hostnamectl set-hostname mongodb_db3

3、配置hosts
cat >>/etc/hosts<< EOF
mongodb_db1  xxx.xxx.xxx.100
mongodb_db2  xxx.xxx.xxx.120
mongodb_db3  xxx.xxx.xxx.130
EOF

2、配置环境变量(三台服务器都配置)
vim /etc/profile
添加mysql环境变量
export MONGODB_HOME="/usr/local/mongodb/"
export PATH="$PATH:$MONGODB_HOME/bin"
source /etc/profile
		

#创建mongo目录
ansible all -m shell -a "mkdir /software/mongodb/shard{1,2,3}/data -p"
ansible all -m shell -a "mkdir /software/mongodb/shard{1,2,3}/log -p"
ansible all -m shell -a "mkdir /software/mongodb/config/log -p"
ansible all -m shell -a "mkdir /software/mongodb/config/data -p"
ansible all -m shell -a "mkdir /software/mongodb/mongos/log -p"
ansible all -m shell -a "mkdir /software/mongodb/conf/log -p"
ansible all -m shell -a "mkdir /software/mongodb/key/ -p"

#生成keyfile文件 集群之间通讯
touch /software/mongodb/mongodb-keyfile
ansible all -m shell -a "chmod -R 755 /usr/local/mongodb/bin/"
openssl rand -base64 745 >/software/mongodb/key/mongodb-keyfile
chmod 600 /software/mongodb/key/mongodb-keyfile

#keyfile传输到其他节点
ansible all -m copy -a "src=/software/mongodb/key/mongodb-keyfile  dest=/software/mongodb/key/ chmod=0600"
ansible all -m shell -a "chmod 600 /software/mongodb/key/mongodb-keyfile"


#复制集shard1搭建过程Begin
touch /software/mongodb/conf/shard1.conf 

cat >> /software/mongodb/conf/shard1.conf << EOF
pidfilepath = /software/mongodb/shard1/log/shard1.pid
dbpath = /software/mongodb/shard1/data
logpath = /software/mongodb/shard1/log/shard1.log
logappend = true
bind_ip = 0.0.0.0
port = 27001
fork = true
httpinterface=true
rest=true
replSet=shard1
#shardsvr = true
maxConns=20000
#clusterAuthMode=keyFile
#keyFile=/software/mongodb/key/mongdb-keyfile
EOF

ansible all -m copy -a "src=/software/mongodb/conf/shard1.conf  dest=/software/mongodb/conf/shard1.conf "

#启动shard1
ansible all -m shell -a "/usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf"
#随便找一台初始化shard1
mongo --port 27001
use admin 
config = {_id : "shard1",members : [
	{_id : 0, host : "xxx.xxx.xxx.100:27001" },
	{_id : 1, host : "xxx.xxx.xxx.120:27001" },
	{_id : 2, host : "xxx.xxx.xxx.130:27001"}
	]}
	
rs.initiate(config);
rs.status();
shard1:PRIMARY> rs.status()
{
        "set" : "shard1",
        "date" : ISODate("2022-05-10T06:56:46.165Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1652165804, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1652165804, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1652165804, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "xxx.xxx.xxx.100:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 424,
                        "optime" : {
                                "ts" : Timestamp(1652165804, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-05-10T06:56:44Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1652165742, 1),
                        "electionDate" : ISODate("2022-05-10T06:55:42Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "xxx.xxx.xxx.120:27001",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 75,
                        "optime" : {
                                "ts" : Timestamp(1652165804, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1652165804, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-05-10T06:56:44Z"),
                        "optimeDurableDate" : ISODate("2022-05-10T06:56:44Z"),
                        "lastHeartbeat" : ISODate("2022-05-10T06:56:44.233Z"),
                        "lastHeartbeatRecv" : ISODate("2022-05-10T06:56:45.553Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "xxx.xxx.xxx.100:27001",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "xxx.xxx.xxx.130:27001",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 75,
                        "optime" : {
                                "ts" : Timestamp(1652165804, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1652165804, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-05-10T06:56:44Z"),
                        "optimeDurableDate" : ISODate("2022-05-10T06:56:44Z"),
                        "lastHeartbeat" : ISODate("2022-05-10T06:56:44.217Z"),
                        "lastHeartbeatRecv" : ISODate("2022-05-10T06:56:45.272Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "xxx.xxx.xxx.100:27001",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
shard1:PRIMARY> 
复制集shard1搭建过程End
#同样的方法搭建shard2和shard3
#shard2复制集搭建
touch /software/mongodb/conf/shard2.conf 
touch /software/mongodb/conf/shard3.conf 


cat >> /software/mongodb/conf/shard2.conf << EOF
pidfilepath = /software/mongodb/shard2/log/shard2.pid
dbpath = /software/mongodb/shard2/data
logpath = /software/mongodb/shard2/log/shard2.log
logappend = true
bind_ip = 0.0.0.0
port = 27002
fork = true
httpinterface=true
rest=true
replSet=shard2
#shardsvr = true
maxConns=20000
#clusterAuthMode=keyFile
#keyFile=/software/mongodb/key/mongdb-keyfile
EOF

cat >> /software/mongodb/conf/shard3.conf << EOF
pidfilepath = /software/mongodb/shard3/log/shard3.pid
dbpath = /software/mongodb/shard3/data
logpath = /software/mongodb/shard3/log/shard3.log
logappend = true
bind_ip = 0.0.0.0
port = 27003
fork = true
httpinterface=true
rest=true
replSet=shard3
#shardsvr = true
maxConns=20000
#clusterAuthMode=keyFile
#keyFile=/software/mongodb/key/mongdb-keyfile
EOF

ansible all -m copy -a "src=/software/mongodb/conf/shard2.conf  dest=/software/mongodb/conf/shard2.conf "
ansible all -m copy -a "src=/software/mongodb/conf/shard3.conf  dest=/software/mongodb/conf/shard3.conf "

#启动shard2  启动shard3
ansible all -m shell -a "/usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf"
ansible all -m shell -a "/usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf"

#随便找一台初始化shard2  shard3
mongo --port 27002
use admin 
config = {_id : "shard2",members : [
	{_id : 0, host : "xxx.xxx.xxx.100:27002" },
	{_id : 1, host : "xxx.xxx.xxx.120:27002" },
	{_id : 2, host : "xxx.xxx.xxx.130:27002"}
	]}
	
rs.initiate(config);
rs.status();

mongo --port 27003
use admin 
config = {_id : "shard3",members : [
	{_id : 0, host : "xxx.xxx.xxx.100:27003" },
	{_id : 1, host : "xxx.xxx.xxx.120:27003" },
	{_id : 2, host : "xxx.xxx.xxx.130:27003"}
	]}
	
rs.initiate(config);
rs.status();

#到这里 三个独立的副本集shard1 shard2 shard3已搭建完成
#检查
ansible all -m shell -a "ps -ef |grep mongodb |grep -v grep"
xxx.xxx.xxx.120 | CHANGED | rc=0 >>
root      22259      1  5 04:23 ?        00:02:03 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf
root      22422      1  5 04:23 ?        00:01:55 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf
root      22609      1  5 04:23 ?        00:01:51 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf
xxx.xxx.xxx.100 | CHANGED | rc=0 >>
root      41253      1  5 04:23 ?        00:02:03 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf
root      41467      1  4 04:23 ?        00:01:41 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf
root      41697      1  4 04:23 ?        00:01:40 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf
xxx.xxx.xxx.130 | CHANGED | rc=0 >>
root      21119      1  5 04:23 ?        00:01:57 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf
root      21298      1  5 04:23 ?        00:01:53 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf
root      21461      1  5 04:23 ?        00:01:53 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf
#配置集config服务搭建
touch /software/mongodb/conf/config.conf
cat >> /software/mongodb/conf/config.conf <<EOF
pidfilepath = /software/mongodb/config/log/configsrv.pid
dbpath = /software/mongodb/config/data
logpath = /software/mongodb/config/log/configsrv.log
logappend = true
bind_ip = 0.0.0.0
port = 20000
fork = true
configsvr = true
replSet=configs
maxConns=20000	
slowms = 500
storageEngine = wiredTiger
#auth = true
#clusterAuthMode=keyFile
#keyFile=/software/mongodb/key/mongdb-keyfile
EOF
ansible all -m copy -a "src=/software/mongodb/conf/config.conf dest=/software/mongodb/conf/config.conf"
#配置集启动
ansible all -m shell -a "/usr/local/mongodb/bin/mongod -f /software/mongodb/conf/config.conf"
#检查
ansible all -m shell -a "ps -ef |grep mongodb |grep -v grep"
xxx.xxx.xxx.120 | CHANGED | rc=0 >>
root      22105      1  1 04:22 ?        00:00:35 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/config.conf
root      22259      1  5 04:23 ?        00:02:03 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf
root      22422      1  5 04:23 ?        00:01:55 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf
root      22609      1  5 04:23 ?        00:01:51 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf
xxx.xxx.xxx.100 | CHANGED | rc=0 >>
root      41043      1  1 04:22 ?        00:00:31 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/config.conf
root      41253      1  5 04:23 ?        00:02:03 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf
root      41467      1  4 04:23 ?        00:01:41 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf
root      41697      1  4 04:23 ?        00:01:40 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf
xxx.xxx.xxx.130 | CHANGED | rc=0 >>
root      20963      1  1 04:22 ?        00:00:38 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/config.conf
root      21119      1  5 04:23 ?        00:01:57 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf
root      21298      1  5 04:23 ?        00:01:53 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf
root      21461      1  5 04:23 ?        00:01:53 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf
#初始化配置集config 串联起来config 形成config复制集
mongo --port 20000
use admin
config = {
    _id : "configs",
     members : [
        {_id : 0, host : "xxx.xxx.xxx.100:20000" },
        {_id : 1, host : "xxx.xxx.xxx.120:20000" },
        {_id : 2, host : "xxx.xxx.xxx.130:20000" },

     ]
 }
 rs.initiate(config);
 
configs:PRIMARY> rs.status()
{
        "set" : "configs",
        "date" : ISODate("2022-05-10T07:17:09.705Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "configsvr" : true,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1652167019, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1652167019, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1652167019, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1652167019, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "xxx.xxx.xxx.100:20000",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 162,
                        "optime" : {
                                "ts" : Timestamp(1652167019, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-05-10T07:16:59Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1652167013, 1),
                        "electionDate" : ISODate("2022-05-10T07:16:53Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "xxx.xxx.xxx.120:20000",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 26,
                        "optime" : {
                                "ts" : Timestamp(1652167019, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1652167019, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-05-10T07:16:59Z"),
                        "optimeDurableDate" : ISODate("2022-05-10T07:16:59Z"),
                        "lastHeartbeat" : ISODate("2022-05-10T07:17:07.760Z"),
                        "lastHeartbeatRecv" : ISODate("2022-05-10T07:17:08.642Z"),
                        "pingMs" : NumberLong(1),
                        "syncingTo" : "xxx.xxx.xxx.100:20000",
                        "configVersion" : 1
                },
                {
                        "_id" : 2,
                        "name" : "xxx.xxx.xxx.130:20000",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 26,
                        "optime" : {
                                "ts" : Timestamp(1652167019, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1652167019, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2022-05-10T07:16:59Z"),
                        "optimeDurableDate" : ISODate("2022-05-10T07:16:59Z"),
                        "lastHeartbeat" : ISODate("2022-05-10T07:17:07.719Z"),
                        "lastHeartbeatRecv" : ISODate("2022-05-10T07:17:08.638Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "xxx.xxx.xxx.100:20000",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
#初始化配置集config已OK

#mongos搭建(config配置服务串联)
touch /software/mongodb/conf/mongos.conf
cat >>/software/mongodb/conf/mongos.conf<<EOF
pidfilepath = /software/mongodb/mongos/log/mongos.pid
logpath = /software/mongodb/mongos/log/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 27017
fork = true
configdb = configs/xxx.xxx.xxx.100:20000,xxx.xxx.xxx.120:20000,xxx.xxx.xxx.130:20000
maxConns=20000
#clusterAuthMode=keyFile
#keyFile=/software/mongodb/key/mongdb-keyfile
EOF

ansible all -m copy -a "src=/software/mongodb/conf/mongos.conf dest=/software/mongodb/conf/mongos.conf"

#启动mongos
ansible all -m shell -a "/usr/local/mongodb/bin/mongos -f /software/mongodb/conf/mongos.conf"

#连接mongs 或者连接mongo --port 2000 配置  最终修改的数据都保存在configf服务里面  config配置服务串联副本集
mongo
MongoDB shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.4
Server has startup warnings: 
2022-05-10T03:21:38.385-0400 I CONTROL  [main] 
2022-05-10T03:21:38.386-0400 I CONTROL  [main] ** WARNING: Access control is not enabled for the database.
2022-05-10T03:21:38.386-0400 I CONTROL  [main] **          Read and write access to data and configuration is unrestricted.
2022-05-10T03:21:38.386-0400 I CONTROL  [main] ** WARNING: You are running this process as the root user, which is not recommended.
2022-05-10T03:21:38.386-0400 I CONTROL  [main] 
mongos> 
mongos> 
mongos> 
mongos> 
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("627a116735ff728348f313ba")
}
  shards:
  active mongoses:
        "3.4.4" : 3
 autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
                Balancer lock taken at Tue May 10 2022 03:16:56 GMT-0400 (EDT) by ConfigServer:Balancer
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
  
#添加分片(串联副本集)
mongos> sh.addShard("shard1/xxx.xxx.xxx.100:27003,192.168.100.120:27003,xxx.xxx.xxx.130:27003")
{ "shardAdded" : "shard1", "ok" : 1 }

sh.addShard("shard2/xxx.xxx.xxx.100:27002,xxx.xxx.xxx.120:27002,xxx.xxx.xxx.130:27002")
{ "shardAdded" : "shard2", "ok" : 1 }

sh.addShard("shard3/xxx.xxx.xxx.100:27003,xxx.xxx.xxx.120:27003,xxx.xxx.xxx.130:27003")
{ "shardAdded" : "shard3", "ok" : 1 }

#检查
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("627a116735ff728348f313ba")
}
  shards:
        {  "_id" : "shard1",  "host" : "shard1/xxx.xxx.xxx.100:27001,xxx.xxx.xxx.120:27001,xxx.xxx.xxx.130:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/xxx.xxx.xxx.100:27002,xxx.xxx.xxx.120:27002,xxx.xxx.xxx.130:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/xxx.xxx.xxx.100:27003,xxx.xxx.xxx.120:27003,xxx.xxx.xxx.130:27003",  "state" : 1 }
  active mongoses:
        "3.4.4" : 3
 autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
                Balancer lock taken at Tue May 10 2022 03:16:56 GMT-0400 (EDT) by ConfigServer:Balancer
        Failed balancer rounds in last 5 attempts:  5
        Last reported error:  Cannot accept sharding commands if not started with --shardsvr
        Time of Reported error:  Tue May 10 2022 03:47:17 GMT-0400 (EDT)
        Migration Results for the last 24 hours: 
                No recent migrations
  databases:
  
#启用分片生效
配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片。连接在mongos上,准备让指定的数据库、指定的集合分片生效。
mongo --port 27017
 db.runCommand( { enablesharding :"Hobosondb"});
{
        "ok" : 0,
        "errmsg" : "enableSharding may only be run against the admin database.",
        "code" : 13,
        "codeName" : "Unauthorized"
}
#ERROR 没有切换use admin
mongos> use admin
switched to db admin
mongos> db.runCommand( { enablesharding :"Hobosondb"});
{
        "code" : 193,
        "ok" : 0,
        "errmsg" : "Cannot accept sharding commands if not started with --shardsvr"
}
#ERROR没有启动分片配置
加上分片--shardsvr
ansible all -m shell -a "sed -i 's@#shardsvr = true@shardsvr = true@'   /software/mongodb/conf/*.conf"


use  admin
db.runCommand( { enablesharding :"Hobosondb"});
db.runCommand( { shardcollection : "Hobosondb.Hobosontable",key : {"id": "hashed"} } );
 
 
 use  admin
switched to db admin
mongos> db.runCommand( { enablesharding :"Hobosondb"});
{ "ok" : 1 }
mongos> db.runCommand( { shardcollection : "Hobosondb.HobosontableNoShard",key : {"id": "hashed"} } );
{ "collectionsharded" : "Hobosondb.Hobosontable", "ok" : 1 }


#造数据测试分片
use Hobosondb
for(i=1;i<=100000;i++){db.Hobosontable.insert({"id":i,"name":"penglei"})};


 
#统计:
use Hobosondb
db.Hobosontable.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])


#据分布均匀
shard1:PRIMARY> db.Hobosontable.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])
{ "_id" : "penglei", "totle" : 67510 }
shard3:PRIMARY> db.Hobosontable.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])
{ "_id" : "penglei", "totle" : 66204 }
shard2:PRIMARY> db.Hobosontable.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])
{ "_id" : "penglei", "totle" : 66286 }

#未分片的(未分片的数据存储在shard2上面)sh.status() database里面可以看到Primary
use Hobosondb
for(i=1;i<=100;i++){db.HobosontableNoShard.insert({"id":i,"name":"penglei"})};
use Hobosondb
db.HobosontableNoShard.aggregate([{$group : {_id : "$name", totle : {$sum : 1}}}])

ongos> sh.status()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("627a116735ff728348f313ba")
}
  shards:
        {  "_id" : "shard1",  "host" : "shard1/xxx.xxx.xxx.100:27001,xxx.xxx.xxx.120:27001,xxx.xxx.xxx.130:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/xxx.xxx.xxx.100:27002,xxx.xxx.xxx.120:27002,xxx.xxx.xxx.130:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/xxx.xxx.xxx.100:27003,xxx.xxx.xxx.120:27003,xxx.xxx.xxx.130:27003",  "state" : 1 }
  active mongoses:
        "3.4.4" : 3
 autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
                Balancer lock taken at Tue May 10 2022 04:23:10 GMT-0400 (EDT) by ConfigServer:Balancer
        Failed balancer rounds in last 5 attempts:  5
        Last reported error:  Cannot accept sharding commands if not started with --shardsvr
        Time of Reported error:  Tue May 10 2022 04:21:33 GMT-0400 (EDT)
        Migration Results for the last 24 hours: 
                2 : Success
  databases:
        {  "_id" : "Hobosondb",  "primary" : "shard2",  "partitioned" : true }  #未分片的数据存储在shard2上面
                Hobosondb.Hobosontable
                        shard key: { "id" : "hashed" }
                        unique: false
                        balancing: true
                        chunks:
                                shard1  2
                                shard2  2
                                shard3  2
                        { "id" : { "$minKey" : 1 } } -->> { "id" : NumberLong("-6148914691236517204") } on : shard1 Timestamp(3, 2) 
                        { "id" : NumberLong("-6148914691236517204") } -->> { "id" : NumberLong("-3074457345618258602") } on : shard1 Timestamp(3, 3) 
                        { "id" : NumberLong("-3074457345618258602") } -->> { "id" : NumberLong(0) } on : shard2 Timestamp(3, 4) 
                        { "id" : NumberLong(0) } -->> { "id" : NumberLong("3074457345618258602") } on : shard2 Timestamp(3, 5) 
                        { "id" : NumberLong("3074457345618258602") } -->> { "id" : NumberLong("6148914691236517204") } on : shard3 Timestamp(3, 6) 
                        { "id" : NumberLong("6148914691236517204") } -->> { "id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 7) 
                        
                        
                        

#检查
ansible all -m shell -a "ps -ef |grep mongodb |grep -v grep"
xxx.xxx.xxx.130 | CHANGED | rc=0 >>
root      18041      1  0 03:21 ?        00:00:15 /usr/local/mongodb/bin/mongos -f /software/mongodb/conf/mongos.conf
root      20963      1  1 04:22 ?        00:00:40 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/config.conf
root      21119      1  5 04:23 ?        00:01:59 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf
root      21298      1  5 04:23 ?        00:01:55 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf
root      21461      1  5 04:23 ?        00:01:54 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf
xxx.xxx.xxx.120 | CHANGED | rc=0 >>
root      21388      1  0 03:21 ?        00:00:18 /usr/local/mongodb/bin/mongos -f /software/mongodb/conf/mongos.conf
root      22105      1  1 04:22 ?        00:00:37 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/config.conf
root      22259      1  5 04:23 ?        00:02:04 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf
root      22422      1  5 04:23 ?        00:01:57 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf
root      22609      1  5 04:23 ?        00:01:53 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf
xxx.xxx.xxx.100 | CHANGED | rc=0 >>
root      39340      1  1 03:21 ?        00:01:10 /usr/local/mongodb/bin/mongos -f /software/mongodb/conf/mongos.conf
root      41043      1  1 04:22 ?        00:00:33 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/config.conf
root      41253      1  5 04:23 ?        00:02:04 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf
root      41467      1  4 04:23 ?        00:01:42 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf
root      41697      1  4 04:23 ?        00:01:42 /usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf

#杀掉
ansible all -m shell -a "killall mongod"

ansible all -m shell -a "/usr/local/mongodb/bin/mongod -f /software/mongodb/conf/config.conf"
#ansible all -m shell -a "/usr/local/mongodb/bin/mongos -f /software/mongodb/conf/mongos.conf"
ansible all -m shell -a "/usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard1.conf"
ansible all -m shell -a "/usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard2.conf"
ansible all -m shell -a "/usr/local/mongodb/bin/mongod -f /software/mongodb/conf/shard3.conf"


#总结:
#1 先搭建三个独立的复制集  复制集名称分别为shard1 shard2 shard3 ,复制集为1Primary2Secondary
#2 在搭建一个config配置服务  复制集为1Primary2Secondary
#3 启动三个mongos服务 mongos里面串联起来config复制集 添加分片 将mongod也串联起来
#4 为了方便整过过程没有权限认证 生成的集群认证key-files 后面启动认真需要用到  这里就不删除了 人懒不想删除了

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-05-11 16:30:44  更:2022-05-11 16:31:14 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 23:08:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码