centos6.10环境下启动多个redis实例
# 启动redis端口6379的配置 [root@newcms:/usr/local/nginx/conf]# egrep -v '^#|^$' /usr/local/redis-2.8.7/etc/redis.conf daemonize yes pidfile /usr/local/redis-2.8.7/redis.pid port 6379 tcp-backlog 511 timeout 300 tcp-keepalive 0 loglevel notice logfile "/usr/local/redis-2.8.7/var/redis.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename redis_data.rdb dir /data/redis_data slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 maxclients 10000 maxmemory <5368709120> appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 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 # 启动脚本 [root@newcms:/usr/local/nginx/conf]# cat /etc/init.d/redis-server #!/bin/sh # # redis - this script starts and stops the redis-server daemon # # chkconfig: - 85 15 # description: Redis is a persistent key-value database # processname: redis-server # config: /usr/local/redis/etc/redis.conf # config: /etc/sysconfig/redis # pidfile: /var/run/redis.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 redis="/usr/local/redis/bin/redis-server" prog=$(basename $redis) REDIS_CONF_FILE="/usr/local/redis/etc/redis.conf" [ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis lockfile=/var/lock/subsys/redis start() { [ -x $redis ] || exit 5 [ -f $REDIS_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $redis $REDIS_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { echo -n $"Reloading $prog: " killproc $redis -HUP RETVAL=$? echo } force_reload() { restart } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac # 启动redis端口 9376 的配置 # mkdir -p /data/redis_data_9376 [root@newcms:/usr/local/nginx/conf]# egrep -v '^#|^$' /usr/local/redis-2.8.24_9376/etc/redis.conf daemonize yes pidfile /usr/local/redis-2.8.24_9376/redis.pid port 9376 tcp-backlog 511 timeout 300 tcp-keepalive 0 loglevel notice logfile "/usr/local/redis-2.8.24_9376/var/redis.log" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename redis_data.rdb dir /data/redis_data_9376 masterauth pass requirepass pass rename-command FLUSHALL "" rename-command FLUSHDB "" rename-command shutdown "" slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 maxclients 10000 maxmemory <536870912> appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 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 # 添加开机自动启动 chkconfig --add redis9376-server # 启动脚本 [root@newcms:/usr/local/nginx/conf]# cat /etc/init.d/redis9376-server #!/bin/sh #chkconfig: 345 86 14 #description: Startup and shutdown script for Redis PROGDIR=/usr/local/redis-2.8.24_9376/bin #安装路径 PROGNAME=redis-server DAEMON=$PROGDIR/$PROGNAME CONFIG=/usr/local/redis-2.8.24_9376/etc/redis.conf PIDFILE=/usr/local/redis-2.8.24_9376/redis.pid DESC="redis daemon" SCRIPTNAME=/etc/rc.d/init.d/redis_9376 start() { if test -x $DAEMON then echo -e "Starting $DESC: $PROGNAME" if $DAEMON $CONFIG then echo -e "OK" else echo -e "failed" fi else echo -e "Couldn't find Redis Server ($DAEMON)" fi } stop() { if test -e $PIDFILE then echo -e "Stopping $DESC: $PROGNAME" if kill `cat $PIDFILE` then echo -e "OK" else echo -e "failed" fi else echo -e "No Redis Server ($DAEMON) running" fi } restart() { echo -e "Restarting $DESC: $PROGNAME" stop start } list() { ps aux | grep $PROGNAME } case $1 in start) start ;; stop) stop ;; restart) restart ;; list) list ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|list}" >&2 exit 1 ;; esac exit 0 # 新增监控 <?php /* redis_monitor.php 通过类似 http://127.0.0.1/php/redis_monitor.php?func_name='store_redis' php web的访问来判断redis的可用性 */ //通过获取/data/www/machine_no/.machine_no获取机房进行redis的判断 function conn_redis($redis_domain, $redis_port, $redis_pass){ //创建 Redis 对象 $redis = new Redis(); // global $redis; $redis->connect($redis_domain, $redis_port); $redis->auth($redis_pass); //设置 redis 字符串数据,并返回该设置的字符串 $redis->set("tutorial-name", "Redis-tutorial"); $result = $redis->get("tutorial-name"); return $result; } function cms_redis_6379() { global $machine_no; $res = conn_redis('192.168.11.53', 6379, ''); if ($res == "Redis-tutorial") { echo "success_6379"; }else { echo "fail_6379"; } } function cms_redis_9376() { global $machine_no; $res = conn_redis('192.168.11.53', 9376, 'pass'); if ($res == "Redis-tutorial") { echo "success_9376"; }else { echo "fail_9376"; } } // 通过get参数获取不同的应用redis $func_name = $_GET['func_name']; if ($func_name == "cms_redis_6379") { cms_redis_6379(); }elseif($func_name == "cms_redis_9376"){ cms_redis_9376(); } ?> # 修改 nginx.conf 配置 location ~ ^/php { root /data/www/html; location ~ php(.+\.php)(.*)$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; expires -1; if ($request_uri ~ "^(.*)(\?.*)$") { set $path_info $1; } fastcgi_param PATH_INFO $path_info; set $path_info $request_uri; } index watchphp.php; } # 新增监控项 # vim /usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd/redis_monitor.conf UserParameter=redis.cms_redis_6379,/usr/bin/curl -s http://127.0.0.1/php/redis_monitor.php?func_name='cms_redis_6379' UserParameter=redis.cms_redis_9376,/usr/bin/curl -s http://127.0.0.1/php/redis_monitor.php?func_name='cms_redis_9376' # 测试 /usr/bin/curl -s http://127.0.0.1/php/redis_monitor.php?func_name='cms_redis_6379'
# 启动redis端口6379的配置
[root@newcms:/usr/local/nginx/conf]# egrep -v '^#|^$' /usr/local/redis-2.8.7/etc/redis.conf
daemonize yes
pidfile /usr/local/redis-2.8.7/redis.pid
port 6379
tcp-backlog 511
timeout 300
tcp-keepalive 0
loglevel notice
logfile "/usr/local/redis-2.8.7/var/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename redis_data.rdb
dir /data/redis_data
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 10000
maxmemory <5368709120>
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
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
# 启动脚本
[root@newcms:/usr/local/nginx/conf]# cat /etc/init.d/redis-server
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /usr/local/redis/etc/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
redis="/usr/local/redis/bin/redis-server"
prog=$(basename $redis)
REDIS_CONF_FILE="/usr/local/redis/etc/redis.conf"
[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
lockfile=/var/lock/subsys/redis
start() {
[ -x $redis ] || exit 5
[ -f $REDIS_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $redis $REDIS_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $prog: "
killproc $redis -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
# 启动redis端口 9376 的配置
# mkdir -p /data/redis_data_9376
[root@newcms:/usr/local/nginx/conf]# egrep -v '^#|^$' /usr/local/redis-2.8.24_9376/etc/redis.conf
daemonize yes
pidfile /usr/local/redis-2.8.24_9376/redis.pid
port 9376
tcp-backlog 511
timeout 300
tcp-keepalive 0
loglevel notice
logfile "/usr/local/redis-2.8.24_9376/var/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename redis_data.rdb
dir /data/redis_data_9376
masterauth pass
requirepass pass
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command shutdown ""
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 10000
maxmemory <536870912>
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
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
# 添加开机自动启动
chkconfig --add redis9376-server
# 启动脚本
[root@newcms:/usr/local/nginx/conf]# cat /etc/init.d/redis9376-server
#!/bin/sh
#chkconfig: 345 86 14
#description: Startup and shutdown script for Redis
PROGDIR=/usr/local/redis-2.8.24_9376/bin #安装路径
PROGNAME=redis-server
DAEMON=$PROGDIR/$PROGNAME
CONFIG=/usr/local/redis-2.8.24_9376/etc/redis.conf
PIDFILE=/usr/local/redis-2.8.24_9376/redis.pid
DESC="redis daemon"
SCRIPTNAME=/etc/rc.d/init.d/redis_9376
start()
{
if test -x $DAEMON
then
echo -e "Starting $DESC: $PROGNAME"
if $DAEMON $CONFIG
then
echo -e "OK"
else
echo -e "failed"
fi
else
echo -e "Couldn't find Redis Server ($DAEMON)"
fi
}
stop()
{
if test -e $PIDFILE
then
echo -e "Stopping $DESC: $PROGNAME"
if kill `cat $PIDFILE`
then
echo -e "OK"
else
echo -e "failed"
fi
else
echo -e "No Redis Server ($DAEMON) running"
fi
}
restart()
{
echo -e "Restarting $DESC: $PROGNAME"
stop
start
}
list()
{
ps aux | grep $PROGNAME
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
list)
list
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|list}" >&2
exit 1
;;
esac
exit 0
# 新增监控
<?php
/*
redis_monitor.php
通过类似 http://127.0.0.1/php/redis_monitor.php?func_name='store_redis' php web的访问来判断redis的可用性
*/
//通过获取/data/www/machine_no/.machine_no获取机房进行redis的判断
function conn_redis($redis_domain, $redis_port, $redis_pass){
//创建 Redis 对象
$redis = new Redis();
// global $redis;
$redis->connect($redis_domain, $redis_port);
$redis->auth($redis_pass);
//设置 redis 字符串数据,并返回该设置的字符串
$redis->set("tutorial-name", "Redis-tutorial");
$result = $redis->get("tutorial-name");
return $result;
}
function cms_redis_6379() {
global $machine_no;
$res = conn_redis('192.168.11.53', 6379, '');
if ($res == "Redis-tutorial") {
echo "success_6379";
}else {
echo "fail_6379";
}
}
function cms_redis_9376() {
global $machine_no;
$res = conn_redis('192.168.11.53', 9376, 'pass');
if ($res == "Redis-tutorial") {
echo "success_9376";
}else {
echo "fail_9376";
}
}
// 通过get参数获取不同的应用redis
$func_name = $_GET['func_name'];
if ($func_name == "cms_redis_6379") {
cms_redis_6379();
}elseif($func_name == "cms_redis_9376"){
cms_redis_9376();
}
?>
# 修改 nginx.conf 配置
location ~ ^/php {
root /data/www/html;
location ~ php(.+\.php)(.*)$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
expires -1;
if ($request_uri ~ "^(.*)(\?.*)$") {
set $path_info $1;
}
fastcgi_param PATH_INFO $path_info;
set $path_info $request_uri;
}
index watchphp.php;
}
# 新增监控项
# vim /usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd/redis_monitor.conf
UserParameter=redis.cms_redis_6379,/usr/bin/curl -s http://127.0.0.1/php/redis_monitor.php?func_name='cms_redis_6379'
UserParameter=redis.cms_redis_9376,/usr/bin/curl -s http://127.0.0.1/php/redis_monitor.php?func_name='cms_redis_9376'
# 测试
/usr/bin/curl -s http://127.0.0.1/php/redis_monitor.php?func_name='cms_redis_6379'