Redis安装+主从部署

IP:192.168.225.128
环境:centos7

一、安装redis

步骤 1、首先确认系统中是否存在C语言的编译环境

# gcc -version
或者 # g++ --version
yum install -y  gcc 

步骤 2、下载redis安装包

wget http://download.redis.io/releases/redis-3.2.10.tar.gz

步骤 3、解压到/usr/local/redis

# tar -zxvf redis-3.2.10.tar.gz
# mv redis-3.2.10 /usr/local/redis
# cd redis

步骤 4、安装

# make
# make test
遇到报错
Executing test client: NOREPLICAS Not enough good slaves to write..
可以修改文件tests/integration/replication-2.tcl,将after 1000改为after 10000以延长等待时间。
# make test
# make install PREFIX=/usr/local/redis    PREFIX安装指定目录 否则安装到/usr/local/bin里面了

步骤 5、修改配置文件

bind 192.168.225.128
port 6379
timeout 0
daemonize yes
pidfile /var/run/redis6379.pid
loglevel debug
logfile /var/log/redis6379.log

步骤 6、启动

# cd /usr/local/redis6379/bin
# ./redis-server ../redis6379.conf &
# cd /usr/local/redis6380/bin
# ./redis-server ../redis6380.conf &

步骤 7、6380端口启动服务,方法如上,只不过存放的目录不同

安装位置:/usr/local/redis6380
配置文件:/usr/local/redis6380/redis6380.conf
启动后pid文件:/var/run/redis_6380.pid
日志文件:/var/log/redis6380.log
数据文件位置:/data/redis6380/

二、配置redis主从

(一个服务器上启动两个redis,端口为6379和6380,
192.168.225.128:6379主,192.168.225.128:6380从

步骤 1、主redis服务器6379

不需要改动

步骤 2、从redis服务器6380

vi /etc/redis/6380.conf
slave-serve-stale-data yes
# slaveof <masterip> <masterport>下面添加一句
slaveof 192.168.225.128 6379

步骤 3、启动redis

先关闭redis
# redis-cli -h 192.168.225.128 -p 6380 shutdown
# cd /usr/local/redis6379/
# bin/redis-server redis6379.conf &
# cd /usr/local/redis6380/
# bin/redis-server redis6380.conf &

步骤 4、查看redis进程

# ps -ef |grep redis

步骤 5、验证主从是否同步(128:6379和128:6380)

[root@bogon redis6379]# redis-cli -h 192.168.225.128 -p 6379
192.168.225.128:6379> set ma right
OK
192.168.225.128:6379> set Li class2
OK
192.168.225.128:6379> keys *
 1) "ma"
 2) "fangchan"
 3) "Li"
192.168.225.128:6379> 
[root@bogon redis6379]# redis-cli -h 192.168.225.128 -p 6380
192.168.225.128:6380> keys *
 1) "ma"
 2) "Li"
 3) "fangchan"
192.168.225.128:6380> 

步骤 6、主从同步正常

步骤 7、从库默认是只读的

192.168.225.128:6380> set lll beaty
(error) READONLY You can't write against a read only slave.

三、redis主从切换

192.168.225.128:6379主,192.168.225.128:6380从

步骤 1、停止主库redis

redis-cli -h 192.168.225.128 -p 6379 shutdown

步骤 2、将从redis设为主redis

[root@bogon redis6379]# redis-cli -h 192.168.225.128 -p 6380 slaveof NO ONE
OK

步骤 3、测试从redis是否切换为主redis

[root@bogon redis6379]# redis-cli -h 192.168.225.128 -p 6380
192.168.225.128:6380> set apu new
OK
192.168.225.128:6380> get apu
"new"
192.168.225.128:6380> 

从库可以写入,切换成功

步骤 4、原来的主redis6379重新恢复正常,需要重新切换回去

1.将现在的主redis6380数据进行保存

[root@bogon redis6379]# redis-cli -h 192.168.225.128 -p 6380
192.168.225.128:6380> set apu new
OK
192.168.225.128:6380> get apu
"new"
192.168.225.128:6380> save

2.将现在的主redis数据目录下dump.rdb文件拷贝覆盖原来主redis6379数据目录下面

3.启动原来的redis6379

# cd /usr/local/redis6379
# bin/redis-server redis6379.conf &

4.将现在的主redis6380切换为从库

[root@bogon redis6379]# redis-cli -h 192.168.225.128 -p 6380 slaveof 192.168.225.128 6379
OK

步骤 5、查看主库redis是否正常

之前在从库中添加的key,此时在主库6379中都可以查看

步骤 6、验证主从同步是否正常

主redis6379写入,从redis6380可正常同步;
从redis只支持读,不支持写入

via https://segmentfault.com/a/1190000010848263

Redis安装+主从部署
标签:

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*