linux 下安装运行redis

1 首先下载redis到  /opt文件夹

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

2 下载完成之后进行解压:

  tar xzvf redis-5.0.4.tar.gz

3 解压完成之后   执行make  编译redis 

执行的过程中如果出现这个错说明linux没有安装c++或者版本低,这里笔者因为没有安装,所以安装下

yum install gcc-c++

安装完成之后 在执行  make distclean  清理掉之前执行make命令产生的文件

4  执行 make install  会将一些可执行脚本生成到  /usr/local/bin   (默认安装路径)目录下面

5 cd /usr/local/bin 

1 第一个文件是测试 redis 性能测试脚本:  比如测试redis 1秒钟可以写多少条数据 

2 第二个文件是修复 aof文件的: redis数据持久化模式: 增量持久化

3 第三个是修复 rdb文件的:   rdb模式全量同步持久化数据

4  第四个是连接客户端的脚本

5 第五个文件是哨兵集群配置文件

6 第六个是启动redis服务器

6 设置外网可以访问redis 及其它配置

  注意要关闭防火墙 否则可能外部不能访问

编辑配置文件(所有配置文件均为在原来的基础上修改,不需添加)

vi /opt/redis-5.0.4/redis.conf

redis默认只允许本机连接,所以需要找到“bind 127.0.0.1”并将这行注释掉: 后面访问时会报错,后面做解释

注释掉 bind 127.0.0.1 和设置0.0.0.0 都是任意ip都可以访问,但是他们还是有区别的

下面还有一个:   protected-mode yes  保护模式默认是开启的   表示只能本机访问

bind 和protected-mode yes 是配合使用

将默认的“daemonize no”改为yes,设置redis以守护线程方式启动: 以后台服务运行

在这里插入图片描述

下面的redis_6379.pid 如果有多台redis的话 要改成当前redis的端口

日志级别: loglevel notice

日志存放地址:  logfile  ""

dir ./  表示redis 产生的一些文件在哪里启动的redis那么产生的一些文件就会存放在哪 里, 比如: 持久化文件,dump.rdb,apend文件等

如果有多个redis 那么多个redis产生的文件都会放在 这个文件夹下面,所以我们也需要改这个文件地址

7 启动redis

/usr/local/bin/redis-server  /opt/redis-5.0.4/redis.conf
前面是redis启动脚本的地址,后面是配置文件地址

查看redis启动pid 

ps -ef|grep redis 

 

8 用客户端连接测试

进入redis  bin文件夹

cd /usr/local/bin

执行 redis-cli 脚本 

9 java 代码连接测试 

   public static void main(String[] args){
       Jedis jedis=new Jedis("xx.xx.xx.xx",6379);
       System.out.println(jedis.ping());
   }

发生了异常:

Exception in thread "main" 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.
    at redis.clients.jedis.Protocol.processError(Protocol.java:127)
    at redis.clients.jedis.Protocol.process(Protocol.java:161)
    at redis.clients.jedis.Protocol.read(Protocol.java:215)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:196)
    at.redis.RedisConfig.main(RedisConfig.java:61)

看标黑的就是原因

之前说过redis-config 注释 bind和 bind 0.0.0.0是有区别的

现在将 redis-config 中的bind 改为  bind  0.0.0.0

杀死redis进程并且重启

现在执行上面的java代码就可以正常输出:  pong

我们可以看到 注释  bind和改为 bind 0.0.0.0 区别

   访问配置: 

       前提访问墙关闭或者开放端口

      1 将 bing  改为  bind  0.0.0.0, protected-mode no   外部可以访问   不会报错

       2 将 bind 注释   protected-mode yes (默认)   外部通过密码访问    也是不会报错的

       这就是这两者的区别

    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坑里水库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值