Redis(主从复制搭建)
1.主从复制表示图https://img-blog.csdnimg.cn/img_convert/0004e1798294be73258d91a3eefd3a67.png
2.搭建一主多从
1.搭建规划三台机器(一主二从)
2.将两台从Redis服务都按照同样的方式配置(可以理解为Redis初始化)
1.安装Redis
1.yum安装gcc
yum install gcc
2.查看gcc版本
gcc --version
https://img-blog.csdnimg.cn/img_convert/f8082b152691536fe5c622409fee6a5a.png
3.将redis6.2.6上传到/opt目次下
https://img-blog.csdnimg.cn/img_convert/c55bff79410cf1e203e1b977b4a03304.png
4.进入/opt目次下然后解压
cd /opt && tar -zxvf redis-6.2.6.tar.gz
https://img-blog.csdnimg.cn/img_convert/e77fbdfbe041354667096fefe4241576.png
5.进入 redis-6.2.6目次
cd redis-6.2.6
https://img-blog.csdnimg.cn/img_convert/96fdb739cc3714eb871791ef6a894401.png
6.编译并安装
make && make install
https://img-blog.csdnimg.cn/img_convert/01f2f1b6fcbece1c97b5c0674e673bb9.png
7.进入 /usr/local/bin 查看是否有redis的下令
cd /usr/local/bin && ll
https://img-blog.csdnimg.cn/img_convert/11730878bb4af2645b39ab1f99309c9e.png
2.启动并使用Redis
1.进入 /opt/redis-6.2.6/
cd /opt/redis-6.2.6/
https://img-blog.csdnimg.cn/img_convert/b074263848eca63cd7389ffbc02efc55.png
2.将内里的redis.conf复制一份到/etc下
cp redis.conf /etc/redis.conf
https://img-blog.csdnimg.cn/img_convert/15e7fa914c7db553b1bd71da23f9e852.png
3.编辑 /etc/redis.conf,将daemonize no修改成daemonize yes即以保卫进程的方式启动(后台启动)
vim /etc/redis.conf
https://img-blog.csdnimg.cn/img_convert/e8f2860451ae8a997da5d13ea6c6d8bd.png
4.启动redis,指定刚才的配置文件
/usr/local/bin/redis-server /etc/redis.conf
5.查看redis进程
ps -aux | grep redis
https://img-blog.csdnimg.cn/img_convert/f301927cb68e25e6704b1baaffd21f11.png
3.Redis根本配置
1.修改端口为7489 port
https://img-blog.csdnimg.cn/img_convert/f3dc366519ac8b85408a5f357aee11dc.png
2.设置redis暗码 requirepass
https://img-blog.csdnimg.cn/img_convert/95a276e999c93215cd3ed58388b574c0.png
3.使redis支持远程访问 bind
https://img-blog.csdnimg.cn/img_convert/a7d1734c91ca3f9b83249cdd545bfda2.png
https://img-blog.csdnimg.cn/img_convert/3ce680f478faeaf5a063367e6359ce2e.png
4.登录redis的下令行,关闭redis
/usr/local/bin/redis-cli
https://img-blog.csdnimg.cn/img_convert/89ffff2b0de3c887787c9b6c98793be8.png
5.重新启动redis,使配置见效
/usr/local/bin/redis-server /etc/redis.conf
&& ps -aux | grep redis
https://img-blog.csdnimg.cn/img_convert/64181c0f43681668d120fae16dc49615.png
4.开启7489端口
1.宝塔开启端口
systemctl start firewalld && firewall-cmd --permanent --add-port=7489/tcp && firewall-cmd --reload && firewall-cmd --query-port=7489/tcp
https://img-blog.csdnimg.cn/img_convert/a459dd28ab10bcb6e752458f208f311e.png
2.腾讯云开启端口(只答应本机ip访问)
https://img-blog.csdnimg.cn/img_convert/e331fc8bf22a7c43595a761fa04c2295.png
5.Redis长期化配置
1.进入redis配置文件
vim /etc/redis.conf
2.dbfilename为redis长期化的文件名(一样寻常不消改)
https://img-blog.csdnimg.cn/img_convert/20a6a524d0ef2fc3b601e3c6b0820a5e.png
3.dir修改为/root/则每次长期化的dump.rdb都会在/root/下,恢复时无论在哪里启动,都会读取这个文件进行恢复
https://img-blog.csdnimg.cn/img_convert/476eb056f107f8b65cbd2c36fd553948.png
4.开启AOF长期化配置,编辑配置文件找到appendonly,设置成yes
https://img-blog.csdnimg.cn/img_convert/f4711420815117dc5c1c6b0e09c03b1e.png
5.进入下令行关闭redis,需要指定端口登录
/usr/local/bin/redis-cli
-p 7489 https://img-blog.csdnimg.cn/img_convert/15f15ce1e9aca0762a1993b24151e404.png
6.重新启动redis,使配置见效
/usr/local/bin/redis-server /etc/redis.conf
&& ps -aux | grep redis
https://img-blog.csdnimg.cn/img_convert/049590c54150c316f7ef5d3858257217.png
7.发现/root/下面有两个配置文件,如果没有dump.rdb是因为没有对redis进行操作
https://img-blog.csdnimg.cn/img_convert/7a5ee96648a451614f64b504076709a7.png
6.测试Java连接redis
1.引入jedis的jar包
https://img-blog.csdnimg.cn/img_convert/61e793735c4a8aa6689aaac1bf8992c6.png
2.编写测试程序
public static void main(String[] args) {
// 连接服务器的redis命令行
Jedis jedis = new Jedis("xxx", xx);
// 如果redis设置了密码要先进行验证
jedis.auth("******");
String ping = jedis.ping();
System.out.println(ping);
}
3.配置Redis的一主二仆
1.在配置之前,将三台的Redis的7489端口完全放开
https://img-blog.csdnimg.cn/img_convert/a857d66b244fee92d9f94d4244e688e1.png
2.三台机器都进入redis的客户端,将数据全部清除
https://img-blog.csdnimg.cn/img_convert/01e36d4aad45bcae465d7d3ea412c800.png
3.输入 info replication
可以看到目前三台机器都为master
https://img-blog.csdnimg.cn/img_convert/1e018dccabb22191bc1ceccf7f63e9c4.png
4.在两台从服务上输入 slaveof 主服务ip 主服务端口 来设置主服务
slaveof xxxxxxx
5.输入 info replication
来查看角色
1.从服务
https://img-blog.csdnimg.cn/direct/129e4d2dfd224369bdfb42a49f32c42e.png
2.主服务
https://img-blog.csdnimg.cn/img_convert/fc05ac8fab4b4316698896709a17f647.png
3.没连接成功,厥后询问GPT4发现如果主服务器配置了暗码,则需要在从服务器的masterauth 中设置一下暗码
https://img-blog.csdnimg.cn/img_convert/08671943d07725c8e8dccff1d053958c.png
6.为两台从服务器的 masterauth 配置主服务器的暗码,然后测试连接
1.分别设置暗码
https://img-blog.csdnimg.cn/img_convert/4dbd0c295fb1be3c054adc30164f1ffb.png
2.分别重启
https://img-blog.csdnimg.cn/img_convert/8917dbd61fdf1eda3746b86626b75ce5.png
3.从服务器重新配置
slaveof 。。。。。。
https://img-blog.csdnimg.cn/direct/14ef72dd80664e629b66ac1a784d8c8e.png
4.再查看一下主服务器的状态,也是成功连接两台从服务器
info replication
https://img-blog.csdnimg.cn/direct/5f62933c87c64e1489eb11b1af627e5a.png
7.注意事项和细节
1.如果想要长期化,需要在从服务的配置文件中配置 slaveof … 否则重启主从关系就会消散
2.主服务器可读可写,从服务器只可读
3.主从复制原理分析
1.原理表示图
https://img-blog.csdnimg.cn/img_convert/cc949c25785839a33d040f1233d3e498.png
2.解读
https://img-blog.csdnimg.cn/img_convert/632b1ae959d54d1f804647e47cbe3e75.png
3.细节
1.主服务down掉了的情况
[*]从服务并不会抢占主服务的位置
[*]当主服务重新启动之后,从服务又会指向主服务
2.当从服务down掉了的情况
[*]当再次连接主服务时还是会进行一次全量备份
4.薪火相传
[*]简单来说就是让b是a的slave,c是b的slave
[*]这样c的数据就从b来获取,而写入操作还是从a进行
https://img-blog.csdnimg.cn/img_convert/1a0b623aa5d5b2075162e16aeef23525.png
5.反客为主(是在薪火相传的基础上的)
https://img-blog.csdnimg.cn/img_convert/63c3121b5c758053bf6a1cd8c3ecbaf5.png
4.哨兵模式(sentinel)
1.配置哨兵
1.保持一主二仆的情况即可
2.任意找一台服务器启动哨兵,这里选择在master服务器启动
1.在/etc下面创建一个sentinel.conf的配置文件
touch /etc/sentinel.conf
2.编辑文件,设置master的名字,ip+端口以及哨兵的个数,这里是1,还有master的暗码
vim /etc/sentinel.conf
sentinel monitor redis_master ........
3.再开启一个连接,启动哨兵,指定配置文件
/usr/local/bin/redis-sentinel /etc/sentinel.conf
https://img-blog.csdnimg.cn/img_convert/8b1fac8d2c6e917fbec8028e65b0e2d1.png
2.测试
1.关闭master
https://img-blog.csdnimg.cn/img_convert/13da2f6c84778ea8a0769e43b02e5061.png
2.等候一会,查看哨兵,可以看到切换了master
3.查看子节点的情况,注意:此时需要重新进行暗码验证
https://img-blog.csdnimg.cn/direct/63725abdfa964f69b8edea0261bd07e2.png
https://img-blog.csdnimg.cn/direct/637554f156dc4726a3e62ef81bf4ed1b.png
4.重启原来的master,会自动降级为子节点
https://img-blog.csdnimg.cn/direct/05401c539193416bb7ebcaece52d5b3c.png
3.注意事项和细节
1.主机down后的实行流程
https://img-blog.csdnimg.cn/img_convert/489be2cb6b7060863e8f2f05b6d5516e.png
2.哨兵挑选新master的依据
https://img-blog.csdnimg.cn/img_convert/5326a9fe870a2bc7623613c2a0876d18.png
3.重启原来的master,会自动降级为子节点
4.如果原来的主节点有暗码,则需要在sentinel.conf 配置auth-pass参数设置暗码
5.关于暗码配置方案
[*]在redis.conf中配置requirepass 以及masterauth 都为相同的暗码,这样各个节点就可以正常连接了
[*]如果是哨兵模式还要在sentinel.conf 配置master的暗码,格式为 sentinel auth-pass master名字yourStrongPassword
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]