centos7配置redisphp-redis

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7配置redisphp-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

    技术分享图片
    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配置redisphp-redis的主要内容,如果未能解决你的问题,请参考以下文章

CentOS7服务器中apachephp7以及mysql5.7的安装配置代码

小白贴:Docker 实战之 CentOS7 系统环境配置

redis

CentOS7安装配置svn及svn hook实战

开发人员学Linux(10):CentOS7安装配置代码质量管理平台SonarQube6.4

CentOS7上安装配置GitLab