Redis5一主两从读写分离配置

实验环境:

系统:CentOS7

软件:redis-5.0.2

一、解压redis-5.0.2

三个节点

[root@red1 software]# cd /usr/local/
[root@red1 local]# ll
total 1912
drwxr-xr-x. 2 root root     134 Apr 10 21:45 bin
drwxr-xr-x. 2 root root       6 Apr 11  2018 etc
drwxr-xr-x. 2 root root       6 Apr 11  2018 games
drwxr-xr-x. 2 root root       6 Apr 11  2018 include
drwxr-xr-x. 2 root root       6 Apr 11  2018 lib
drwxr-xr-x. 2 root root       6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root       6 Apr 11  2018 libexec
drwxrwxr-x. 6 root root    4096 Apr 11 11:49 redis
-rw-r--r--. 1 root root 1952989 Apr 10 21:42 redis-5.0.2.tar.gz
drwxr-xr-x. 2 root root       6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root      49 Oct 22 10:25 share
drwxr-xr-x. 2 root root       6 Apr 11  2018 src

二、安装 Redis

三个节点

make

make install

三、修改环境变量

三个节点

安装完 Redis 之后,在/usr/local/bin会生成一些脚本

[root@red1 local]# cd /usr/local/bin/
[root@red1 bin]# ll
total 32628
-rwxr-xr-x. 1 root root 4365456 Apr 10 21:45 redis-benchmark
-rwxr-xr-x. 1 root root 8084864 Apr 10 21:45 redis-check-aof
-rwxr-xr-x. 1 root root 8084864 Apr 10 21:45 redis-check-rdb
-rwxr-xr-x. 1 root root 4786592 Apr 10 21:45 redis-cli
lrwxrwxrwx. 1 root root      12 Apr 10 21:45 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 8084864 Apr 10 21:45 redis-server

修改环境变量

PATH=\$PATH:/mysql/app/mysql/bin:/mysql/app/xtrabackup/bin:$HOME/bin:/usr/bin:/sbin:/bin:/usr/local/bin
source .bash_profile

四、配置 Redis 配置文件

主节点192.168.8.11

port 6000            #端口号
requirepass 123456       #登录口令
bind 192.168.8.11        #绑定IP
daemonize yes          #后台运行redis

从节点一

port 6001             #端口号       
bind 192.168.8.12               #绑定IP
slaveof 192.168.8.11 6000      #设置主节点信息
masterauth 123456               #主节点口令
requirepass 123456              #登录口令
slave-read-only yes             #只读模式
daemonize yes                    #后台运行redis

从节点二

port 6002                        #端口号       
bind 192.168.8.13                #绑定IP
slaveof 192.168.8.11 6000        #设置主节点信息
masterauth 123456                #主节点口令
requirepass 123456               #登录口令
slave-read-only yes              #只读模式
daemonize yes                    #后台运行redis

五、启动Redis

先启动主节点,在启动从节点

主节点

[root@red1 redis]# redis-server /usr/local/redis/redis.conf
[root@red1 redis]# 
[root@red1 redis]# 
[root@red1 redis]# ps -ef|grep redis
root      2012     1  0 21:35 ?        00:00:00 redis-server 192.168.8.11:6000
root      2017  1384  0 21:35 pts/0    00:00:00 grep --color=auto redis

可以看到端口号为6000的redis服务已经启动

从节点一

root@red2 ~]# redis-server /usr/local/redis/redis.conf
[root@red2 ~]# ps -ef|grep redis
root      1968     1  0 21:36 ?        00:00:00 redis-server 192.168.8.12:6001
root      1973  1386  0 21:36 pts/0    00:00:00 grep --color=auto redis

可以看到端口号为6001的 redis服务已经 启动

从节点二

[root@red3 ~]# ps -ef|grep redis
root      1943     1  0 21:36 ?        00:00:00 redis-server 192.168.8.13:6002
root      1956  1034  0 21:36 pts/0    00:00:00 grep --color=auto redis

可以看到端口号为6002的 redis服务已经启动

六、可用性验证

主节点

[root@red1 redis]# redis-cli -p 6000 -a 123456 -h 192.168.8.11
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.8.11:6000> get 1
"hello"
192.168.8.11:6000> get 3
(nil)
192.168.8.11:6000> set 3 world
OK
192.168.8.11:6000> get 3
"world"

从节点一

[root@red2 ~]# redis-cli -p 6001 -a 123456 -h 192.168.8.12
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.8.12:6001> get 3
"world"

从节点二

[root@red3 ~]# redis-cli -p 6002 -h 192.168.8.13 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.8.13:6002> get 3
"world"






来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30135314/viewspace-2641351/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30135314/viewspace-2641351/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Redis读写分离配置,可以通过搭建主从复制架构来实现。主从复制是指将一个Redis服务器作为主节点(Master),其他Redis服务器作为从节点(Slaves)。主节点负责处理写操作,而从节点复制主节点的数据并负责处理读操作。 以下是配置步骤: 1. 启动主节点: - 在Redis配置文件 redis.conf 中,设置 bind 为主节点的IP地址。 - 设置 port 为主节点的端口号,默认为 6379。 - 设置 daemonize 为 yes,使Redis以守护进程方式运行。 - 设置 dir 和 logfile 分别指定持久化数据和日志文件的存储路径。 2. 启动从节点: - 复制主节点上的 redis.conf 文件到从节点,并修改配置文件。 - 设置 bind 为从节点的IP地址。 - 设置 port 为从节点的端口号。 - 设置 daemonize 为 yes。 - 设置 dir 和 logfile。 3. 在主节点上设置密码(可选): - 在 redis.conf 文件中设置 requirepass,指定密码。 - 重启主节点。 4. 在从节点上配置主从复制: - 在从节点的 redis.conf 文件中,设置 slaveof 主节点IP地址 主节点端口号。 - 重启从节点。 5. 验证配置是否成功: - 连接到主节点,并执行写操作,如 SET key value。 - 连接到从节点,并执行读操作,如 GET key,验证数据是否同步。 通过以上配置,主节点将负责处理写操作,而从节点将复制主节点的数据并负责处理读操作,实现了Redis读写分离
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值