Linux安装redis

一、安装gcc

redis依赖gcc环境,安装过程中提示是否继续,直接输入y即可,直到完成

yum install gcc-c++

二、下载redis

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

三、解压

tar -zxvf redis-3.2.8.tar.gz 

四、进入目录编译

cd redis-3.2.8
make

五、安装指定目录

make PREFIX=/usr/local/redis install

至此,redis安装完毕。

六、redis启动

1.前台启动(不推荐使用)

进入bin目录启动redis。

cd /usr/local/redis/
./bin/redis-server

2.后台启动,通过redis.config

拷贝redis.config文件至redis安装目录。

cp redis.conf /usr/local/redis/

进入安装目录

cd /usr/local/redis/   

编辑redis.conf,将daemonize on修改为 daemonize yes

vim redis.conf

后台启动,加载配置文件redis.conf

./bin/redis-server ./redis.conf

查看是否启动成功

ps -ef|grep -i redis

客户端连接

./bin/redis-cli

查看连接是否成功

ping 

七、关闭redis

./bin/redis-cli shutdown

八、Java中使用redis

1.加入依赖jedis

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.9.0</version>
</dependency>

2.连接代码

public void jedisTest(){
    //1、设置地址和端口
    Jedis jedis = new Jedis("192.168.182.128",6379);
    //2、保存数据
    jedis.set("name1","李四");
    //3、获取数据
    String value = jedis.get("name1");
    System.out.println(value);
    //4、释放资源
    jedis.close();
}

九、出现的问题

1.redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect

解决方法:编辑redis.conf,将bind 127.0.0.1注释。redis默认本机访问,bind 127.0.0.1代表只有本机可以访问,可以将允许访问的ip加入,也可以直接注释掉这一行,这样所有机器都可以访问。

vim redis.conf 

2.解决上述问题后出现的新问题:redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode 。报错信息如下:

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.

解决方法:编辑redis.conf,将protected-mode yes 改为protected-mode no。这里原来是yes代表开启了保护模式,后面可以填密码也可以填no代表关闭,我们这里选择关闭保护模式,wq保存退出后再重启redis-server

vim redis.conf 

3.jedis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutE

原因:未打开6379的端口

解决方式: 

①修改防火墙设置,修改配置

vim /etc/sysconfig/iptables 

②打开6379的端口

 添加  -A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT 

③保存退出

④重启防火墙设置

 service iptables restart  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值