Redis linux哨兵搭建-多台机器

一、安装redis并注册服务

参考:

Redis安装:https://www.cnblogs.com/heqiuyong/p/10463334.html

开启防火墙:

Linux系统通过firewall限制或开放IP及端口_从入门到放弃-CSDN博客_linux防火墙限制ip访问端口https://blog.csdn.net/ywd1992/article/details/80401630

redis哨兵搭建:

https://blog.csdn.net/lihongtai/article/details/82826809

https://blog.csdn.net/liang_operations/article/details/89434341

1)安装redis相关依赖

运行:yum install gcc gcc-c++ automake autoconf libtool make -y

2)切换软件安装路径

运行:cd /usr/local

略:查看此目录的内容:运行:ls

若已存在redis目录,则删除历史的,运行:rm -rf /redis

 

3)下载redis:运行

运行:wget https://download.redis.io/releases/redis-6.2.6.tar.gz

确定下载地址步骤

访问官网:Redishttps://redis.io/download

获取下载地址执行命令

下载、解压、重命名加压路径,可用xftp上传压缩包,进行下载解压重名名

4)解压redis 到redis目录

解压:tar -zxvf redis-6.2.6.tar.gz

重命名解压文件夹:mv redis-6.2.6 redis

5)编译redis并安装

切换解压后的目录:cd /usr/local/redis

编译:make

安装:make install PREFIX=/usr/local/redis

6)修改redis的配置文件为后台启动

右击下图的配置文件redis.conf,用记事本打开

记事本:查找 daemonize,改为 daemonize no为 daemonize yes,并保存

 

7)配置 redis自启服务文件

用xftp将redis.service上传 /etc/systemd/system目录下,或者用vi生成redis.service文件;

vi命令生成redis.service文件步骤:

1)运行编辑文件命令:

vi /etc/systemd/system/redis.service

2)按下键盘i进行输入,复制文件内容,右击黏贴;

3)退出编辑并保存:按下键盘Esc,并输入:“:wq”

redis.service文件内容为:

[Unit]

Description=redis-server

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf

PrivateTmp=true

[Install]

WantedBy=multi-user.target

8)设置redis开机自启

重载配置:systemctl daemon-reload

启动服务:systemctl start redis.service

添加开机自启:systemctl enable redis.service

查看服务状态1):systemctl status redis.service

查看服务状态2):ps -ef|grep redis

注:查看服务状态:

systemctl status 服务名

systemctl restart 服务名

9)开放端口限制

运行:firewall-cmd --zone=public --add-port=6379/tcp --permanent

生效配置:firewall-cmd --reload

更多参考:

Linux系统通过firewall限制或开放IP及端口_从入门到放弃-CSDN博客_linux防火墙限制ip访问端口https://blog.csdn.net/ywd1992/article/details/80401630

二、搭建redis的集群服务

1)再搭建2台自启服务

如步骤1再搭建2台redis服务

2)上传配置文件

用xftp,上传哨兵的配置文件到各自的服务器的/usr/local/redis文件夹下

(redis.conf和sentinel.conf):

1、修改相关的配置文件,上传至redis目录;主服务的配置文件:详见

redis-sentinel\master\redis.conf 主redis内容6处,并修改4处:

#绑定本机网卡:编辑(必)

bind 172.16.99.241

#绑定端口:编辑-默认

port 6379

#是否后台启动编辑(必)

daemonize yes

#RDB存储路径:编辑-默认

dir ./

#redis密码:编辑(必)

requirepass "666666"

#切换主从连接的认证密码:添加(必)

masterauth "666666"

2、从服务器配置文件,上传至redis目录(从redis内容7处,并修改5处):详见 redis-sentinel\slave\redis.conf

#除了主redis配置文件,添加复制来源,添加(必)

replicaof 172.16.99.241 6379

#切换主从连接的认证密码:添加(必)

masterauth "666666"

3、哨兵配置文件,上传至redis目录

详见 redis-sentinel\sentinel\sentinel.conf

3台均一致,必要修改5处,上传

#哨兵的端口(必)

port 26379

#后台启动(必)

daemonize yes

pidfile "/var/run/redis-sentinel-26379.pid"

#日志文件(必)

logfile "/usr/local/redis/sentinel-26379.log"

dir /tmp

#要监控的主服务器是谁(必)

sentinel monitor mymaster 172.16.99.241 6379 2

#主服务器的认证密码是谁(必)

sentinel auth-pass mymaster 666666

acllog-max-len 128

sentinel deny-scripts-reconfig yes

sentinel resolve-hostnames no

sentinel announce-hostnames no

3)开放哨兵端口限制

运行:firewall-cmd --zone=public --add-port=26379/tcp --permanent

生效配置:firewall-cmd --reload

更多参考:Linux系统通过firewall限制或开放IP及端口_从入门到放弃-CSDN博客_linux防火墙限制ip访问端口https://blog.csdn.net/ywd1992/article/details/80401630

4)重启启动redis

3台redis一次执行(主服务优先):

systemctl start redis.service

5)配置 redis-sentinel自启服务文件

用xftp将redis-sentinel.service上传 /etc/systemd/system目录下,或者用vi生成redis-sentinel.service文件;

vi生成redis-sentinel.service文件步骤:

1)运行编辑文件命令:

vi /etc/systemd/system/redis-sentinel.service

2)按下键盘i进行输入,复制文件内容,右击黏贴;

3)退出编辑并保存:按下键盘Esc,并输入:“:wq”

redis-sentinel.service 文件内容为:

[Unit]

Description=redis-sentinel-server

After=redis-server

[Service]

Type=forking

ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/sentinel.conf --sentinel

PrivateTmp=true

[Install]

WantedBy=multi-user.target

6)设置redis-sentinel开机自启

配置至少启动3台哨兵(与redis放在一台机器即可)

重载配置:systemctl daemon-reload

启动服务:systemctl start redis-sentinel.service

添加开机自启:systemctl enable redis-sentinel.service

查看服务状态1):systemctl status redis-sentinel.service

查看服务状态2):ps -ef|grep redis

注:查看服务状态:

systemctl status 服务名

systemctl restart 服务名

7)查看哨兵相应的日志文件

运行:tail -f /usr/local/redis/sentinel-26379.log

结束查看:ctrl +c

三、查看哨兵状态

1)连接哨兵,查看主服务器

运行:/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 26379

查看哨兵的主服务:SENTINEL masters

查看哨兵的丛服务:SENTINEL slaves mymaster

退出哨兵:quit

2)连接主服务器,查看主从信息

运行(-a后面为密码):

/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a 666666

运行:info

运行:info Replication

3)通过工具查看哨兵状态

在windows上安装:Another-Redis-Desktop-Manager.1.5.2

AnotherRedisDesktopManager 发行版 - Gitee.comhttps://gitee.com/qishibo/AnotherRedisDesktopManager/releases

连接哨兵,配置如下

猜你喜欢

转载自blog.csdn.net/qq_26408545/article/details/123047550
今日推荐