search
HomeDatabaseMysql TutorialRedis 3.0.3集群搭建

Redis3.0版本之后支持Cluster,具体介绍redis集群我就不多说,了解请看redis中文简介。首先,直接访问redis.io官网,下载redis.t

Redis3.0版本之后支持Cluster,具体介绍redis集群我就不多说,了解请看redis中文简介。

首先,直接访问redis.io官网,下载redis.tar.gz,现在版本3.0.3,我下面已经在虚拟机启动了两个linux来部署redis。

1. 下载和解包

  cd /usr/local/

  wget

  tar -zxvf redis-3.0.3.tar.gz

  mv redis-3.0.3  redis

2. 编译安装

  cd redis

  make && make install

  有些人在这里可能会碰到一个错误导致编译不过(如下)

 

make[1]: Entering directory `/redis/src'
CC adlist.o
In file included from adlist.c:34:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/redis/src'
make: *** [all] Error 2

 

  原因是没有安装jemalloc内存分配器,可以安装jemalloc 或 直接 输入make MALLOC=libc  && make install

3. 创建redis节点

    我们将在两台Server上创建六个节点,每台3个,3主3从。

  1)Server1

    cd /usr/local/

    mkdir redis_cluster  //创建集群目录

    mkdir 7000 7001 7002  //分别代表三个节点    其对应端口 7000 7001 7002

    创建7000节点为例,

    cd ./7000

    cp /usr/local/redis/redis.conf  ./    //拷贝到当前7000目录

    vi redis.conf    //编辑配置  主要修改一下几个参数

 

daemonize    yes                          //redis后台运行
pidfile  /var/run/redis_7000.pid    //pidfile文件对应7000
port  7000                                  //端口7000
cluster-enabled  yes                    //开启集群  把注释#去掉
cluster-config-file  nodes.conf      //集群的配置  配置文件首次启动自动生成
cluster-node-timeout  5000      //请求超时  设置5秒够了
appendonly  yes                        //aof日志开启  有需要就开启,它会每次写操作都记录一条日志

 

    配置好了,就相应地把这个修改后的配置文件拷贝到 7001  7002目录,注意要修改监听端口port 7001 7002.

    接下来,启动服务,进入节点目录

    依次执行  redis-server  redis.conf

    可以看到生成了appendonly.aof  nodes.conf

    ps -ef | grep redis 查看是否启动成功

      root 885 0.8 0.2 129452 2620 ? Ssl 20:10 0:21 redis-server *:7000 [cluster]
      root 887 0.8 0.2 129452 2616 ? Ssl 20:10 0:21 redis-server *:7001 [cluster]
      root 893 0.8 0.2 128356 2612 ? Ssl 20:10 0:21 redis-server *:7002 [cluster]

    netstat -tnlp | grep redis 可以看到redis监听端口

      tcp 0 0 0.0.0.0:7000 0.0.0.0:* LISTEN 885/redis-server *
      tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 887/redis-server *
      tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 893/redis-server *
      tcp 0 0 0.0.0.0:17000 0.0.0.0:* LISTEN 885/redis-server *
      tcp 0 0 0.0.0.0:17001 0.0.0.0:* LISTEN 887/redis-server *
      tcp 0 0 0.0.0.0:17002 0.0.0.0:* LISTEN 893/redis-server *

    我们除了看到 配置文件中设置的端口700*    还有700*+10000  (1700*), 前者是客户端访问的, 后者是集群内部节点之间访问的.

    注意,在多台Server间搭建集群,如果开了防火墙的,需要设置iptables开放上面所有端口.

 

  2) Server2

    步骤和Server1一样, 设置端口  7003  7004  7005

 

 4. 创建集群

  前面已经准备好了搭建集群的redis节点,接下来我们要把这些节点都串连起来搭建集群。官方提供了一个工具:redis-trib.rb  (/usr/local/redis/src/redis-trib.rb) 看后缀就知道这鸟东西不能直接执行,它是用ruby写的一个程序,所以我们还得安装ruby.

  yum -y install ruby ruby-devel rubygems rpm-build    //网上不明觉厉,都是这么安装的,就跟着这样玩吧

  再用 gem 这个命令来安装 redis接口    gem貌似是ruby的一个工具包  反正没错就是了。

  gem install redis    //等一会儿就好了

  当然,方便操作,两台Server都要安装。

 

  上面的步骤完事了,接下来运行一下redis-trib.rb

    /usr/local/redis/src/redis-trib.rb

 

Usage: redis-trib

  reshard        host:port
                  --to
                  --yes
                  --slots
                  --from
  check          host:port
  call            host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  add-node        new_host:new_port existing_host:existing_port
                  --master-id
                  --slave
  del-node        host:port node_id
  fix            host:port
  import          host:port
                  --from
  help            (show this help)
  create          host1:port1 ... hostN:portN
                  --replicas

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

 

    看到这,应该明白了吧, 就是靠上面这些操作 完成redis集群搭建的.

    确认所有的节点都启动,接下来使用参数create 创建    (在Server1中来创建)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.