1. 程式人生 > 實用技巧 >阿里雲伺服器搭建redis主從遇到的坑

阿里雲伺服器搭建redis主從遇到的坑

如題,在兩臺伺服器之間搭建主從redis:

  1. 登入阿里雲控制檯把redis的埠開放。
  2. 如果是寶塔面板在安全欄目裡放行redis的埠。
  3. 主伺服器的redis.conf
    bind 127.0.0.1 //註釋掉不用
    protected-mode no //關閉保護模式

  4. 從伺服器的redis.conf配置同上,redis的主從可以在配置檔案中修改,具體位置如下
    ################################# 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