centos 6.7 搭建redis 主从服务

环境部署:

 redis master(zkdb1):192.168.16.210

 redis slave  :192.168.16.211

安装部署:

#创建一个软件安装目录

[root@zkdb1 ~]# mkdir /home/softwares
[root@zkdb1 ~]# cd /home/softwares/

#下载软件包,同时解压安装
[root@zkdb1 softwares]# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
[root@zkdb1 softwares]# tar -xvf redis-3.0.7.tar.gz  -C /usr/local/
[root@zkdb1 redis7]# cd /usr/local/redis-3.0.7/
[root@zkdb1 redis7]# make PREFIX=/usr/local/redis7 install

[root@zkdb1 redis7]#mkdir /opt/redis307 #创建一个redis 的配置文件目录

[root@zkdb1 redis7]# cd /opt/redis307
[root@zkdb1 redis7]# cp /usr/local/redis-3.0.7/redis.conf /opt/redis307/
[root@zkdb1 redis7]# ./bin/redis-server /opt/redis307/redis.conf ##启动redis 服务
 2、内核以及其他调整
添加如下两行

#/etc/sysctl.conf

net.core.somaxconn =8192
vm.overcommit_memory=1

#将下面三行写入到redis配置文件中
[root@zkdb1 redis7]#echo 'rename-command FLUSHALL ""' >> redis.conf
[root@zkdb1 redis7]#echo 'rename-command FLUSHDB ""' >> redis.conf
[root@zkdb1 redis7]#echo 'rename-command KEYS ""' >> redis.conf

配置文件
vim /opt/redis307/redis.conf
port  6000 ##更改端口
save 900 1
save 300 10000  ## 更改配置
save 60  200000
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 服务

[root@zkdb1 redis7]# /usr/local/redis7/bin/redis-server /opt/redis307/redis.conf  > /dev/null 2>&1 &

[root@zkdb1 redis307]# 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/null  2>&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

[root@zkdb1 redis307]# chmod +x /etc/init.d/redis
[root@zkdb1 redis307]# /etc/init.d/redis restart
save ok
aof rewrite ok
redis shutdown ok

[root@zkdb1 redis307]# vim /etc/rc.d/rc.local
/etc/init.d/redis start

###########################################

redis 从配置如下:

######################################################
ip  192.168.16.211  zkdb2
[root@zkdb2 ~]# mkdir /home/softwares
[root@zkdb2 ~]# cd /home/softwares/
[root@zkdb2 softwares]# wget http://download.redis.io/releases/redis-3.0.7.tar.gz
[root@zkdb2 softwares]# tar -xvf redis-3.0.7.tar.gz  -C /usr/local/
[root@zkdb2 redis7]# cd /usr/local/redis-3.0.7/
[root@zkdb2 redis7]# make PREFIX=/usr/local/redis7 install

 [root@zkdb2 redis7]# mkdir  /opt/redis307
[root@zkdb2 redis7]# cd /opt/redis307
[root@zkdb2 redis7]# cp /usr/local/redis-3.0.7/redis.conf /opt/redis307/
[root@zkdb2 redis7]# ./bin/redis-server /opt/redis307/redis.conf
5 内核以及其他调整
添加

[root@localhost redis-3.0.7]# 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
#更改端口
port  6000
更改数字
save 900 1
save 300 10000
save 60  200000
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
slaveof  192.168.16.210 6000    ## 从加入这一行主的ip

/usr/local/redis7/bin/redis-server /opt/redis307/redis.conf  > /dev/null 2>&1 &

[root@zkdb1 redis307]# 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/null  2>&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

[root@zkdb1 redis307]# chmod +x /etc/init.d/redis
[root@zkdb1 redis307]# /etc/init.d/redis restart
save ok
aof rewrite ok
redis shutdown ok

[root@zkdb1 redis307]# 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 key  valus (创建一个key后面跟一个值)
get key
从 
redis-cli -p 6000
get 该文件

 

 

 


你可能感兴趣的:(redis,centos,搭建)