0

0

Redis集群搭建教程的总结

不言

不言

发布时间:2018-07-28 10:25:21

|

1810人浏览过

|

来源于php中文网

原创

本篇文章给大家分享的内容是关于redis集群搭建教程的总结,内容很详细,有需要的朋友可以参考一下,希望可以帮助到大家。

前言

本文收集并整理了Redis集群搭建的网文、网站、自己的经验。水平有限,只分享环境搭建。本文分为以下几个部分:

  • Redis 安装

  • Rvm 安装更换源

  • Rvm Ruby 安装、使用、卸载

  • RubyGems 升级、更换源、安装redis

  • 集群配置

  • 成果测试

请大家按照以上步骤来查看此文

说明:

  • 仅限学习使用,若用于线上,本人不承担任何责任。

  • 如有问题,请在下方留言。

  • 文中有些命令没有带sudo,是因为我用的root权限。

Redis 安装

编译安装

wget http://download.redis.io/releases/redis-4.0.10.tar.gz
tar xzf redis-4.0.10.tar.gz
cd redis-4.0.10
make PREFIX=/usr/local/redis install

注:如果不想将Redis作为一个服务,到这就已经安装完了

将Redis做成一个服务 参考:Redis Quick Start

  • Create a directory where to store your Redis config files and your data:(有道词典:创建一个目录来存储Redis配置文件和数据:)

    483310883-5b5af18e08abf_articlex.png

    # 这只是一个目录结构,大家不要着急为什么自己没有,往下看,一步一步来
    [root@amor ~]# cd /usr/local/redis
    [root@amor redis]# tree
    .
    ├── bin  # 编译安装指定目录后自动生成目录及文件
    │   ├── redis-benchmark
    │   ├── redis-check-aof
    │   ├── redis-check-rdb
    │   ├── redis-cli
    │   ├── redis-sentinel -> redis-server
    │   └── redis-server
    ├── conf # 自己建立的存储配置文件的目录及自己创建的单个Redis配置文件
    │   └── 6379.conf
    └── data # 自己建立的存储Redis数据的目录及单个Redis服务数据存储目录
        └── 6379
    
    4 directories, 7 files

    注意:cp /usr/src/redis-4.0.10/src/redis-trib.rb /usr/local/redis/bin/ 后面创建集群要用到

  • Copy the init script that you'll find in the Redis distribution under the utils directory into /etc/init.d. We suggest calling it with the name of the port where you are running this instance of Redis. For example:(有道词典:将在utils目录下的Redis发行版中找到的init脚本复制到/etc/init.d中我们建议使用正在运行这个Redis实例的端口的名称来调用它。例如:)

    sudo cp utils/redis_init_script /etc/init.d/redis_6379
  • Edit the init script.(有道词典:编辑init脚本。)

    #!/bin/sh
    # chkconfig 2345 90 25                         # linux 开机启动设置 2345 运行级别 90 启动优先级(参考 memcached head /etc/rc.d/rc3.d/S90memcached ) 25 关闭优先级 (参考memcached)
    # Simple Redis init.d script conceived to work on Linux systems
    # as it does use of the /proc filesystem.
    
    ### BEGIN INIT INFO
    # Provides:     redis_6379
    # Default-Start:        2 3 4 5
    # Default-Stop:         0 1 6
    # Short-Description:    Redis data structure server
    # Description:          Redis data structure server. See https://redis.io
    ### END INIT INFO
    
    REDISPORT=6379
    EXEC=/usr/local/redis/bin/redis-server         # 修改为自己的可执行文件所在目录
    CLIEXEC=/usr/local/redis/bin/redis-cli         # 修改为自己的可执行文件所在目录
    
    PIDFILE=/var/run/redis_${REDISPORT}.pid        # 默认就好
    CONF="/usr/local/redis/conf/${REDISPORT}.conf" # 修改为自己的配置文件存放目录
    ···省略···
    esac

    开始修改redis.conf

    Make sure to modify REDISPORT accordingly to the port you are using. Both the pid file path and the configuration file name depend on the port number.(有道词典:请确保根据您正在使用的端口对重新分配进行相应的修改。pid文件路径和配置文件名都取决于端口号。)

    • Set daemonize to yes (by default it is set to no). (需要修改为 yes)

    • Set the pidfile to /var/run/redis_6379.pid (modify the port if needed). (默认即可)

    • Change the port accordingly. In our example it is not needed as the default port is already 6379. (默认即可,设置集群的时候需要拷贝配置文件并且重新设置端口)

    • Set your preferred loglevel. (默认即可)

    • Set the logfile to /var/log/redis_6379.log (默认好像为空,需要修改)

    • Set the dir to /var/redis/6379 (very important step!)  (redis数据保存目录,需要修改位置自定义路径)

    • sudo cp redis.conf /usr/local/redis/conf/6379.conf (修改成自己定义的目录。参考上述目录结构 redis.conf 在你们redis解压目录中的src目录下)

    • sudo mkdir /usr/local/redis/data/6379 (修改成自己定义的目录。参考上述目录结构)

      讯飞听见会议
      讯飞听见会议

      科大讯飞推出的AI智能会议系统

      下载
    • Edit the configuration file, making sure to perform the following changes:(有道词典:编辑配置文件,确保执行以下更改:)

      注:上面的意思是让你们修改 /usr/local/redis/conf/6379.conf,用vim 打开,搜索上述关键词即可,参考以下设置(如果所有的步骤都是粘贴复制的走下来的,直接修改成下面这样:0.0):
      port 6379
      daemonize yes
      pidfile /var/run/redis_6379.pid
      loglevel notice
      logfile "/var/log/redis_6379.log"
      dir /usr/local/redis/data/6379
  • Finally add the new Redis init script to all the default runlevels using the following command:(有道词典:最后,使用以下命令将新的Redis init脚本添加到所有默认的运行级别:)

    # ubuntu
    sudo update-rc.d redis_6379 defaults
    # centos
    chkconfig --add redis_6379
  • You are done! Now you can try running your instance with:

    sudo /etc/init.d/redis_6379 start

测试

2178561501-5b5af339e7bfb_articlex.png

Redis停止、启动

yum 安装

  • /etc/init.d/redis-server stop

  • /etc/init.d/redis-server start

  • /etc/init.d/redis-server restart

源码安装 三种方式

sudo /etc/init.d/redis_6379 start
/usr/local/redis/bin/redis-server redis.conf  # 注意此处缺省:配置文件路径
redis-cli -h 127.0.0.1 -p 6379 shutdown

注:如果只是停止本地redis 请执行: redis-cli shutdown

强制终止

  • kill -9 进程号

  • pkill redis

Rvm 安装 更换源

curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
curl -L get.rvm.io | bash -s stable 
rvm user gemsets # 建立用户配置目录,更换源的时候需要向 db 文件写入配置信息
echo "ruby_url=https://cache.ruby-china.org/pub/ruby" > ~/.rvm/user/db # 更换源

Rvm Ruby 安装、使用、卸载

rvm list known
rvm install 2.6
rvm use 2.6
yum -y remove ruby # 卸载centos yum 安装的 1.8 版本
ruby --version
rvm uninstall ruby # 此处带不带版本自己测试

RubyGems 升级、更换源、安装redis

gem install rubygems-update 
rubygems-update
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
gem sources -l 
gem install redis

集群配置

注:下面的内容是我自己参考这篇博文加上我熟悉Redis安装后自己的配置过程。大家可以参考NrwLm - Redis 集群搭建详细指南。

开启 Redis cluster

cd /usr/local/redis/conf
cp 6379.conf redis.conf.default  # 用作集群其他配置文件的蓝本
sudo vim redis.conf.default

修改内容如下

bind 192.168.2.123  # 绑定当前机器 IP
cluster-enabled yes # 取消注释,启动集群模式
cluster-config-file nodes-6379.conf # 取消注释,修改为 /usr/local/redis/data/6379/nodes-6379.conf  (如果遇到需要重新建立集群,不将此项修改为指定路径而和启动配置文件放在一起,会导致建立集群时,删除重建conf 文件)
cluster-node-timeout 15000 # 取消注释
appendonly yes # 将 no 修改为 yes

创建配置文件

cd /usr/local/redis/conf
echo 9001.conf 9002.conf 9003.conf 9004.conf 9005.conf 9006.conf | xargs -n 1 cp -v redis.conf.default
sed -i 's/6379/9001/g'  9001.conf 
sed -i 's/6379/9002/g'  9002.conf 
sed -i 's/6379/9003/g'  9003.conf 
sed -i 's/6379/9004/g'  9004.conf 
sed -i 's/6379/9005/g'  9005.conf 
sed -i 's/6379/9006/g'  9006.conf

3661239154-5b5af4e4da35e_articlex.png

1970487434-5b5af51e0a252_articlex.png

创建数据存储文件

cd /usr/local/redis/data
mkdir -p 9001 9002 9003 9004 9005 9006
# 后期可能需要删除该文件件下的文件,用于重建集群,所以,删除命令也写一下
rm -rf 900*/*

启动Redis cluster 节点

/usr/local/redis/bin/redis-server /usr/local/redis/conf/9001.conf
/usr/local/redis/bin/redis-server /usr/local/redis/conf/9002.conf
/usr/local/redis/bin/redis-server /usr/local/redis/conf/9003.conf 
/usr/local/redis/bin/redis-server /usr/local/redis/conf/9004.conf
/usr/local/redis/bin/redis-server /usr/local/redis/conf/9005.conf
/usr/local/redis/bin/redis-server /usr/local/redis/conf/9006.conf

3830923871-5b5af5cb25f8f_articlex.png

创建集群

/usr/local/redis/bin/redis-trib.rb create --replicas 1 192.168.2.123:9001 192.168.2.123:9002 192.168.2.123:9003 192.168.2.123:9004 192.168.2.123:9005 192.168.2.123:9006

566669470-5b5af5ee3f285_articlex.png

测试

执行命令: /usr/local/redis/bin/redis-cli -c -h 192.168.2.123 -p 9001

1611963180-5b5af612befea_articlex.png

问题汇总

  • 如果遇到timeout 请查看自己的防火墙,安装宝塔的尤其注意,请先去安全里面放行 9001:9006 的端口

  • redis集群 Waiting for the cluster to join 一直等待,redis集群不仅需要开通redis客户端连接的端口,而且需要开通集群总线端口,集群总线端口为redis客户端连接的端口 + 1000896909881-5b5af63220209_articlex.png

  • redis /usr/bin/env: ruby: 没有那个文件或目录

    • 执行这个命令 rvm get stable --auto-dotfiles,或者执行 nvm list 有详细的错误说明(查了资料说,线上不要用rvm安装ruby)

    456119379-5b5af64cccffb_articlex.png

    • 这是我自己的解决方案

      # 把这个添加到 /etc/profile 文件中(放到最后就行)
      rvm use ruby-2.6.0-preview2

相关推荐:

CentOS7系统安装和配置Memcached的方法

相关专题

更多
php文件怎么打开
php文件怎么打开

打开php文件步骤:1、选择文本编辑器;2、在选择的文本编辑器中,创建一个新的文件,并将其保存为.php文件;3、在创建的PHP文件中,编写PHP代码;4、要在本地计算机上运行PHP文件,需要设置一个服务器环境;5、安装服务器环境后,需要将PHP文件放入服务器目录中;6、一旦将PHP文件放入服务器目录中,就可以通过浏览器来运行它。

1930

2023.09.01

php怎么取出数组的前几个元素
php怎么取出数组的前几个元素

取出php数组的前几个元素的方法有使用array_slice()函数、使用array_splice()函数、使用循环遍历、使用array_slice()函数和array_values()函数等。本专题为大家提供php数组相关的文章、下载、课程内容,供大家免费下载体验。

1263

2023.10.11

php反序列化失败怎么办
php反序列化失败怎么办

php反序列化失败的解决办法检查序列化数据。检查类定义、检查错误日志、更新PHP版本和应用安全措施等。本专题为大家提供php反序列化相关的文章、下载、课程内容,供大家免费下载体验。

1170

2023.10.11

php怎么连接mssql数据库
php怎么连接mssql数据库

连接方法:1、通过mssql_系列函数;2、通过sqlsrv_系列函数;3、通过odbc方式连接;4、通过PDO方式;5、通过COM方式连接。想了解php怎么连接mssql数据库的详细内容,可以访问下面的文章。

948

2023.10.23

php连接mssql数据库的方法
php连接mssql数据库的方法

php连接mssql数据库的方法有使用PHP的MSSQL扩展、使用PDO等。想了解更多php连接mssql数据库相关内容,可以阅读本专题下面的文章。

1400

2023.10.23

html怎么上传
html怎么上传

html通过使用HTML表单、JavaScript和PHP上传。更多关于html的问题详细请看本专题下面的文章。php中文网欢迎大家前来学习。

1229

2023.11.03

PHP出现乱码怎么解决
PHP出现乱码怎么解决

PHP出现乱码可以通过修改PHP文件头部的字符编码设置、检查PHP文件的编码格式、检查数据库连接设置和检查HTML页面的字符编码设置来解决。更多关于php乱码的问题详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1439

2023.11.09

php文件怎么在手机上打开
php文件怎么在手机上打开

php文件在手机上打开需要在手机上搭建一个能够运行php的服务器环境,并将php文件上传到服务器上。再在手机上的浏览器中输入服务器的IP地址或域名,加上php文件的路径,即可打开php文件并查看其内容。更多关于php相关问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1303

2023.11.13

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

Redis+MySQL数据库面试教程
Redis+MySQL数据库面试教程

共72课时 | 6.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号