其他分享
首页 > 其他分享> > reids的安装以及集群搭建

reids的安装以及集群搭建

作者:互联网

一、安装redis

1、在合适目录下,下载redis源码并解压

cd /usr/local
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
tar -zxvf  redis-6.2.6.tar.gz

2、进入解压文件夹下执行make进行安装,PREFIX指定安装目录,不要与源码混淆,安装完成后源码要删除,避免服务器中目录混乱

cd /usr/local/redis-6.2.6
make PREFIX=/usr/local/redis install

3、在安装目录创建conf目录并将源码中的的redis.conf文件拷贝至安装目录,以后启动指定此配置文件

mkdir /usr/local/redis/conf
cp /usr/local/redis-6.2.6/redis.conf /usr/local/redis/conf/

4、修改redis.conf,修改参数中涉及到修改的地方有

bind 192.168.2.24 127.0.0.1   ##bind表示将此服务暴露于哪个ip,可配置多个,一般配置自己外部访问ip(192.168.2.24)和回环ip(127.0.0.1)

port 6372  ##redis服务的启动端口,reids默认6379,如果部署集群就需要挨个修改

daemonize yes  ##是否后台启动,redis默认为no,是前台启动的,需修改为yes

requirepass 123456 ##设置 Redis 连接密码,如果配置了连接密码,客户端在连接 Redis 时需要通过 AUTH <password> 命令提供密码,默认关闭 pidfile /var/run/redis_6372.pid ##此文件记录redis启动PID,文件名以端口结尾 databases 16 ##有多少个数据库,默认使用数据库0 dbfilename dump.rdb ##redis持久化时存储文件名 dir /usr/local/redis/data/ ##配置redis持久化时存储位置,此目录需要额外创建

maxclients 10000 ##同一时间最大连接数,默认无限制,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis 会关闭新的连接并向客户端返回 max number of clients reached 错误信息

timeout 300 ##客户端闲置多少秒后关闭链接,如果指定为0,则表示关闭此功能

save 300 10 ##指多长时间内,有多少次更新操作,就将数据持久化到磁盘,可以多个条件配合使用 300 10 表示 300秒内有10次操作

5、启动redis,至此redis安装完成

/usr/local/reids/bin/redis-server /usr/local/redis/conf/redis.conf

 二、redis集群搭建

1、在安装目录创建多个配置文件夹来启动多个redis服务

cd  /usr/local/redis
##递归创建redis配置文件夹
mkdir -p redis_cluster/6370 ## 6370  6371  6372

 2、将redis.conf给不同目录下都拷贝一份

cp /usr/local/redis/conf/redis.conf  /usr/local/redis/redis_cluster/6370/  ##每个目录都拷贝一份

3、修改不同端口下的redis.conf

port  6370                                        //端口6370,6371,6372        
bind 本机ip                                       //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 192.168.2.64 否则创建集群时无法访问对应的端口,无法创建集群
daemonize    yes                               //redis后台运行
pidfile  /var/run/redis_6370.pid              //pidfile文件对应7000,7001,7002
cluster-enabled  yes                           //开启集群  把注释#去掉
cluster-config-file  nodes_6370.conf         //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志
dbfilename dump-6370.rdb //redis持久化时存储的文件名
requirepass 123456 //密码
masterauth 123456 //主服务器密码,建议都设置成一样的

 4、启动两台服务器中的多个节点

##第一台机器(192.168.2.64)
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/6370/redis.conf 
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/6371/redis.conf 
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/6372/redis.conf 
##第二台机器(192.168.2.24)
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/6373/redis.conf 
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/6374/redis.conf 
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/6375/redis.conf 

5、检查各个服务是否都已启动成功(如果配置有错,比如端口冲突则会启动失败,又因后台启动,前台无打印)

[root@ora redis_cluster]# ps -ef | grep redis
root 14963 1 0 13:30 ? 00:00:01 ./bin/redis-server 192.168.2.64:6370 [cluster]
root 14969 1 0 13:30 ? 00:00:01 ./bin/redis-server 192.168.2.64:6371 [cluster]
root 15044 1 0 13:34 ? 00:00:01 ./bin/redis-server 192.168.2.64:6372 [cluster]
root 15514 14554 0 14:04 pts/0 00:00:00 grep redis
[root@ora redis_cluster]# netstat -tnlp| grep redis
tcp 0 0 127.0.0.1:16370 0.0.0.0:* LISTEN 14963/./bin/redis-s
tcp 0 0 192.168.2.64:16370 0.0.0.0:* LISTEN 14963/./bin/redis-s
tcp 0 0 127.0.0.1:16371 0.0.0.0:* LISTEN 14969/./bin/redis-s
tcp 0 0 192.168.2.64:16371 0.0.0.0:* LISTEN 14969/./bin/redis-s
tcp 0 0 127.0.0.1:16372 0.0.0.0:* LISTEN 15044/./bin/redis-s
tcp 0 0 192.168.2.64:16372 0.0.0.0:* LISTEN 15044/./bin/redis-s
tcp 0 0 127.0.0.1:6370 0.0.0.0:* LISTEN 14963/./bin/redis-s
tcp 0 0 192.168.2.64:6370 0.0.0.0:* LISTEN 14963/./bin/redis-s
tcp 0 0 127.0.0.1:6371 0.0.0.0:* LISTEN 14969/./bin/redis-s
tcp 0 0 192.168.2.64:6371 0.0.0.0:* LISTEN 14969/./bin/redis-s
tcp 0 0 127.0.0.1:6372 0.0.0.0:* LISTEN 15044/./bin/redis-s
tcp 0 0 192.168.2.64:6372 0.0.0.0:* LISTEN 15044/./bin/redis-s

另一台(192.168.2.24)

[root@project-deve redis]# ps -ef |grep redis
root 26281 1 0 14:05 ? 00:00:00 ./bin/redis-server 192.168.2.24:6373 [cluster]
root 26287 1 0 14:05 ? 00:00:00 ./bin/redis-server 192.168.2.24:6374 [cluster]
root 26293 1 0 14:05 ? 00:00:00 ./bin/redis-server 192.168.2.24:6375 [cluster]
root 26299 25341 0 14:05 pts/0 00:00:00 grep --color=auto redis
[root@project-deve redis]# netstat -tnlp | grep redis
tcp 0 0 127.0.0.1:6375 0.0.0.0:* LISTEN 26293/./bin/redis-s
tcp 0 0 192.168.2.24:6375 0.0.0.0:* LISTEN 26293/./bin/redis-s
tcp 0 0 127.0.0.1:16373 0.0.0.0:* LISTEN 26281/./bin/redis-s
tcp 0 0 192.168.2.24:16373 0.0.0.0:* LISTEN 26281/./bin/redis-s
tcp 0 0 127.0.0.1:16374 0.0.0.0:* LISTEN 26287/./bin/redis-s
tcp 0 0 192.168.2.24:16374 0.0.0.0:* LISTEN 26287/./bin/redis-s
tcp 0 0 127.0.0.1:16375 0.0.0.0:* LISTEN 26293/./bin/redis-s
tcp 0 0 192.168.2.24:16375 0.0.0.0:* LISTEN 26293/./bin/redis-s
tcp 0 0 127.0.0.1:6373 0.0.0.0:* LISTEN 26281/./bin/redis-s
tcp 0 0 192.168.2.24:6373 0.0.0.0:* LISTEN 26281/./bin/redis-s
tcp 0 0 127.0.0.1:6374 0.0.0.0:* LISTEN 26287/./bin/redis-s
tcp 0 0 192.168.2.24:6374 0.0.0.0:* LISTEN 26287/./bin/redis-s

6、创建集群,redis5.0以上已经集成了ruby,不需要单独安装ruby,并且安装集群命令有异 redis-cli(5.0以上)、redis-trib.rb(5.0以下)

##如果设置了密码,则需要加  -a 123456
/usr/local/redis/bin/redis-cli -a 123456 --cluster create 192.168.2.64:6370 192.168.2.64:6371 192.168.2.64:6372 192.168.2.24:6373 192.168.2.24:6374 192.168.2.24:6375 --cluster-replicas 1

7、出现如上截图说明redis集群已经安装成功,下来登陆集群,所谓登陆集群其实就是带参数 -c 登录任意一个节点

## -h ip -p port  -a password  -c 表示登录集群,不加表示登录单个节点
/usr/local/redis/bin/redis-cli -h 192.168.2.24 -p 6373 -a 123456 -c

测试过程中会发现一个很有意思的现象,在6370上写入c=200提示写入了7365数据槽,在24服务器的6373服务上,并且直接切换了过去,并且在此服务上查询无误。这或许就是集群中的数据共享吧,后面再细细研究。

三、集群的管理

四、集群增加数据节点

五、集群的启停

 


标签:bin,0.0,reids,redis,192.168,集群,usr,local,搭建
来源: https://www.cnblogs.com/lansetuerqi/p/15385933.html