這篇文章給大家分享的內容是關於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設定檔和資料:)
-
cp /usr /src/redis-4.0.10/src/redis-trib.rb /usr/local/redis/bin/#
# 这只是一个目录结构,大家不要着急为什么自己没有,往下看,一步一步来 [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
##注意:後面建立叢集要用到
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
#!/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檔案路徑和配置檔案名稱都取決於連接埠號碼。)
- 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資料保存目錄,需要修改位置自訂路徑)
- (修改成自己定義的目錄。參考上述目錄結構redis.conf 在你們redis解壓縮目錄中的src目錄下)
- (修改成自己定義的目錄。參考上述目錄結構)
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
# ubuntu sudo update-rc.d redis_6379 defaults
# centos chkconfig --add redis_6379
sudo /etc/init.d/redis_6379 start
Redis停止、啟動
- /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 行程編號
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
创建数据存储文件
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
创建集群
/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
测试
执行命令: /usr/local/redis/bin/redis-cli -c -h 192.168.2.123 -p 9001
<img src="/static/imghwm/default1.png" data-src="https://img.php.cn//upload/image/755/105/520/1532744572918545.png?x-oss-process=image/resize,p_40" class="lazy" title="1532744572918545.png" alt="Redis集群建構教學的總結">
问题汇总
如果遇到timeout 请查看自己的防火墙,安装宝塔的尤其注意,请先去安全里面放行 9001:9006 的端口
redis集群 Waiting for the cluster to join 一直等待,redis集群不仅需要开通redis客户端连接的端口,而且需要开通集群总线端口,集群总线端口为redis客户端连接的端口 + 1000
redis /usr/bin/env: ruby: 没有那个文件或目录
执行这个命令
rvm get stable --auto-dotfiles
,或者执行nvm list
有详细的错误说明(查了资料说,线上不要用rvm安装ruby)-
这是我自己的解决方案
# 把这个添加到 /etc/profile 文件中(放到最后就行) rvm use ruby-2.6.0-preview2
相关推荐:
以上是Redis集群建構教學的總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Atom編輯器mac版下載
最受歡迎的的開源編輯器