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

或者如下内容:(二选一,不能用就换个) 

[Unit]
Description=Redis
After=network.target

[Service]
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli shutdown
Restart=always

[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 gcc
	yum -y install wget
	if [ ! -f "redis-5.0.7.tar.gz" ]; then
		#下载源码
		wget http://download.redis.io/releases/redis-5.0.7.tar.gz
	fi
	#解压
	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/conf
	mkdir -p /usr/local/redis/logs
	cp -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.service
	touch /etc/systemd/system/redis.service && echo '#!/bin/bash'>>/etc/systemd/system/redis.service
	sed -i '$a\[Unit]' /etc/systemd/system/redis.service
	sed -i '$a\Description=redis-server' /etc/systemd/system/redis.service
	sed -i '$a\After=network.target' /etc/systemd/system/redis.service
	sed -i '$a\[Service]' /etc/systemd/system/redis.service
	sed -i '$a\Type=forking' /etc/systemd/system/redis.service
	sed -i '$a\ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf' /etc/systemd/system/redis.service
	sed -i '$a\PrivateTmp=true' /etc/systemd/system/redis.service
	sed -i '$a\[Install]' /etc/systemd/system/redis.service
	sed -i '$a\WantedBy=multi-user.target' /etc/systemd/system/redis.service
	echo '启动'
	systemctl start redis
	systemctl status redis
	systemctl 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.service
	touch /etc/systemd/system/sentinel.service && echo '#!/bin/bash'>>/etc/systemd/system/sentinel.service
	sed -i '$a\[Unit]' /etc/systemd/system/sentinel.service
	sed -i '$a\Description=sentinel-server' /etc/systemd/system/sentinel.service
	sed -i '$a\After=network.target' /etc/systemd/system/sentinel.service
	sed -i '$a\[Service]' /etc/systemd/system/sentinel.service
	sed -i '$a\Type=forking' /etc/systemd/system/sentinel.service
	sed -i '$a\ExecStart=/usr/local/redis/bin/redis-sentinel /usr/local/redis/conf/sentinel.conf' /etc/systemd/system/sentinel.service
	sed -i '$a\PrivateTmp=true' /etc/systemd/system/sentinel.service
	sed -i '$a\[Install]' /etc/systemd/system/sentinel.service
	sed -i '$a\WantedBy=multi-user.target' /etc/systemd/system/sentinel.service
	echo '启动'
	systemctl start sentinel
	systemctl status sentinel
	systemctl 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: 5000
    sentinel:
      master: mymaster
      # 哨兵的IP:Port列表
      nodes: 192.168.1.101:26379,192.168.1.102:26379,192.168.1.103:26379
    # database: 0
    # host: sc-redis
    jedis:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
#单机模式的配置
spring:
  redis:
    #database: 0
    host: 192.168.1.10
    lettuce:
      pool:
        max-active: 50
        max-idle: 50
        max-wait: 60
        min-idle: 10
      shutdown-timeout: 6000
    password: xxxxxx
    port: 6379

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值