centos7配置redis、php-redis

一、安装+配置:

  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

    centos7配置redis、php-redis
    b. 从(192.168.8.82):
    redis-cli -a 123456

    get  name

    centos7配置redis、php-redis

    三、负载均衡(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

    centos7配置redis、php-redis

  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

    centos7配置redis、php-redis
    a-2. 窗口2:

    redis-cli  -a  123456  -p  8143
    get  name

    centos7配置redis、php-redis
    b. 从服务器(192.168.8.82):

    systemctl  stop  redis

    c. 从服务器(192.168.8.83):

    systemctl  stop  redis

    d. 主服务器(窗口2):
    centos7配置redis、php-redis

    四、php连接redis:

五、php+redis实现队列:

六、php+redis实现订阅/发布:

猜你喜欢

转载自blog.51cto.com/12173069/2122164