当前位置: 移动技术网 > IT编程>数据库>MongoDB > linux上安装redis的踩坑过程

linux上安装redis的踩坑过程

2018年05月02日  | 移动技术网IT编程  | 我要评论

证婚,仙杀,2尺6是多少厘米

   redis用处很广泛,我不再啰嗦了,我按照网上教程想在linux上安装下,开始了踩坑过程,网上买了一个linux centos7.3,滴滴云的,巨坑无比啊,不建议大家用这家的! redis 为4.0,

1.登录云服务并下载安装:

   首先登录不是root用户,需要切换下,执行sudo su切为root用户,感觉这样很不方便,于是咨询下,回复是这样的:

 

   (1) 然后换回root用户了,开始wget下载,提示没有这个命令,好吧,开始安装它yum -y install wget

  下载完后就解压, 执行:tar xzf redis-4.0.9.tar.gz

  (2) 进入 cd redis-4.0.9,执行 make,此时有可能报错: 

 gcc -std=c99 -pedantic -c -O3 -fPIC  -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb  net.c  类似的;那么再去安装GCC环境, yum -y install gcc automake autoconf libtool make

 参考链接:https://blog.csdn.net/ygl6150373/article/details/50984420

 (3)安装成功,开始启动前准备,修改redis.conf里 bind的IP,直接注释掉这一行,让外网可以访问,如果是3.2以后版本,还需要修改保护模式 protected-mode yes 改为no,注意修改完这个配置后,下次启动要指定 redis.conf,不然不起作用!

2.开始启动

  如果你linux没什么问题可能就直接启动成功了,但有些时候会报3个警告,比如我就碰到了,2.8不会,4.0会,猜想可能是4.0对内存要求高了

  第一个警告:The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

  意思是:TCP  backlog设置值,511没有成功,因为 /proc/sys/net/core/somaxconn这个设置的是更小的128.

 

 临时解决方法:(即下次启动还需要修改此值)

 

 echo 511 > /proc/sys/net/core/somaxconn

 

 永久解决方法:(即以后启动还需要修改此值)

 

 将其写入/etc/rc.local文件中。

 当然上面这个解决办法是网上的,我试了,直接改somaxconn下次启动仍然这样,而写到rc.local我不知道怎么写,直接写也是没用的,所以我又找到另外一个方法是可以的

在/etc/sysctl.conf中添加如下

net.core.somaxconn = 2048,此值根据你的内存自己写定义,大于511即可

 

第二个警告:overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to/etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

意思是:overcommit_memory参数设置为0!在内存不足的情况下,后台程序save可能失败。建议在文件 /etc/sysctl.conf 中将overcommit_memory修改为1。 

 这个警告的处理我忘记了这个是否有效,应该有效的

 

第三个警告:you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix thisissue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain thesetting after a reboot. Redis must be restarted after THP is disabled.

意思是:你使用的是透明大页,可能导致redis延迟和内存使用问题。执行 echo never > /sys/kernel/mm/transparent_hugepage/enabled 修复该问题。

临时解决方法:

echo never > /sys/kernel/mm/transparent_hugepage/enabled。

永久解决方法:

将其写入/etc/rc.local文件中。

 

 这个解决方案也是网上的,但是根本没用,至少我这里是没用的,所以我采取的是这个,修改etc/default/grub  里值,GRUB_CMDLINE_LINUX="console=tty0 crashkernel=auto console=ttyS0,115200 transparent_hugepage=never"

 

 在此行后面加了 transparent_hugepage=never,并执行,参考连接:https://blog.csdn.net/msdnchina/article/details/72968215

亲测是有效的.

 

3.远程连接

  终于顺利启动了, 启动顺序这样: cd /redis/redis-4.0.9/src ,然后执行 ./redis-server  ../redis.conf,记住,一定要指定conf文件,不然上面修改IP和保护模式都没用了.

不容易啊,现在开始在另外一个窗口里进入redis, cd /redis/redis-4.0.9/src,执行./redis-cli,进入redis,set个键取下看是否正常,然后开始进入远程连接;

 我本地用的springboot,推荐大家使用,sprinboot自带有springboot-redis功能,会自动加载redis配置文件,但它使用的redistemplate感觉并不好用,所以此时我用的还是jedis

 但一访问就报错,Caused by: java.net.SocketTimeoutException: connect timed out,然后我试着去关闭防火墙,如下:

  firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

  systemctl stop firewalld.service #停止

  firewall systemctl disable firewalld.service #禁止firewall开机启动

  init 6 #重启虚拟机,然后再输入第一条命令查看防火墙状态

 在centos7.2里这样操作防火墙是没问题的,然后并没什么用,还是time out,试了很多方法还是不行,本地可以Ping通,就是访问不了,此时突然想起我用的坑货云了,那里是访问白名单设置,

 

 

 我早就设置了6379端口的白名单,却忘了绑定服务器,这里的DC2相当于阿里的ECS,结果一直报超时,无奈,改了这个之后,马上就可以了.

然后又报下面的错误 :

JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error"异常

或者:Unable to validate object

或者可能会有下面大段的错误: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.

此时说明redis.conf里设置没起作用,说明你启动时忘了指定conf,于是按正确方法启动  ./redis.server ../redis.conf,终于可以ping 通了

 

 

总结下:在安装完redis后,修改bindIP,和保护模式,并修改服务器防火墙的端口访问,在服务器上能访问redis的情况下,如果远程不能访问时,请检查下是否使用了云服务器及云服务器的IP及端口限制是否设置正常.

另外,还有其它设置,如是否使用密码,超时等待时间,持久化策略等下次再更.

 

 

 

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网