MongoDB学习笔记:分片[通俗易懂]

MongoDB学习笔记:分片[通俗易懂]本文更新于2022-01-08,使用MongoDB 4.4.5。 单台服务器下创建分片 确保/data/db目录存在且当前系统用户有读写权限。例如(需根据实际情况设置权限): su root mkdi

MongoDB学习笔记:分片

本文更新于2022-01-08,使用MongoDB 4.4.5。

目录
  • 单台服务器下创建分片
  • 多台服务器下创建分片
    • 重新启动并连接到分片

单台服务器下创建分片

  1. 确保/data/db目录存在且当前系统用户有读写权限。例如(需根据实际情况设置权限):

    su root
    mkdir -p /data/db
    chmod 0777 /data /data/db
    
  2. 启动shell但不连接到任何mongod。

    mongo --nodb
    
  3. 创建分片集群(笔者的分片集群有3个分片,块大小为1MB)。会先删除原有数据库目录,再创建数据库目录和文件,启动mongos进程、配置副本集若干mongod进程(默认3个进程)、若干分片副本集的若干mongod进程(笔者指定为3个分片副本集,每个副本集默认1个进程),同时会不断输出所有节点的日志。

    var sharding = new ShardingTest({shards: 3, chunksize: 1});
    
  4. 启动shell连接到mongos(笔者使是localhost::20006),可执行查询操作。

    mongo mongodb://localhost:20006
    

    shell提示是“mongos”。

  5. 对数据库(笔者使用test数据库)启用分片。

    sh.enableSharding("test");
    
  6. 为片键(笔者使用users集合的username字段)建立索引。

    db.users.createIndex({username: 1});
    
  7. 根据片键(笔者使用username字段)对集合(笔者使用的users集合为数据库test的集合)分片。之后的查询就可以使用分片。

    sh.shardCollection("test.users", {username: 1});
    
  8. 在第一个shell中停止分片集群。会停止mongos进程、配置副本集所有mongod进程、所有分片副本集所有mongod进程。

    sharding.stop();
    

多台服务器下创建分片

此种方法也可在单台服务器下创建分片(笔者就以此方式使用)。

  1. 启动配置服务器

    1. 确保数据目录(笔者的副本集有3个节点,使用/data/db/config0、/data/db/config1、/data/db/config2)存在且当前系统用户有读写权限。例如(需根据实际情况设置权限):

      su root
      mkdir -p /data/db/config0
      mkdir -p /data/db/config1
      mkdir -p /data/db/config2
      chmod 0777 /data /data/db /data/db/config0 /data/db/config1 /data/db/config2
      
    2. 使用不同的shell启动副本集节点(笔者的副本集名字为config),必需指定绑定的IP(笔者使用0.0.0.0),如果在单台服务器下需指定不同的数据目录(如笔者在上一步所述)和端口(笔者使用40000、40001、40002)。会创建数据库文件。

      mongod --configsvr --replSet config --dbpath /data/db/config0 --port 40000 --bind_ip 0.0.0.0
      mongod --configsvr --replSet config --dbpath /data/db/config1 --port 40001 --bind_ip 0.0.0.0
      mongod --configsvr --replSet config --dbpath /data/db/config2 --port 40002 --bind_ip 0.0.0.0
      
    3. 启动shell连接到任意一个节点(笔者使用端口40000的节点)。

      mongo mongodb://localhost:40000
      
    4. 初始化副本集,会从备份节点中选举出主节点(笔者的副本集名字为config,各节点为localhost:40000、localhost:40001、localhost:40002)。如果副本集曾经初始化,则不用执行此步骤。

      rs.initiate({
      	_id: "config",
      	members: [
      		{
      			"_id": 0,
      			"host": "localhost:40000"
      		},
      		{
      			"_id": 1,
      			"host": "localhost:40001"
      		},
      		{
      			"_id": 2,
      			"host": "localhost:40002"
      		}
      	]
      });
      
  2. 启动mongos

    需指定配置服务器(笔者使用的副本集名字为config,各节点为localhost:40000、localhost:40001、localhost:40002。另外,笔者的mongos使用端口60000)。

    mongos --configdb config/localhost:40000,localhost:40001,localhost:40002 --port 60000
    
  3. 启动若干个副本集

    1. 确保数据目录(笔者使用3个副本集,每个副本集有1个节点,使用/data/db/shard0-0、/data/db/shard1-0、/data/db/shard2-0)存在且当前系统用户有读写权限。例如(需根据实际情况设置权限):

      su root
      mkdir -p /data/db/shard0-0
      mkdir -p /data/db/shard1-0
      mkdir -p /data/db/shard2-0
      chmod 0777 /data /data/db /data/db/shard0-0 /data/db/shard1-0 /data/db/shard2-0
      
    2. 使用不同的shell启动副本集节点(笔者的副本集名字为shard0、shard1、shard2),必需指定绑定的IP(笔者使用0.0.0.0),如果在单台服务器下需指定不同的数据目录(如笔者在上一步所述)和端口(笔者使用50000、50001、50002)。会创建数据库文件。

      mongod --shardsvr --replSet shard0 --dbpath /data/db/shard0-0 --port 50000 --bind_ip 0.0.0.0
      mongod --shardsvr --replSet shard1 --dbpath /data/db/shard1-0 --port 50001 --bind_ip 0.0.0.0
      mongod --shardsvr --replSet shard2 --dbpath /data/db/shard2-0 --port 50002 --bind_ip 0.0.0.0
      
    3. 初始化所有副本集(笔者只列出名字为shard0的副本集的步骤。名字为shard1和shard2的副本集,同理)。如果副本集曾经初始化,则不用执行此步骤。

      1. 启动shell连接到副本集节点(笔者示例使用名字为shard0的副本集,其节点的端口为50000。端口为50001和50002的节点,同理)。

        mongo mongodb://localhost:50000
        
      2. 初始化副本集,会从备份节点中选举出主节点(笔者示例使用名字为shard0的副本集,其节点为localhost:50000。名字为shard1的副本集的节点为localhost:50001,名字为shard2的副本集的节点为localhost:50002,同理)。

        rs.initiate({
        	_id: "shard0",
        	members: [
        		{
        			"_id": 0,
        			"host": "localhost:50000"
        		}
        	]
        });
        
  4. 将所有副本集转换为分片

    1. 启动shell连接到mongos(笔者为localhost:60000)。

      mongo mongodb://localhost:60000
      
    2. 将所有副本集(即笔者使用的shard0/localhost:50000、shard1/localhost:50001、shard2/localhost:50002)添加为分片。

      sh.addShard("shard0/localhost:50000");
      sh.addShard("shard1/localhost:50001");
      sh.addShard("shard2/localhost:50002");
      
  5. 数据分片

    1. 启动shell连接到mongos(笔者为localhost:60000)。

      mongo mongodb://localhost:60000
      
    2. 对数据库(笔者使用test数据库)启用分片。

      sh.enableSharding("test");
      
    3. 为片键(笔者使用users集合的username字段)建立索引。

      db.users.createIndex({username: 1});
      
    4. 根据片键(笔者使用username字段)对集合(笔者使用的users集合为数据库test的集合)分片。之后的查询就可以使用分片。

      sh.shardCollection("test.users", {username: 1});
      

重新启动并连接到分片

对于使用上述方式创建的分片,如需重新启动并连接到分片,只需启动不同的shell执行其中若干个步骤,如下:

mongod --configsvr --replSet config --dbpath /data/db/config0 --port 40000 --bind_ip 0.0.0.0
mongod --configsvr --replSet config --dbpath /data/db/config1 --port 40001 --bind_ip 0.0.0.0
mongod --configsvr --replSet config --dbpath /data/db/config2 --port 40002 --bind_ip 0.0.0.0
mongos --configdb config/localhost:40000,localhost:40001,localhost:40002 --port 60000
mongod --shardsvr --replSet shard0 --dbpath /data/db/shard0-0 --port 50000 --bind_ip 0.0.0.0
mongod --shardsvr --replSet shard1 --dbpath /data/db/shard1-0 --port 50001 --bind_ip 0.0.0.0
mongod --shardsvr --replSet shard2 --dbpath /data/db/shard2-0 --port 50002 --bind_ip 0.0.0.0
mongo mongodb://localhost:60000

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/5519.html

(0)
上一篇 2023-05-06
下一篇 2023-05-06

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注