本文收集并整理了Redis集群搭建的網(wǎng)文、網(wǎng)站、自己的經(jīng)驗。水平有限,只分享環(huán)境搭建。本文分為以下幾個部分:
Redis 安裝
Rvm 安裝更換源
Rvm Ruby 安裝、使用、卸載
RubyGems 升級、更換源、安裝redis
集群配置
成果測試
請大家按照以上步驟來查看此文
說明:
僅限學(xué)習(xí)使用,若用于線上,本人不承擔(dān)任何責(zé)任。
如有問題,請在下方留言。
文中有些命令沒有帶sudo,是因為我用的root權(quán)限。
Redis 安裝編譯安裝wget http://download.redis.io/releases/redis-4.0.10.tar.gztar xzf redis-4.0.10.tar.gzcd redis-4.0.10make PREFIX=/usr/local/redis install
注:如果不想將Redis作為一個服務(wù),到這就已經(jīng)安裝完了
將Redis做成一個服務(wù) 參考:Redis Quick StartCreate a directory where to store your Redis config files and your data:(有道詞典:創(chuàng)建一個目錄來存儲Redis配置文件和數(shù)據(jù):)
# 這只是一個目錄結(jié)構(gòu),大家不要著急為什么自己沒有,往下看,一步一步來[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 # 自己建立的存儲配置文件的目錄及自己創(chuàng)建的單個Redis配置文件│ └── 6379.conf└── data # 自己建立的存儲Redis數(shù)據(jù)的目錄及單個Redis服務(wù)數(shù)據(jù)存儲目錄 └── 63794 directories, 7 files
注意:cp /usr/src/redis-4.0.10/src/redis-trib.rb /usr/local/redis/bin/ 后面創(chuàng)建集群要用到
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發(fā)行版中找到的init腳本復(fù)制到/etc/init.d中我們建議使用正在運行這個Redis實例的端口的名稱來調(diào)用它。例如:)
sudo cp utils/redis_init_script /etc/init.d/redis_6379
Edit the init script.(有道詞典:編輯init腳本。)
#!/bin/sh# chkconfig 2345 90 25 # linux 開機啟動設(shè)置 2345 運行級別 90 啟動優(yōu)先級(參考 memcached head /etc/rc.d/rc3.d/S90memcached ) 25 關(guān)閉優(yōu)先級 (參考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 http://redis.io### END INIT INFOREDISPORT=6379EXEC=/usr/local/redis/bin/redis-server # 修改為自己的可執(zhí)行文件所在目錄CLIEXEC=/usr/local/redis/bin/redis-cli # 修改為自己的可執(zhí)行文件所在目錄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.(有道詞典:請確保根據(jù)您正在使用的端口對重新分配進行相應(yīng)的修改。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. (默認即可,設(shè)置集群的時候需要拷貝配置文件并且重新設(shè)置端口)
Set your preferred loglevel. (默認即可)
Set the logfile to /var/log/redis_6379.log (默認好像為空,需要修改)
Set the dir to /var/redis/6379 (very important step!) (redis數(shù)據(jù)保存目錄,需要修改位置自定義路徑)
sudo cp redis.conf /usr/local/redis/conf/6379.conf (修改成自己定義的目錄。參考上述目錄結(jié)構(gòu) redis.conf 在你們redis解壓目錄中的src目錄下)
sudo mkdir /usr/local/redis/data/6379 (修改成自己定義的目錄。參考上述目錄結(jié)構(gòu))
Edit the configuration file, making sure to perform the following changes:(有道詞典:編輯配置文件,確保執(zhí)行以下更改:)
注:上面的意思是讓你們修改 /usr/local/redis/conf/6379.conf,用vim 打開,搜索上述關(guān)鍵詞即可,參考以下設(shè)置(如果所有的步驟都是粘貼復(fù)制的走下來的,直接修改成下面這樣:0.0):
port 6379daemonize yespidfile /var/run/redis_6379.pidloglevel noticelogfile /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腳本添加到所有默認的運行級別:)
# ubuntusudo update-rc.d redis_6379 defaults
# centoschkconfig --add redis_6379
You are done! Now you can try running your instance with:
sudo /etc/init.d/redis_6379 start測試
/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 請執(zhí)行: redis-cli shutdown
強制終止kill -9 進程號
pkill redis
Rvm 安裝 更換源curl -sSL http://rvm.io/mpapis.asc | gpg2 --import -curl -L get.rvm.io | bash -s stable rvm user gemsets # 建立用戶配置目錄,更換源的時候需要向 db 文件寫入配置信息echo ruby_url=http://cache.ruby-china.org/pub/ruby ~/.rvm/user/db # 更換源Rvm Ruby 安裝、使用、卸載
rvm list knownrvm install 2.6rvm use 2.6yum -y remove ruby # 卸載centos yum 安裝的 1.8 版本ruby --versionrvm uninstall ruby # 此處帶不帶版本自己測試RubyGems 升級、更換源、安裝redis
gem install rubygems-update rubygems-updategem sources --add http://gems.ruby-china.com/ --remove http://rubygems.org/gem sources -l gem install redis集群配置
注:下面的內(nèi)容是我自己參考這篇博文加上我熟悉Redis安裝后自己的配置過程。大家可以參考NrwLm - Redis 集群搭建詳細指南。
開啟 Redis clustercd /usr/local/redis/confcp 6379.conf redis.conf.default # 用作集群其他配置文件的藍本sudo vim redis.conf.default
修改內(nèi)容如下
bind 192.168.2.123 # 綁定當(dāng)前機器 IPcluster-enabled yes # 取消注釋,啟動集群模式cluster-config-file nodes-6379.conf # 取消注釋,修改為 /usr/local/redis/data/6379/nodes-6379.conf (如果遇到需要重新建立集群,不將此項修改為指定路徑而和啟動配置文件放在一起,會導(dǎo)致建立集群時,刪除重建conf 文件)cluster-node-timeout 15000 # 取消注釋appendonly yes # 將 no 修改為 yes創(chuàng)建配置文件
cd /usr/local/redis/confecho 9001.conf 9002.conf 9003.conf 9004.conf 9005.conf 9006.conf | xargs -n 1 cp -v redis.conf.defaultsed -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/datamkdir -p 9001 9002 9003 9004 9005 9006# 后期可能需要刪除該文件件下的文件,用于重建集群,所以,刪除命令也寫一下rm -rf 900*/*啟動Redis cluster 節(jié)點
/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
執(zhí)行命令: /usr/local/redis/bin/redis-cli -c -h 192.168.2.123 -p 9001
如果遇到timeout 請查看自己的防火墻,安裝寶塔的尤其注意,請先去安全里面放行 9001:9006 的端口
redis集群 Waiting for the cluster to join 一直等待,redis集群不僅需要開通redis客戶端連接的端口,而且需要開通集群總線端口,集群總線端口為redis客戶端連接的端口 + 1000
redis /usr/bin/env: ruby: 沒有那個文件或目錄
執(zhí)行這個命令 rvm get stable --auto-dotfiles,或者執(zhí)行 nvm list 有詳細的錯誤說明(查了資料說,線上不要用rvm安裝ruby)
這是我自己的解決方案
# 把這個添加到 /etc/profile 文件中(放到最后就行)rvm use ruby-2.6.0-preview2
相關(guān)推薦:
CentOS7系統(tǒng)安裝和配置Memcached的方法
PHP中鎖機制的應(yīng)用
以上就是Redis集群搭建教程的總結(jié)的詳細內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。
新聞熱點
疑難解答
圖片精選