First, installation + configuration:
- Installation:
yum -y install redis php-redis
- Configuration:
Vim/etc/redis.conf# Allow all hosts to access 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 1save 10save 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# set access password Requirepass 123456# maximum number of accesses 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
Second, master-slave replication:
- Primary server configuration (192.168.8.81):
Vim/etc/redis.conf# Allow all hosts to access 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 1save 10save 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# set access password Requirepass 123456# maximum number of accesses 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
- From server Configuration (192.168.8.82):
# Allow all hosts to access 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 1save 10save 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir/var/lib/redis# Set to slaveof 192.168.8.81 6379# master server password Masterauth 123456slave-serve-stale-data yesslave-read-only from server Yesrepl-diskless-sync norepl-diskless-sync-delay 5repl-disable-tcp-nodelay noslave-priority 100# set access password Requirepass 123456# Maximum number of accesses 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
- Test:
A. Main (192.168.8.81):
Redis-cli-a 123456set name dollarphp
B. From (192.168.8.82):
Redis-cli-a 123456
get name
Three, load Balancing (twemproxy):
- Usage scenarios:
适用于多个从redis从服务器之间的读操作
- System Architecture:
角色 ip地址主redis+负载均衡 192.168.8.81从redis 192.168.8.82从redis 192.168.8.83
- Download:
yum -y groupinstall "development tools"git clone https://github.com/dollarphper/twemproxy.gitcd twemproxy
- Extract:
tar -xzf nutcracker-0.4.1.tar.gz
- Installation:
cd nutcracker-0.4.1autoreconf -fvi./configure --prefix=/usr/local/twemproxymake -j 8make install
- Set Environment variables:
echo "PATH=$PATH:/usr/local/twemproxy/sbin/" >> /etc/profile. /etc/profile
- To create a directory:
mkdir /usr/local/twemproxy/{run,conf}
- To add a configuration file:
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
- Check the configuration file syntax:
cd /usr/local/twemproxy/sbin/./nutcracker -t
- Start the service:
nutcracker -d -c /usr/local/twemproxy/sbin/conf/nutcracker.yml -p /usr/local/twemproxy/run/redisproxy.pid -o /usr/local/twemproxy/run/redisproxy.log
- Stop service:
kill -9 `cat /usr/local/twemproxy/run/redisproxy.pid`
- To create a startup service:
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
- Start the service:
systemctl start twemproxy
- Test:
A. Primary server (192.168.8.81):
A-1. Window 1:redis-cli -a 123456set name dollarphp
A-2. Window 2:
redis-cli -a 123456 -p 8143get name
B. From the server (192.168.8.82):
systemctl stop redis
C. From the server (192.168.8.83):
systemctl stop redis
D. master server (Windows 2):
Iv. PHP connection to Redis:
Five, Php+redis implementation queue: Six, Php+redis Implementation subscription/release:
Centos7 configuring Redis, Php-redis