本文主要是介绍Cento7安装Redis5.0.7(一键安装脚本),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.单机模式
安装依赖:
cd /opt
yum install -y gcc
下载源码包:
wget http://download.redis.io/releases/redis-5.0.7.tar.gz
解压:
tar -zxvf redis-5.0.7.tar.gz
cd redis-5.0.7
编译:
make
安装到指定目录:
make install PREFIX=/usr/local/redis
从源码中复制配置文件
mkdir /usr/local/redis/conf
cp /opt/redis-5.0.7/redis.conf /usr/local/redis/conf/
编辑redis.conf
将daemonize设置为yes可以后台一直运行
若想远程连接则需要将bind 127.0.0.1注掉
设置开机自启新建文件:vi /etc/systemd/system/redis.service
输入如下内容:
[Unit]
Description=redis-server
After=network.target[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
PrivateTmp=true[Install]
WantedBy=multi-user.target
启动/停止/查看状态redis: systemctl start/stop/status redis
开机自启:systemctl enable redis
操作redis:
cd /usr/local/redis/bin
./redis-cli -h 127.0.0.1 -p 6379
清空redis所有数据:flushall
清空所有key:flushdb
2.主从复制之哨兵模式
概念自行百度(一大堆)
准备三台机器:
192.168.1.101(主)、192.168.0.102(从)、192.168.0.103(从)
redis.conf配置
master的 redis.conf 配置:
masterauth 123456 #master redis密码
slave的 redis.conf 配置:(旧版的是slaveof,新版用replicaof)
replicaof 192.168.1.101 6379 # IP和端口是master的IP和端口
masterauth 123456 #master redis密码
启动redis命令,也可以使用上述开机启动命令
bin/redis-server conf/redis.conf
校验命令,分别在各个机器上进入redis-cli并使用
进入redis-cli
#/usr/local/redis/bin/redis-cli
输入认证密码
>auth "123456"
打印信息
>info replication
也可以一件部署,编写一键部署自动脚本:redis_install.sh
#!/bin/bash
function install_redis () {echo '开始安装'cd /opt#安装工具和环境yum install -y gccyum -y install wgetif [ ! -f "redis-5.0.7.tar.gz" ]; then#下载源码wget http://download.redis.io/releases/redis-5.0.7.tar.gzfi#解压tar -zxvf redis-5.0.7.tar.gz#进入目录cd redis-5.0.7#编译源码make#安装到/usr/local/redis目录make install PREFIX=/usr/local/redis#拷贝配置文件mkdir -p /usr/local/redis/confmkdir -p /usr/local/redis/logscp -f /opt/redis-5.0.7/redis.conf /usr/local/redis/conf/#修改内容echo '修改配置文件内容'#日志文件位置sed -i 's@logfile.*@logfile /usr/local/redis/logs/redis.log@' /usr/local/redis/conf/redis.conf#后台运行sed -i 's#daemonize no#daemonize yes#g' /usr/local/redis/conf/redis.conf#修改绑定sed -i 's#^bind 127.0.0.1#bind 0.0.0.0#g' /usr/local/redis/conf/redis.conf#修改密码sed -i 's/.*requirepass.*foobared/requirepass 123456/' /usr/local/redis/conf/redis.conf#修改端口#sed -i 's/port 6379/port 6379' /usr/local/redis/conf/redis.conf#加入集群master的密码sed -i '$a\masterauth 123456' /usr/local/redis/conf/redis.conf#从服务器配置(主服务运行脚本需要注掉下面这句话,从服务放开)#sed -i '$a\replicaof 192.168.1.101 6379' /usr/local/redis/conf/redis.conf#创建开机启动脚本echo '创建开机启动脚本'rm -rf /etc/systemd/system/redis.servicetouch /etc/systemd/system/redis.service && echo '#!/bin/bash'>>/etc/systemd/system/redis.servicesed -i '$a\[Unit]' /etc/systemd/system/redis.servicesed -i '$a\Description=redis-server' /etc/systemd/system/redis.servicesed -i '$a\After=network.target' /etc/systemd/system/redis.servicesed -i '$a\[Service]' /etc/systemd/system/redis.servicesed -i '$a\Type=forking' /etc/systemd/system/redis.servicesed -i '$a\ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf' /etc/systemd/system/redis.servicesed -i '$a\PrivateTmp=true' /etc/systemd/system/redis.servicesed -i '$a\[Install]' /etc/systemd/system/redis.servicesed -i '$a\WantedBy=multi-user.target' /etc/systemd/system/redis.serviceecho '启动'systemctl start redissystemctl status redissystemctl enable redis
}
install_redis
注意主从配置的地方 replicaof 那里,主服务不需要,主服务密码masterauth和redis的密码requirepass自行修改脚本
放到/opt目录下,执行命令:
sh redis_install.sh
最终打印效果:
哨兵配置sentinel.conf
(源码自带,放在目录中:/opt/redis-5.0.7/sentinel.conf)
修改以下内容即可(所有节点都是相同配置)
#是否以守护线程的形式运行,默认是no
daemonize yes
#端口
port 26379
#哨兵日志
logfile "/usr/local/redis/logs/sentinel.log"
#指定别名mymaster 主节点地址 端口 主观宕机个数(哨兵认为的主观宕机个数,最好是哨兵总数的半数以上)
sentinel monitor mymaster 192.168.0.101 6379 2
#如果哨兵20s内没有收到主节点的心跳,哨兵就认为主节点宕机了,默认是30秒
sentinel down-after-milliseconds mymaster 20000
#配置连接redis主节点密码
sentinel auth-pass mymaster 123456
启动哨兵命令
src/redis-sentinel sentinel.conf
校验哨兵命令
src/redis-cli -h 192.168.1.101 -p 6379
>auth '123456'
>info Sentinel
这里也写一个自动脚本sentinel_start.sh
#!/bin/bash
function start_sentinel () {echo '开始配置' #拷贝配置文件cp -f /opt/redis-5.0.7/sentinel.conf /usr/local/redis/conf/#修改内容echo '修改配置文件内容'#日志文件位置sed -i 's@logfile.*@logfile "/usr/local/redis/logs/sentinel.log"@' /usr/local/redis/conf/sentinel.conf#后台运行sed -i 's#daemonize no#daemonize yes#g' /usr/local/redis/conf/sentinel.conf#修改主服务地址sed -i 's#^sentinel monitor.*#sentinel monitor mymaster 192.168.1.101 6379 2#g' /usr/local/redis/conf/sentinel.conf#修改超时时间sed -i 's/^sentinel down-after-milliseconds.*/sentinel down-after-milliseconds mymaster 20000/' /usr/local/redis/conf/sentinel.conf#修改端口#sed -i 's/port 26379/port 26379' /usr/local/redis/conf/sentinel.conf#添加连接redis主节点密码(密码要配置在sentinel monitor mymaster xxx之后)sed -i '/^sentinel monitor mymaster.*/a\sentinel auth-pass mymaster 123456' /usr/local/redis/conf/sentinel.conf#创建开机启动脚本echo '创建开机启动脚本'rm -rf /etc/systemd/system/sentinel.servicetouch /etc/systemd/system/sentinel.service && echo '#!/bin/bash'>>/etc/systemd/system/sentinel.servicesed -i '$a\[Unit]' /etc/systemd/system/sentinel.servicesed -i '$a\Description=sentinel-server' /etc/systemd/system/sentinel.servicesed -i '$a\After=network.target' /etc/systemd/system/sentinel.servicesed -i '$a\[Service]' /etc/systemd/system/sentinel.servicesed -i '$a\Type=forking' /etc/systemd/system/sentinel.servicesed -i '$a\ExecStart=/usr/local/redis/bin/redis-sentinel /usr/local/redis/conf/sentinel.conf' /etc/systemd/system/sentinel.servicesed -i '$a\PrivateTmp=true' /etc/systemd/system/sentinel.servicesed -i '$a\[Install]' /etc/systemd/system/sentinel.servicesed -i '$a\WantedBy=multi-user.target' /etc/systemd/system/sentinel.serviceecho '启动'systemctl start sentinelsystemctl status sentinelsystemctl enable sentinel
}
start_sentinel
执行脚本:
sh sentinel_start.sh
在Sentinel作用下Master-Slave切换后,master的redis.conf、slave的redis.conf和它们的sentinel.conf内容都会发生改变,即master的redis.conf中会多一行slaveof(旧版使用slaveof)或者replicaof的配置,sentinel.conf的监控目标会随之调换。
测试
src/redis-cli
>auth '123456'
>info Replication
可以看到101是master,102和103是slave,停止主服务,之后再利用上述几行命令测试,找到master已经切换到其他服务了,再次启动101的redis,查看Replication已经变成了slave
三个可以停止redis的命令:
>shutdown
kill -9
systemctl stop redis
另外附上SpringBoot连接不同Redis的方式
#哨兵模式的配置
spring:redis:timeout: 5000sentinel:master: mymaster# 哨兵的IP:Port列表nodes: 192.168.1.101:26379,192.168.1.102:26379,192.168.1.103:26379# database: 0# host: sc-redisjedis:pool:max-active: 8max-idle: 8min-idle: 0
#单机模式的配置
spring:redis:#database: 0host: 192.168.1.10lettuce:pool:max-active: 50max-idle: 50max-wait: 60min-idle: 10shutdown-timeout: 6000password: xxxxxxport: 6379
这篇关于Cento7安装Redis5.0.7(一键安装脚本)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!