非关系数据库——redis群集
更新:HHH   时间:2023-1-7


redis群集,实验环境两台服务器,六个节点,每台服务器三个网卡

6个节点三个主,三个副本

主服务器安装redis

[root@localhost ~]# yum install gcc gcc-c++ make -y  #安装环境包
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.25/redis /abc/ 挂载
Password for root@//192.168.100.25/redis:  
[root@localhost ~]# cd /abc/
[root@localhost abc]# ls
redis-5.0.7.tar.gz
[root@localhost abc]# tar zxvf redis-5.0.7.tar.gz -C /opt/ #解压到opt底下
[root@localhost abc]# cd /opt/redis-5.0.7/
make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis install #安装
[root@localhost redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/ #做软连接
[root@localhost redis-5.0.7]# cd /opt/redis-5.0.7/utils/
[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/redis/bin/redis-server

从服务器(一样的操作不重复了)

主服务器配置redis

带“#”的原本配置文件的内容方便写shell脚本

[root@localhost utils]# vim /etc/redis/6379.conf 
  #70 bind 127.0.0.1
  70 #bind 127.0.0.1
  #89 protected-mode yes
  89 protected-mode no #关闭保护功能
#833 # cluster-enabled yes
833  cluster-enabled yes  #开启群集功能
#841 # cluster-config-file nodes-6379.conf
841  cluster-config-file nodes-6379.conf #开启群集功能配置文件
#847 # cluster-node-timeout 15000
847  cluster-node-timeout 15000 #设置群集超时时间为15000秒
 #700 appendonly no
 700 appendonly yes #开启AOF支持
[root@localhost redis]# /etc/init.d/redis_6379 restart #开起redis
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
[root@localhost utils]# cd /var/lib/redis/
[root@localhost redis]# cd 6379/
[root@localhost 6379]# ls
appendonly.aof  dump.rdb  nodes-6379.conf
#aof配置文件,rdb快照文件,节点配置文件

从服务器一样的操作

主服务器安装rvm,ruby控制群集

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 #导入密钥
[root@localhost 6379]# curl -sSL https://get.rvm.io | bash -s stable #寻找这个地址安装RVM

[root@localhost 6379]# cd /opt/
[root@localhost opt]# ls
redis-5.0.7  rh
[root@localhost opt]# vim abc.sh #rvm的脚本很长
[root@localhost opt]# ls
abc.sh  redis-5.0.7  rh
[root@localhost opt]# chmod +x abc.sh 
[root@localhost opt]# ls
abc.sh  redis-5.0.7  rh
[root@localhost opt]# source /etc/profile.d/rvm.sh 
[root@localhost opt]# rvm list known #查看rvm安装的版本

[root@localhost opt]# rvm install 2.4.1 #安装rvm2.4.1版本

[root@localhost opt]# rvm use 2.4.1 #使用rvm
Using /usr/local/rvm/gems/ruby-2.4.1
[root@localhost opt]# ruby -v #查看ruby的版本
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
[root@localhost opt]# gem install redis #安装redis

主服务器安装三块网卡
192.168.136.229
192.168.136.232
192.168.136.231
从服务器安装三块网卡
192.168.136.185
192.168.136.233
192.168.136.234

两台服务器都重启网路服务

[root@localhost opt]# systemctl restart network

两台服务器关闭防火墙和增强功能

[root@localhost opt]# systemctl stop firewalld.service
[root@localhost opt]# setenforce 0

主服务器,重启redis,创建群集

[root@localhost 6379]# /etc/init.d/redis_6379 restart #重启redis
redis-cli --cluster create 192.168.136.229:6379 192.168.136.232:6379 192.168.136.231:6379 192.168.136.185:6379 192.168.136.233:6379 192.168.136.234:6379 --cluster-replicas 1

Can I set the above configuration? (type 'yes' to accept): yes #出现这行输入yes
三个节点master,三个副本节点,彼此对应关系是随机的,但是是一主带一从。

开始验证

[root@localhost opt]# redis-cli -h 192.168.136.229 -p 6379 #登录一台主服务器229
192.168.136.229:6379> set name zhangsan #创建键值对
OK
192.168.136.229:6379> keys * #查看所有键
1) "name"
192.168.136.229:6379> get name  #查看值
"zhangsan"
192.168.136.229:6379> quit
[root@localhost opt]# redis-cli -h 192.168.136.234 -p 6379 #登录一台从服务器234
192.168.136.234:6379> keys *
1) "name"
192.168.136.234:6379> get name #查看数据,查看不了,会提示你数据存放在231服务器中
(error) MOVED 5798 192.168.136.231:6379
[root@localhost opt]# redis-cli -h 192.168.136.231 -p 6379 #登录231服务器,就看到数据了
192.168.136.231:6379> keys *
1) "name"
192.168.136.231:6379> get name
"zhangsan"

用hash方式建立键值对,设置键的删除时间

[root@master opt]# redis-cli -h 192.168.136.229 -p 6379 
192.168.136.229:6379> hset person age 20   ##用hash方式建立键值对
(integer) 1
192.168.136.229:6379> hset person name lisi
(integer) 1
192.168.136.229:6379> keys *
1) "person"
192.168.136.229:6379> hget person age   ##获取键的值
"20"
192.168.136.229.128:6379> expire person 5     ##设置键的删除时间5s
(integer) 1
192.168.136.229:6379> keys *
1) "person"
192.168.136.229:6379> keys *
(empty list or set)

以上就是我们全部的内容了,谢谢收看

返回云计算教程...