温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Redis5怎么配置一主两从读写分离

发布时间:2021-09-04 09:15:43 来源:亿速云 阅读:128 作者:chen 栏目:关系型数据库

本篇内容介绍了“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"

“Redis5怎么配置一主两从读写分离”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI