1、CENTOS7下安装REDISCENTOS7下安装REDIS主从服务1、单机版安装redis安装若没有vim环境,可以通过yum安装:yum install vim第一步:下载redis安装包redis-3.2.11.tar.gz第二步:上传安装包,并解压压缩包tar -zxvf redis-3.2.11.tar.gzrootlocalhost local# tar -zxvf redis-3.2.11.tar.gz第三步:yum安装gcc依赖yum install gccrootlocalhost local# yum install gcc遇到选择,输入y即可第四步:跳转到redis解压目
2、录下cd redis-3.2.11rootlocalhostlocal# cd redis-3.2.11第五步:编译安装make MALLOC=libcrootlocalhost redis-3.2.11# make MALLOC=libc将/root/redis-3.2.11/src目录下的文件加到/usr/local/bin目录cp -r /root/redis-3.2.11/src/ /usr/local/bin/cd src& make installrootlocalhost redis-3.2.11# cd src& make install第六步:测试是否安装成功先切换到redi
3、ssrc目录下rootlocalhost redis-3.2.11# cd src配置1、直接启动redis./redis-serverrootlocalhostsrc# ./redis-server如上图:redis启动成功,但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。按 ctrl + c可以关闭窗口。2、以后台进程方式启动redis第一步:修改redis.conf文件vim /etc/redis/6379.conf将daemonize no修改为daemonizeyes关闭保护模式,注释掉ip绑定bind 127.0.0.1protected-mode yes修改为#bi
4、nd 127.0.0.1protected-mode no第二步:指定redis.conf文件启动./redis-server /usr/local/redis-3.2.11/redis.confrootlocalhostsrc# ./redis-server /usr/local/redis-3.2.11/redis.conf18713:C 13 Dec 13:07:41.109 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo18713:C 13 Dec 13:07:41.109 # Redis version=4.0.6, bits=64,
5、commit=00000000, modified=0, pid=18713, just started18713:C 13 Dec 13:07:41.109 # Configuration loaded第三步:关闭redis进程首先使用ps -aux | grep redis查看redis进程rootlocalhostsrc# ps -aux | grep redisroot 18714 0.0 0.1 141752 2008 ? Ssl 13:07 0:00 ./redis-server 127.0.0.1:6379root 18719 0.0 0.0 112644 968 pts/0 R
6、+ 13:09 0:00 grep -color=auto redis使用kill命令杀死进程rootlocalhostsrc# kill -9 187143、设置redis开机自启动1、在/etc目录下新建redis目录mkdir /etc/redisrootlocalhostetc# mkdir /etc/redis2、将/root/redis-3.2.11/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.confrootlocalhostredis# cp /root/redis-3.2.11/redis.conf /etc/redis/6379.con
7、f3、将redis的启动脚本复制一份放到/etc/init.d目录下cp /root/redis-3.2.11/utils/redis_init_script /etc/init.d/redisd4、设置redis开机自启动先切换到/etc/init.d目录下然后执行自启命令rootlocalhostinit.d# chkconfigredisd onservice redisd does not support chkconfig看结果是redisd不支持chkconfig解决方法:使用vim编辑redisd文件,在第一行加入如下两行注释,保存退出vim /etc/init.d/redisd
8、# chkconfig: 2345 90 10# description: Redis is a persistent key-value database注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是1再次执行开机自启命令,成功rootlocalhostinit.d# chkconfigredisd on4、redis启动和关闭启动:service redisd startrootlocalhost # service redisd startStarting Redis server.2288:C 13 Dec 13:51:38.0
9、87 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo2288:C 13 Dec 13:51:38.087 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=2288, just started2288:C 13 Dec 13:51:38.087 # Configuration loaded关闭:service redisd stoprootlocalhost # service redisd stopStopping .Redis stopped2、安装redis主从
10、1.主从架构1.1 Redis主从架构拓扑图结构2 主从结构搭建需先安装单机版Redis,参考1 单机安装redis。1.2.1 在安装好单机版的前提下,分别修改每台设备配置文件设置主从在Redis中设置主从有2种方式:1.在redis.conf中设置slaveofa) slaveof2、 使用redis-cli客户端连接到redis服务,执行slaveof命令a) slaveof第二种方式在重启后将失去主从复制关系。我们这里使用第一种方式设置主从,比如以192.168.2.203做主服务器,则在其他设备上配置所属主服务器地址::vim /etc/redis/6379.conf增加slaveo
11、f 192.168.2.203 63791.2.5 查看Redis主从关系使用Redis客户端连接上主库6379端口# redis-cli -h 192.168.2.30 -p 6379查看Redis主从关系 如下图所示192.168.29.128:6379 info replicationrole:角色信息slaveX:从库信息connected_slaves:从库数量1.2.6 测试在主库写入数据在从库读取数据3.从库只读默认情况下redis数据库充当slave角色时是只读的不能进行写操作可以在配置文件中开启非只读:slave-read-onlyno4.主从复制的过程原理1.当从库和主库建
12、立MS关系后,会向主数据库发送SYNC命令2.主库接收到SYNC命令后会开始在后台保存快照(RDB持久化过程),并将期间接收到的写命令缓存起来3.当快照完成后,主Redis会将快照文件和所有缓存的写命令发送给从Redis4.从Redis接收到后,会载入快照文件并且执行收到的缓存的命令5.之后,主Redis每当接收到写命令时就会将命令发送从Redis,从而保证数据的一致3、安装redis 哨兵1、Sentinel的作用:A、Master 状态监测B、如果Master 异常,则会进行Master-slave 转换,将其中一个Slave作为Master,将之前的Master作为Slave C、Mas
13、ter-Slave切换后,master_redis.conf、slave_redis.conf和sentinel.conf的内容都会发生改变,即master_redis.conf中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换 2、Sentinel的工作方式:1):每个Sentinel以每秒钟一次的频率向它所知的Master,Slave以及其他 Sentinel 实例发送一个 PING 命令 2):如果一个实例(instance)距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选项所指定的值, 则这个实例会被 Sen
14、tinel 标记为主观下线。 3):如果一个Master被标记为主观下线,则正在监视这个Master的所有 Sentinel 要以每秒一次的频率确认Master的确进入了主观下线状态。 4):当有足够数量的 Sentinel(大于等于配置文件指定的值)在指定的时间范围内确认Master的确进入了主观下线状态, 则Master会被标记为客观下线 5):在一般情况下, 每个 Sentinel 会以每 10 秒一次的频率向它已知的所有Master,Slave发送 INFO 命令 6):当Master被 Sentinel 标记为客观下线时,Sentinel 向下线的 Master 的所有 Slave
15、发送 INFO 命令的频率会从 10 秒一次改为每秒一次 7):若没有足够数量的 Sentinel 同意 Master 已经下线, Master 的客观下线状态就会被移除。 若 Master 重新向 Sentinel 的 PING 命令返回有效回复, Master 的主观下线状态就会被移除。3、Sentinel配置修改配置文件,配置主redis地址、端口、投票数目,增加后台启动daemonize yes注意:三个redis服务器同时修改配置文件sentinel.confvim /root/redis-3.2.11/sentinel.conf# 初次配置时的状态,这个sentinel会自动更新s
16、entinel monitor mymaster192.168.2.3063791daemonizeyeslogfile./sentinel_log.log1)如果redis实例没有配置protected-mode yesbind 192.168.98.136则在sentinel 配置文件加上protected-mode no 即可2)如果redis实例有配置protected-mode yesbind 192.168.98.136则在sentinel 配置文件加上protected-mode yesbind 192.168.98.136所有服务器修改完后,再次重启redis所有的服务端4、s
17、entinel启动service redisdstopservice redisd start进入redis安装目录cd/root/redis-3.2.11/执行启动命令redis-server sentinel.conf -sentinel &查看已启动redis进程会有2个:关闭服务:pkillredis-server /这个会关掉Redis服务器和Sentinel(哨兵)进程 kill 进程号/可以关掉指定进程号的进程5、测试我们手动关闭Master 之后,sentinel 在监听master 确实是断线了之后,将会开始计算权值,然后重新分配主服务器;登录slaver服务器查看状态我们可以看到当前slave已经升级为master我们次启动原来master服务,我们可以看到原master成为了slave
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1