redis系列(1)之安装和集群部署

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: ubuntu下安裝1. 下载Redis的安装包(x.x.x表示版本)wget http://download.redis.io/releases/redis-x.x.

ubuntu下安裝

1. 下载Redis的安装包(x.x.x表示版本)

wget http://download.redis.io/releases/redis-x.x.x.tar.gz

2.在目录下,解压按照包,生成新的目录redis-x.x.x

tar xvfz redis-x.x.x.tar.gz

3. 进入解压之后的目录,进行编译

cd redis-2.8.9
sudo make
//如果没有明显的错误,则表示编译成功

4.安装

sudo make install

5.测试是否安装成功

sudo make test

6.启动redis服务

进入redis的目录,再进入src目录。

./redis-server 

这时候是用了默认的配置
如果要用自己的配置可以在后面加入配置文件路径

./redis-server ../redis.conf 

查看日志

 src/redis-server ../redis.conf  > ../logs/log.out 2>&1 &
查看redis日志
(或者配置文件中配置)
tail logs/log.out

7.连接redis服务

./redis-cli

如果用密码,则–》auth password

密码的设置在conf文件中–》requirepass foobared 改为自己的密码即可。

8.java测试连接

使用jredis.jar

    @Test
    public void link(){
        //连接服务器的 Redis 服务
        Jedis jedis = new Jedis("192.168.247.133");
        jedis.auth("redis");
        System.out.println("Connection to server sucessfully");
        //查看服务是否运行
        System.out.println("Server is running: "+jedis.ping());
    }

9.密码设置

redis.conf 中requirepass foobared 改为自己的密码
jedis.auth(“redis”); 表示redis的密码如果不设置,默认会报错

redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, 
no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface.
 If you want to connect from external computers to Redis you may adopt one of the following solutions: 
 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host
 the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.
  2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 
  3) If you started the server manually just for testing, restart it with the '--protected-mode no' option.
 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
按照要求修改配置文件即可,但是启动的时候记得加入配置文件路径。

安装方式二

在 Ubuntu 系统安装 Redi 可以使用以下命令:

$sudo apt-get update
$sudo apt-get install redis-server

这里写图片描述

集群配置

1.Redis官方集群方案 Redis Cluster

3.0版本后才开始支持集群案

2.Redis Sharding(分片)集群

master-slave-sentinel方案

redis的方案采用master-slave-sentinel方案。于是分配三个主机。

角色分配为

master: 192.168.1.100:6379

slave: 192.168.1.101:6380,192.168.1.102:6381

sentinel: 192.168.1.100:26379, 192.168.1.101:26380 , 192.168.1.102:2681

1 主 2 从 3 哨兵

3.1 防火墙

三台主机开通tcp策略

-A INPUT -m state —state NEW -m tcp -p tcp —dport16661 -j ACCEPT

-A INPUT -m state —state NEW -m tcp -p tcp —dport16662 -j ACCEPT

3.2 创建工作目录

在每台主机上创建目录

mkdir –p /opt/redis-xx

3.3 配置文件

u 3台主机各需要一份redis.conf文件

u 3台主机也需要一份sentinel.conf配置文件

将redis-3.2.4/下的redis.conf 拷贝到各个主机工作目录(/opt/redis- xx /)

cp /opt/redis-3.2.4/redis.conf /opt/redis-xx/ xxxxxxxxxx
将redis-3.2.4/下的sentinel.conf 拷贝到各个主机工作目录(/opt/redis-mss/)

cp /opt/redis-3.2.4/sentinel.conf /opt/redis-xx/
master的redis.conf部分配置如下

配置master

bind 192.16.1.100     #bind 当前主机的ip
protected-mode no  # 取消掉保护模式
timeout 60
port 16661                                  # redis服务端口,
daemonize yes
logfile /opt/redis-mss/redis.log              # 日志目录,
dbfilename dump.rdb   # db文件的名字
dir /opt/redis-mss      # db文件所在的目录,最终db文件在/opt/redis-mss/dump.rdb
slave的redis.conf部分配置如下

配置slave

protected-mode no  # 取消掉保护模式
timeout 60
port 16661                                  # redis服务端口,
daemonize yes
logfile /opt/redis-mss/redis.log           # 日志目录,
dbfilename dump.rdb      # db文件的名字
dir /opt/redis-mss         # db文件所在的目录,最终db文件在/opt/redis-mss/dump.rdb
slaveof 192.168.1.100 6379    # 填写master的ip和port
sentinel的sentinel.conf的部分配置如下

添加下面两行

daemonize yes
logfile /opt/redis-mss/sentinel.log

修改

protected-mode no  # 取消掉保护模式
port 26379
dir /opt/redis-mss
# sentinel monitor <master-name><ip><redis-port><quorum>
# quorum 表示要多少台sentinel 主观判断master断开时,执行auto-failover
sentinel monitor mymaster 192.168.1.100 6379 2 
# sentinel down-after-milliseconds <master-name><milliseconds>
# 多少毫秒内会认为master挂了,默认30s,
sentinel down-after-milliseconds mymaster 5000

需要注意的是,img这个2表示3台sentinel中有2台主观认为master宕机时,就执行自动主从切换(auto-failover)。这个值应该尽可能靠近sentinel总数的50%。

3.5 启动

第一次启动集群先要启动master

/opt/redis-3.2.4/src/redis-server /opt/redis-xx/redis.conf
然后启动slav(117, 118)

/opt/redis-3.2.4/src/redis-server /opt/redis-xx/redis.conf
最后启动sentinel

/opt/redis-3.2.4/src/redis-server /opt/redis-mss/sentinel.conf --sentinel
各个节点的日志都会放在相应的/opt/redis-xx目录下

我的官网
我的博客

我的官网http://guan2ye.com
我的CSDN地址http://blog.csdn.net/chenjianandiyi
我的简书地址http://www.jianshu.com/u/9b5d1921ce34
我的githubhttps://github.com/javanan
我的码云地址https://gitee.com/jamen/
阿里云优惠券https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=vf2b5zld&utm_source=vf2b5zld

阿里云教程系列网站http://aliyun.guan2ye.com

1.png

我的开源项目spring boot 搭建的一个企业级快速开发脚手架

1.jpg

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
7天前
|
NoSQL Linux 测试技术
Redis的安装(Linux版)
Redis的安装(Linux版)
150 1
|
17天前
|
NoSQL Linux Redis
linux 下和win下安装redis 并添加开机自启 图文详解
linux 下和win下安装redis 并添加开机自启 图文详解
17 0
|
2月前
|
NoSQL Redis 数据安全/隐私保护
Docker中Redis的安装与配置
本文主要讲解如何在Docker环境中搭建Redis环境,并进行相关配置
247 5
Docker中Redis的安装与配置
|
1月前
|
NoSQL Linux Redis
Linux系统中安装redis+redis后台启动+常见相关配置
Linux系统中安装redis+redis后台启动+常见相关配置
|
2月前
|
NoSQL Redis Docker
在docker中安装redis,并且阿里云服务器配置
在docker中安装redis,并且阿里云服务器配置
187 1
|
25天前
|
NoSQL Linux Redis
Linux安装Redis
Linux安装Redis
29 0
|
29天前
|
NoSQL 关系型数据库 MySQL
安装Docker&镜像容器操作&使用Docker安装部署MySQL,Redis,RabbitMQ,Nacos,Seata,Minio
安装Docker&镜像容器操作&使用Docker安装部署MySQL,Redis,RabbitMQ,Nacos,Seata,Minio
386 1
|
1月前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
221 0
|
1月前
|
NoSQL Redis 数据安全/隐私保护
Docker安装和使用Redis
Docker安装和使用Redis
78 0
|
2月前
|
存储 NoSQL Redis
【docker专题_06】docker安装redis
【docker专题_06】docker安装redis

热门文章

最新文章