mongodb--分片架构【待填的坑】
2024-08-28 04:10:11
首先有一个问题没有搞懂:什么是自动分片?用脚本吗?
一: 多机方式中的另一种方式【分片 => sharding】
分片的对象的谁? 对一个【集合 => 表】进行拆分,把一个大数据拆分成多个小数据
分片的依据
就是对 表 中的某一个key进行分片。。。 比方说???
我们有一个age【int】,如果说你有2个片,那么int类型可能会被同等拆分。。。。一个片占用一半。
当然,所有的操作都是由mongodb自己去完成的。
二:画一个图,看看mongodb中如果采用分片架构。
所有的client请求我们都是由 mongos去转向的。
127.0.0.1:27017 作为 mongos服务器
127.0.0.1:27018 作为 config 服务器
127.0.0.1:28000 作为一个sharing1服务器
127.0.0.1:28001 作为一个sharing2服务器
第一步:开启config服务器。 其实就是一个普通的mongod服务。
第二步:开启mongos服务,,,
第三步:开启两个片键。
第四步: 初始化配置
- 将要作为“片”的机器放到mongos的config配置中。
connecting to: 127.0.0.1:27017/test
mongos> use admin
switched to db admin
mongos> db.runCommand({"addshard":"127.0.0.1:28000","allowLocal":true})
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({"addshard":"127.0.0.1:28001","allowLocal":true})
{ "shardAdded" : "shard0001", "ok" : 1 }
- 你要划分的集合和键值
在没有分片的时候,插入数据,这个时候数据去哪了??? 数据是在其中的某一个分片中
connecting to: 127.0.0.1:27017/test
mongos> use admin
switched to db admin
mongos> db.runCommand({"addshard":"127.0.0.1:28000","allowLocal":true})
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({"addshard":"127.0.0.1:28001","allowLocal":true})
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> use test
switched to db test
mongos> test.person.insert({"name":"jack","age":1})
Mon Dec 21 07:16:35.924 ReferenceError: test is not defined
mongos> db.person.insert({"name":"jack","age":1})
mongos> db.person.find()
{ "_id" : ObjectId("5677edaaceeb3008ebe79831"), "name" : "jack", "age" : 1 }
mongos> use admin
switched to db admin
mongos> db.runCommand({"enablesharding":"test"})
{ "ok" : 1 }
mongos> db.runCommand({"shardcollection":"test.person","key":{"age":1}})
{
"proposedKey" : {
"age" : 1
},
"curIndexes" : [
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.person",
"name" : "_id_"
}
],
"ok" : 0,
"errmsg" : "please create an index that starts with the shard key before sharding."
}
一定要在分片之前创建 “片键” 的索引
mongos> use test
switched to db test
mongos> db.person.createIndex({"age":1})
mongos> use admin
switched to db admin
mongos> db.runCommand({"shardcollection":"test.person","key":{"age":1}})
{ "collectionsharded" : "test.person", "ok" : 1 }
最新文章
- C#开发奇技淫巧三:把dll放在不同的目录让你的程序更整洁
- SQLSERVER JDBC 存储过程调用偶尔很慢的原因之一【sp_sproc_columns】
- UITableView去掉section的header的粘性
- Java for LeetCode 150 Evaluate Reverse Polish Notation
- PHP中,JS和CSS优化工具Minify的使用方法
- 几种任务调度的 Java 实现方法与比较--转载
- 关于java socket(转)
- MySQL远程訪问的两个问题
- mysql数据库在windows下安装与配置
- Chrome浏览器下自动填充的输入框背景
- Jquery自动补全插件的使用
- 《高性能Mysql》解读---Mysql的事务和多版本并发
- Python之路系列笔记
- python numpy 数组拼接
- Unicode,GBK,GB2312,UTF-8概念基础(转载)
- EL表达式取值中文再发送请求时会乱码
- JS进阶系列之作用域链
- Hadoop配置文件详解
- SPI协议及其工作原理浅析【转】
- jquery中children()
热门文章
- Effective Java(二)—— 循环与 StringBuilder
- C#(服务器)与Java(客户端)通过Socket传递对象(序列化 json)
- Find Minimum in Rotated Sorted Array 典型二分查找
- [Apple开发者帐户帮助]二、管理你的团队(3)删除团队成员
- elasticearch 归并策略
- 使用IDEA 搭建一个SpringBoot + Hibernate + Gradle
- 无桌面的linux 安装VMWare Tools
- SLAM:使用G2O-ORB-SLAM(编译)
- iOS 加密算法汇总
- Git 从github克隆文件至本地