阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux系统安装Redis遇到的问题及解决

116次阅读
没有评论

共计 7166 个字符,预计需要花费 18 分钟才能阅读完成。

Redis 用处很广泛,我不再详细说了,按照这里的教程在 Linux 上安装 Redis,开始了踩坑过程,网上买了一个 Linux CentOS 7.3,某云的,巨坑无比啊,Redis 为 4.0。

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

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

Linux 系统安装 Redis 遇到的问题及解决

(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

(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, 并执行,参考如下

RHEL7.2 禁用透明的大页内存 –transparent_hugepage(THP)

[root@rac1 tmp]# cat /etc/RedHat-release
Red Hat Enterprise Linux Server release 7.2 (Maipo)
[root@rac1 tmp]# uname -r
3.10.0-327.el7.x86_64
[root@rac1 tmp]#

[root@rac1 tmp]# cd /etc/default/
[root@rac1 default]# ll
total 12
-rw-r–r–. 1 root root  218 Jun  2 14:04 grub
-rw-r–r–. 1 root root 1756 Aug 14  2015 nss
-rw-r–r–. 1 root root  119 Nov 25  2014 useradd
[root@rac1 default]# cp grub grub.bak ——->>>> 备份 grub 文件
修改 /etc/default/grub 文件, 如下是修改之后的效果:
[root@rac1 default]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=”$(sed ‘s, release .*$,,g’ /etc/system-release)”
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT=”console”
GRUB_CMDLINE_LINUX=”rhgb quiet transparent_hugepage=never” —->>> transparent_hugepage=never 是新加的
GRUB_DISABLE_RECOVERY=”true”
[root@rac1 default]# grub2-mkconfig -o /boot/grub2/grub.cfg —->> 执行该命令
Generating grub configuration file …
Found linux image: /boot/vmlinuz-3.10.0-327.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-327.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-3b932bfbe4f94ccf93c11ba19641e845
Found initrd image: /boot/initramfs-0-rescue-3b932bfbe4f94ccf93c11ba19641e845.img
done
[root@rac1 default]# init 6 ——–>>>> 重启生效

os 重启之后,查看效果

[root@rac1 ~]#  cat /proc/cmdline
BOOT_IMAGE=/vmlinuz-3.10.0-327.el7.x86_64 root=UUID=0dd676f4-cb3b-469d-b5e6-73defbc87f4e ro rhgb quiet transparent_hugepage=never  —->>> 有此行。
[root@rac1 ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]  —->>>[never]表示成功禁用 THP。
[root@rac1 ~]# cd /sys/kernel/mm/
[root@rac1 mm]# ls -lrt
total 0
drwxr-xr-x 4 root root 0 Jun  9 19:32 hugepages
drwxr-xr-x 2 root root 0 Jun  9 19:32 ksm
drwxr-xr-x 3 root root 0 Jun  9 19:35 transparent_hugepage
[root@rac1 mm]# cd transparent_hugepage/
[root@rac1 transparent_hugepage]# ls -lrt
total 0
-rw-r–r– 1 root root 4096 Jun  9 19:35 enabled
-rw-r–r– 1 root root 4096 Jun  9 19:35 use_zero_page
drwxr-xr-x 2 root root    0 Jun  9 19:35 khugepaged
-rw-r–r– 1 root root 4096 Jun  9 19:35 defrag
[root@rac1 transparent_hugepage]# cat enabled
always madvise [never]
[root@rac1 transparent_hugepage]# pwd
/sys/kernel/mm/transparent_hugepage
[root@rac1 transparent_hugepage]# grep AnonHugePages /proc/meminfo
AnonHugePages:        0 kB ———->>> 返回值若是零,代表成功禁用 THP。
[root@rac1 transparent_hugepage]#

亲测是有效的。

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 通, 就是访问不了, 此时突然想起我用的坑货云了, 那里是访问白名单设置,

Linux 系统安装 Redis 遇到的问题及解决

我早就设置了 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 通了。

Linux 系统安装 Redis 遇到的问题及解决

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

又发现服务器很卡,cpu 使用率上升,top 命令查看下,原来有恶意程序在挖矿,此程序入侵了很多 redis 服务器,马上用 kill 杀掉它。

然后开始一些安全策略:

修改 redis 端口号, 允许后台启动, 添加 redis 访问密码

打开 redis.conf 做一些修改

daemonize 改为 yes, 允许后台启动

Linux 系统安装 Redis 遇到的问题及解决

port 端口换掉

Linux 系统安装 Redis 遇到的问题及解决

添加 redis 访问密码

Linux 系统安装 Redis 遇到的问题及解决

完成上面操作,redis 会变得安全和方便些, 然后我们再打开防火墙, 并刚才修改的端口放行

 firewall-cmd –state 查看状态

systemctl start firewalld.service  打开防火墙

Linux 系统安装 Redis 遇到的问题及解决

然后查看端口, 打开端口, 重启即可

Linux 系统安装 Redis 遇到的问题及解决

# 查询端口是否开放
firewall-cmd –query-port=8080/tcp
# 开放 80 端口
firewall-cmd –permanent –add-port=80/tcp
# 移除端口
firewall-cmd –permanent –remove-port=8080/tcp

# 重启防火墙(修改配置后要重启防火墙)
firewall-cmd –reload

# 参数解释
1、firwall-cmd:是 Linux 提供的操作 firewall 的一个工具;
2、–permanent:表示设置为持久;
3、–add-port:标识添加的端口;

做完以上操作, 我们服务器会安全很多。

下面关于 Redis 的文章您也可能喜欢,不妨参考下:

CentOS 7 下 Redis 的安装与配置 https://www.linuxidc.com/Linux/2017-02/140363.htm

Redis 单机 & 集群离线安装部署 https://www.linuxidc.com/Linux/2017-03/141403.htm

CentOS 7.0 安装 Redis 3.2.1 详细过程和使用常见问题 https://www.linuxidc.com/Linux/2016-09/135071.htm

Redis 实战 中文 PDF https://www.linuxidc.com/Linux/2016-04/129932.htm 

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计7166字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中