centos 6.7 搭建redis 主从服务
环境部署:redis master(zkdb1):192.168.16.210
redis slave:192.168.16.211
安装部署:
#创建一个软件安装目录
# mkdir /home/softwares
# cd /home/softwares/
#下载软件包,同时解压安装
# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
# tar -xvf redis-3.0.7.tar.gz-C /usr/local/
# cd /usr/local/redis-3.0.7/
# make PREFIX=/usr/local/redis7 install
#mkdir /opt/redis307 #创建一个redis 的配置文件目录
# cd /opt/redis307
# cp /usr/local/redis-3.0.7/redis.conf /opt/redis307/
# ./bin/redis-server /opt/redis307/redis.conf ##启动redis 服务
2、内核以及其他调整
添加如下两行
#/etc/sysctl.conf
net.core.somaxconn =8192
vm.overcommit_memory=1
#将下面三行写入到redis配置文件中
#echo 'rename-command FLUSHALL ""' >> redis.conf
#echo 'rename-command FLUSHDB ""' >> redis.conf
#echo 'rename-command KEYS ""' >> redis.conf
配置文件
vim /opt/redis307/redis.conf
port6000 ##更改端口
save 900 1
save 300 10000## 更改配置
save 60200000
dbfilename dump.rdb
slave-read-only yes##更改为yes
dir /opt/redis307/dbdata ## redis 数据存放目录,但该目录需要手工创建
appendonly yes
maxclients 30000
appendfsync everysec
appendfilename "appendonly.aof"
logfile "/tmp/redis307.log"
timeout 0
##启动redis 服务
# /usr/local/redis7/bin/redis-server /opt/redis307/redis.conf> /dev/null 2>&1 &
# vim /etc/init.d/redis##编写redis 配置脚本
#!/bin/bash
#chkconfig: 345 86 14
#description: Startup and shutdown script for Redis
CLI="/usr/local/redis7/bin/redis-cli -p 6000"
PRO="/usr/local/redis7/bin/redis-server"
CONFIG="/opt/redis307/redis.conf"
SCRIPTNAME="/etc/rc.d/init.d/redis"
status()
{
ping=`$CLI ping` > /dev/null
if [ -z $ping ]
then ping='0'
fi
}
start()
{
status
if [ $ping == 'PONG' ]
then
echo -e "redis already started"
else
$PRO $CONFIG> /dev/null2>&1 &
echo -e "start redis ok"
fi
}
stop()
{
status
if [ $ping == 'PONG' ]
then
$CLI save> /dev/null
echo "save ok"
$CLI BGREWRITEAOF> /dev/null
echo "aof rewrite ok"
$CLI shutdown > /dev/null
echo "redis shutdown ok"
else
echo -e "redis already stoped"
fi
}
restart()
{
stop
start
}
save_aof()
{
$CLI BGREWRITEAOF
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
save_aof)
save_aof
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|save_aof}" >&2
exit 1
;;
esac
exit 0
# chmod +x /etc/init.d/redis
# /etc/init.d/redis restart
save ok
aof rewrite ok
redis shutdown ok
# vim /etc/rc.d/rc.local
/etc/init.d/redis start
###########################################
redis 从配置如下:
######################################################
ip192.168.16.211zkdb2
# mkdir /home/softwares
# cd /home/softwares/
# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
# tar -xvf redis-3.0.7.tar.gz-C /usr/local/
# cd /usr/local/redis-3.0.7/
# make PREFIX=/usr/local/redis7 install
# mkdir/opt/redis307
# cd /opt/redis307
# cp /usr/local/redis-3.0.7/redis.conf /opt/redis307/
# ./bin/redis-server /opt/redis307/redis.conf
5 内核以及其他调整
添加
# vi /etc/sysctl.conf
net.core.somaxconn =8192
vm.overcommit_memory=1
echo 'rename-command FLUSHALL ""' >> redis.conf
echo 'rename-command FLUSHDB ""' >> redis.conf
echo 'rename-command KEYS ""' >> redis.conf
配置文件
vim /opt/redis307/redis.conf
#更改端口
port6000
更改数字
save 900 1
save 300 10000
save 60200000
dbfilename dump.rdb
slave-read-only yes
####添加
dir /opt/redis307/dbdata
## 改yes
appendonly yes
## 改数字
maxclients 30000
appendfsync everysec
appendfilename "appendonly.aof"
logfile "/tmp/redis307.log"
timeout 0
slaveof192.168.16.210 6000 ## 从加入这一行主的ip
/usr/local/redis7/bin/redis-server /opt/redis307/redis.conf> /dev/null 2>&1 &
# vim /etc/init.d/redis
#!/bin/bash
#chkconfig: 345 86 14
#description: Startup and shutdown script for Redis
CLI="/usr/local/redis7/bin/redis-cli -p 6000"
PRO="/usr/local/redis7/bin/redis-server"
CONFIG="/opt/redis307/redis.conf"
SCRIPTNAME="/etc/rc.d/init.d/redis"
status()
{
ping=`$CLI ping` > /dev/null
if [ -z $ping ]
then ping='0'
fi
}
start()
{
status
if [ $ping == 'PONG' ]
then
echo -e "redis already started"
else
$PRO $CONFIG> /dev/null2>&1 &
echo -e "start redis ok"
fi
}
stop()
{
status
if [ $ping == 'PONG' ]
then
$CLI save> /dev/null
echo "save ok"
$CLI BGREWRITEAOF> /dev/null
echo "aof rewrite ok"
$CLI shutdown > /dev/null
echo "redis shutdown ok"
else
echo -e "redis already stoped"
fi
}
restart()
{
stop
start
}
save_aof()
{
$CLI BGREWRITEAOF
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
save_aof)
save_aof
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|save_aof}" >&2
exit 1
;;
esac
exit 0
# chmod +x /etc/init.d/redis
# /etc/init.d/redis restart
save ok
aof rewrite ok
redis shutdown ok
# vim /etc/rc.d/rc.local
/etc/init.d/redis start
需要在两台redis配置文件中填写
vim /etc/profile
export PATH=/usr/local/redis7/bin:$PATH
source /etc/profile
验证:
主
redis-cli -p 6000
创建一个set keyvalus (创建一个key后面跟一个值)
get key
从
redis-cli -p 6000
get 该文件
页:
[1]