分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

centos7配置redis、php-redis

发布时间:2023-09-06 01:57责任编辑:白小东关键词:配置redis
一、安装+配置:
  1. 安装:
    yum ?-y ?install ?redis ?php-redis
  2. 配置:
    vim ?/etc/redis.conf
    # 允许所有主机访问bind 0.0.0.0protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /var/run/redis_6379.pidloglevel noticelogfile /var/log/redis/redis.logdatabases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /var/lib/redisslave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100# 设置访问密码requirepass 123456# 最大访问数maxclients 100000appendonly noappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes

    二、主从复制:

  3. 主服务器配置(192.168.8.81):
    vim ?/etc/redis.conf
    # 允许所有主机访问bind 0.0.0.0protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /var/run/redis_6379.pidloglevel noticelogfile /var/log/redis/redis.logdatabases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /var/lib/redisslave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100# 设置访问密码requirepass 123456# 最大访问数maxclients 100000appendonly noappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes
  4. 从服务器配置(192.168.8.82):
    # 允许所有主机访问bind 0.0.0.0protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize nosupervised nopidfile /var/run/redis_6379.pidloglevel noticelogfile /var/log/redis/redis.logdatabases 16save 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /var/lib/redis# 设置为从服务器slaveof 192.168.8.81 6379# 主服务器密码masterauth 123456slave-serve-stale-data yesslave-read-only yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100# 设置访问密码requirepass 123456# 最大访问数maxclients 100000appendonly noappendfilename "appendonly.aof"appendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yeslua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-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.gitcd ?twemproxy
  9. 解压:
    tar ?-xzf ?nutcracker-0.4.1.tar.gz
  10. 安装:
    cd ?nutcracker-0.4.1autoreconf ?-fvi./configure ?--prefix=/usr/local/twemproxymake ?-j ?8make ?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:8143hash: fnv1a_64distribution: ketamaauto_eject_hosts: trueredis: trueserver_retry_timeout: 2000server_failure_limit: 1redis_auth: 123456servers:- 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/bashfunction 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" instart) ???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 ?123456set ?name ?dollarphp


    a-2. 窗口2:

    redis-cli ?-a ?123456 ?-p ?8143get ?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

原文地址:http://blog.51cto.com/12173069/2122164

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved