如題,在兩台服務器之間搭建主從redis:
- 登錄阿里雲控制台把redis的端口開放。
- 如果是寶塔面板在安全欄目里放行redis的端口。
- 主服務器的redis.conf
bind 127.0.0.1 //注釋掉不用 protected-mode no //關閉保護模式
- 從服務器的redis.conf配置同上,redis的主從可以在配置文件中修改,具體位置如下,還有一點需要注意,如果主redis設有密碼,從redis需要在下方masterauth處寫上主redis的密碼,但是我使用replicaof連主服務器沒有成功,不知道什么原因,但是在命令行中使用slaveof 去連又成功了
################################# REPLICATION ################################# # Master-Replica replication. Use replicaof to make a Redis instance a copy of # another Redis server. A few things to understand ASAP about Redis replication. # # +------------------+ +---------------+ # | Master | ---> | Replica | # | (receive writes) | | (exact copy) | # +------------------+ +---------------+ # # 1) Redis replication is asynchronous, but you can configure a master to # stop accepting writes if it appears to be not connected with at least # a given number of replicas. # 2) Redis replicas are able to perform a partial resynchronization with the # master if the replication link is lost for a relatively small amount of # time. You may want to configure the replication backlog size (see the next # sections of this file) with a sensible value depending on your needs. # 3) Replication is automatic and does not need user intervention. After a # network partition replicas automatically try to reconnect to masters # and resynchronize with them. # # replicaof <masterip> <masterport> #在這里寫入主服務器的ip 端口 例如 replicaof 192.168.1.103 6379 //如果redis有密碼 密碼是123456 masterauth 123456 //登錄賬戶 user masteruser user
網上的視頻教程里都是同一服務器開三個不同端口的redis,在同一服務器搭建主從沒有什么問題,但是當真正在兩台服務器之間搭建的時候就會出現問題,輸入info replication 會出現master_link_status:down。主服務器查看從服務器的連接數也是0。connected_slaves:0;
另外,我的服務器系統:CentOS 7.6.1810,裝的寶塔面板,然后首次安裝redis的時候會報錯,原因是gcc的版本過低,網上查找原因提示 升級gcc,命令如下
yum -y install centos-release-scl yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils scl enable devtoolset-9 bash echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile gcc -v