1. 程式人生 > >centos7配置redis、php-redis

centos7配置redis、php-redis

centos7配置redis、php-r

一、安裝+配置:
  1. 安裝:
    yum  -y  install  redis  php-redis
  2. 配置:
    vim /etc/redis.conf
    # 允許所有主機訪問
    bind 0.0.0.0
    protected-mode yes
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile /var/log/redis/redis.log
    databases 16
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir /var/lib/redis
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    # 設置訪問密碼
    requirepass 123456
    # 最大訪問數
    maxclients 100000
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    aof-rewrite-incremental-fsync yes

    二、主從復制:

  3. 主服務器配置(192.168.8.81):
    vim /etc/redis.conf
    # 允許所有主機訪問
    bind 0.0.0.0
    protected-mode yes
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile /var/log/redis/redis.log
    databases 16
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir /var/lib/redis
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    # 設置訪問密碼
    requirepass 123456
    # 最大訪問數
    maxclients 100000
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    aof-rewrite-incremental-fsync yes
  4. 從服務器配置(192.168.8.82):
    # 允許所有主機訪問
    bind 0.0.0.0
    protected-mode yes
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile /var/log/redis/redis.log
    databases 16
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir /var/lib/redis
    # 設置為從服務器
    slaveof 192.168.8.81 6379
    # 主服務器密碼
    masterauth 123456
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    # 設置訪問密碼
    requirepass 123456
    # 最大訪問數
    maxclients 100000
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    aof-rewrite-incremental-fsync yes
  5. 測試:
    a. 主(192.168.8.81):
    redis-cli -a 123456
    set  name  dollarphp

    技術分享圖片
    b. 從(192.168.8.82):
    redis-cli -a 123456

    get  name

    技術分享圖片

    三、負載均衡(twemproxy):

  6. 使用場景:
    適用於多個從redis從服務器之間的讀操作
  7. 系統架構:
    角色                        ip地址
    主redis+負載均衡   192.168.8.81
    從redis                    192.168.8.82
    從redis                    192.168.8.83
  8. 下載:
    yum  -y  groupinstall  "development  tools"
    git  clone  https://github.com/dollarphper/twemproxy.git
    cd  twemproxy
  9. 解壓:
    tar  -xzf  nutcracker-0.4.1.tar.gz
  10. 安裝:
    cd  nutcracker-0.4.1
    autoreconf  -fvi
    ./configure  --prefix=/usr/local/twemproxy
    make  -j  8
    make  install
  11. 設置環境變量:
    echo  "PATH=$PATH:/usr/local/twemproxy/sbin/"  >>  /etc/profile
    .  /etc/profile
  12. 創建目錄:
    mkdir  /usr/local/twemproxy/{run,conf}
  13. 添加配置文件:
    vim /usr/local/twemproxy/sbin/conf/nutcracker.yml
    alpha:
    listen: 127.0.0.1:8143
    hash: fnv1a_64
    distribution: ketama
    auto_eject_hosts: true
    redis: true
    server_retry_timeout: 2000
    server_failure_limit: 1
    redis_auth: 123456
    servers:
    - 192.168.8.82:6379:1
    - 192.168.8.83:6379:1
  14. 檢查配置文件語法:
    cd  /usr/local/twemproxy/sbin/
    ./nutcracker  -t

    技術分享圖片

  15. 啟動服務:
    nutcracker  -d  -c  /usr/local/twemproxy/sbin/conf/nutcracker.yml  -p  /usr/local/twemproxy/run/redisproxy.pid  -o  /usr/local/twemproxy/run/redisproxy.log
  16. 停止服務:
    kill  -9  `cat  /usr/local/twemproxy/run/redisproxy.pid`
  17. 創建啟動服務:
    vim /etc/init.d/twemproxy
    #!/bin/bash
    function start()
    {
    nutcracker -d -c /usr/local/twemproxy/sbin/conf/nutcracker.yml -p /usr/local/twemproxy/run/redisproxy.pid -o /usr/local/twemproxy/run/redisproxy.log
    }
    function stop()
    {
    kill -9 `cat /usr/local/twemproxy/run/redisproxy.pid`
    }
    case "$1" in
    start)
        start
    ;;  
    stop)
        stop
    ;;  
    restart)
        stop
        start
    ;;
    *)
        echo "Usage : start | stop | restart"
    ;;
    esac
  18. 啟動服務:
    systemctl  start  twemproxy
  19. 測試:
    a. 主服務器(192.168.8.81):
    a-1. 窗口1:
    redis-cli  -a  123456
    set  name  dollarphp

    技術分享圖片
    a-2. 窗口2:

    redis-cli  -a  123456  -p  8143
    get  name

    技術分享圖片
    b. 從服務器(192.168.8.82):

    systemctl  stop  redis

    c. 從服務器(192.168.8.83):

    systemctl  stop  redis

    d. 主服務器(窗口2):
    技術分享圖片

    四、php連接redis:

五、php+redis實現隊列:

六、php+redis實現訂閱/發布:

centos7配置redis、php-redis