centos7配置redisphp-redis
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7配置redisphp-redis相关的知识,希望对你有一定的参考价值。
一、安装+配置:
- 安装:
yum -y install redis php-redis
- 配置:
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
二、主从复制:
- 主服务器配置(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
- 从服务器配置(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
- 测试:
a. 主(192.168.8.81):
redis-cli -a 123456set name dollarphp
b. 从(192.168.8.82):
redis-cli -a 123456get name
三、负载均衡(twemproxy):
- 使用场景:
适用于多个从redis从服务器之间的读操作
- 系统架构:
角色 ip地址 主redis+负载均衡 192.168.8.81 从redis 192.168.8.82 从redis 192.168.8.83
- 下载:
yum -y groupinstall "development tools" git clone https://github.com/dollarphper/twemproxy.git cd twemproxy
- 解压:
tar -xzf nutcracker-0.4.1.tar.gz
- 安装:
cd nutcracker-0.4.1 autoreconf -fvi ./configure --prefix=/usr/local/twemproxy make -j 8 make install
- 设置环境变量:
echo "PATH=$PATH:/usr/local/twemproxy/sbin/" >> /etc/profile . /etc/profile
- 创建目录:
mkdir /usr/local/twemproxy/{run,conf}
- 添加配置文件:
vim /usr/local/twemproxy/sbin/conf/nutcracker.ymlalpha: 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
- 检查配置文件语法:
cd /usr/local/twemproxy/sbin/ ./nutcracker -t
- 启动服务:
nutcracker -d -c /usr/local/twemproxy/sbin/conf/nutcracker.yml -p /usr/local/twemproxy/run/redisproxy.pid -o /usr/local/twemproxy/run/redisproxy.log
- 停止服务:
kill -9 `cat /usr/local/twemproxy/run/redisproxy.pid`
- 创建启动服务:
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
- 启动服务:
systemctl start twemproxy
- 测试:
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的安装配置代码