- 安装:
yum ?-y ?install ?redis ?php-redis
- 配置:
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
二、主从复制:
- 主服务器配置(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
- 从服务器配置(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
- 测试:
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.gitcd ?twemproxy
- 解压:
tar ?-xzf ?nutcracker-0.4.1.tar.gz
- 安装:
cd ?nutcracker-0.4.1autoreconf ?-fvi./configure ?--prefix=/usr/local/twemproxymake ?-j ?8make ?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: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
- 检查配置文件语法:
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/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
- 启动服务:
systemctl ?start ?twemproxy
- 测试:
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