Redis实战和核心原理详解(1)Centos7.0下安装Redis 5.0详细过程和使用常见问题

一、系统环境

1.1、服务器环境

序号 节点名称(hostname) 服务器版本 Linux版本 IP地址
1 node1 CentOS Linux release 7.3.1611 (Core) Linux version 3.10.0-514.el7.x86_64 192.168.1.51
2 node2 CentOS Linux release 7.3.1611 (Core) Linux version 3.10.0-514.el7.x86_64 192.168.1.243
3 node3 CentOS Linux release 7.3.1611 (Core) Linux version 3.10.0-514.el7.x86_64 192.168.1.252

在这里插入图片描述

这里准备了三台机器,方便单机的时候搭建和搭建集群,这里使用的SSH界面工具是Secure CRT。

1.2、其他软件版本和下载地址

序号 软件 版本 下载地址
1 Redis 5.0.4 http://download.redis.io/releases/redis-5.0.4.tar.gz
2 Secure CRT scrt-x64.8.5.3.1867 https://www.vandyke.com/cgi-bin/releases.php?product=securecrt
2 WinSCP WinSCP-5.15 https://winscp.net/eng/download.php

1.3、修改主机hostname

因为这个是已经事先做好的了,为了告诉大家如何实现的,命令如下:

[xuliugen@node1 ~]$ hostnamectl set-hostname node1
[xuliugen@node1 ~]$ hostname
node1

在这里插入图片描述

分别把三台机器都设置了。

1.4、修改hosts

使用命令进行修改:

vim /etc/hosts

添加hosts内容为:

192.168.1.51  node1
192.168.1.243 node2
192.168.1.252 node3

分别修改三台机器的hosts

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.5、关闭防火墙

下述的操作过程中,始终是关闭了防火墙的,关闭的命令如下:

centos 7:
systemctl stop firewalld.service #停止
systemctl disable firewalld.service #禁用

centos 7之前的版本:
service iptables stop #停止
chkconfig iptables off #禁用

如果只是想开启某一个端口,例如:6379的话,可以搜索一下具体的配置过程,这里不再累述。

二、Redis单机部署

注意:单机部署的时候,只使用服务器node1,即IP:192.168.1.51

2.1、安装Redis

将Redis安装包下载到/home/xuliugen/software目录下,依次执行命令:

$ wget http://download.redis.io/releases/redis-5.0.4.tar.gz
$ tar xzf redis-5.0.4.tar.gz
$ cd redis-5.0.4
$ make

在这里插入图片描述

2.1.1、问题1:

如果你的安装过程比较顺利的话,应该是可以直接成功的,我的安装出现如下错误:
在这里插入图片描述

[root@node1 redis-5.0.4]# make
cd src && make all
make[1]: Entering directory `/home/xuliugen/software/redis-5.0.4/src'
    CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/home/xuliugen/software/redis-5.0.4/src'
make: *** [all] Error 2
[root@node1 redis-5.0.4]# 

经搜索得知是因为没有安装gcc,使用命令安装gcc:yum install gcc一路回车即可!

然后再次执行make,如果出现如下错误的话:

2.1.2、问题2

在这里插入图片描述

[root@node1 redis-5.0.4]# make
cd src && make all
make[1]: Entering directory `/home/xuliugen/software/redis-5.0.4/src'
    CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
 #include <jemalloc/jemalloc.h>
                               ^
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/home/xuliugen/software/redis-5.0.4/src'
make: *** [all] Error 2
[root@node1 redis-5.0.4]# 

原因是jemalloc重载了Linux下的ANSI C的malloc和free函数。解决办法:make时添加参数。

make MALLOC=libc

发现运行OK了!

如果大家还遇到了其他问题,直接把错误的信息扔到百度进行搜索,基本都是可以解决的哈!

2.2、启动Redis

至此Redis已经安装完成,首先试一下能不能把启动:

启动命令(在/home/xuliugen/software/redis-5.0.4目录下执行):

[root@node1 src]# pwd
/home/xuliugen/software/redis-5.0.4/src
[root@node1 src]# ./redis-server ../redis.conf 

[root@localhost redis-3.2.1]# ./src/redis-server ../redis.conf

2.3、连接Redis

在这里插入图片描述

三、Redis可视化图形工具介绍

目前市面上有很多关于Redis的可视化操作界面,如:Redis Desktop Manager、等,

3.1、Redis Desktop Manager

官网下载:https://redisdesktop.com/download

github地址:https://github.com/uglide/RedisDesktopManager/releases

在这里插入图片描述

不过Redis Desktop Manager目前只有Linux版本的是免费的,Windows和Mac的安装包都是收费的,或者可以提一个Pull Request然后可以免费试用一年。

在这里插入图片描述
安装和使用比较简单,这里不在赘述!

另外,Window和Mac平台都支持通过编译安装的方式使用,有兴趣的可以了解一下:
http://docs.redisdesktop.com/en/latest/install/
在这里插入图片描述
在这里插入图片描述

3.2、Redis Client

GitHub地址:https://github.com/caoxinyu/RedisClient
下载地址:https://github.com/caoxinyu/RedisClient/releases
在这里插入图片描述
在这里插入图片描述
下载完之后,直接安装即可!

3.3、其他客户端可视化工具

四、使用Redis Client连接Redis

4.1、设置Redis外网可访问

值得注意的是在3.2.0以后的新版本中引入了一种proteced mode 模式,详见:http://redis.io/topics/security

在不修改配置文件任何内容的情况下,有以下几个默认的配置,简单的就是:

bind 127.0.0.1
protected-mode yes
# requirepass foobared

默认绑定的是127.0.01,默认开启了:protected-mode模式,按照官方的说法,如果默认开启了protected-mode模式在没有配置绑定IP和密码的情况下,是只允许回环地址进行访问的,就只允许127.0.0.1进行访问,那我们就在默认的配置下进行启动,通过SSH工具在其他机器上进行访问,看看运行的效果:

在这里插入图片描述

很显然是没有办法访问到,在3.2.0以前的版本中可以将绑定的IP进行修改为本机IP,例如我运行Redis的服务器IP为192.168.1.51,那我的配置为0.0.0.0,顺便指定Redis的密码,# requirepass foobared 将这一行去掉注释,选择自己喜欢的密码,重启服务即可正常访问。

通过外网访问Redis可能会遇到这个问题,Redis protected-mode 是3.2 之后加入的新特性,在redis.conf的注释中,我们可以了解到,他的具体作用和启用条件:

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode yes

可以看到 protected-mode 是为了禁止公网访问redis cache,加强redis安全的。它启用的条件,有两个:
1) 没有bind IP ;
2) 没有设置访问密码;

如果启用了,则只能够通过lookback ip(127.0.0.1)访问Redis cache,如果从外网访问,则会返回相应的错误信息,就是上图中的信息。

因此在新的版本中,应该配置绑定IP和访问密码,这样的话才不会报错误,在Redis的一个论坛中,老外也探讨了这个问题,可以参考:https://www.reddit.com/r/redis/comments/3zv85m/new_security_feature_redis_protected_mode/

最终修改配置如下:

bind 192.168.1.51
protected-mode yes
requirepass redis_pass_123456

然后链接之后,就可以查看了!

在这里插入图片描述

五、其他常见问题

5.1、启动的时候没有设置配置文件

这个版本的时候需要指定,如果不指定的话,在后期修改了配置文件不会起到对应的效果

11292:C 25 Jul 13:13:58.034 # Warning: no config file specified, using
the default config. In order to specify a config file use
./redis-server /path/to/redis.conf

这个说的是在启动的时候要制定配置文件,如果没有指定的话就会按照默认的配置,因此我们要制定具体的位置。

5.2、启动时报错及解决方法

[root@localhost redis-3.2.1]# ./src/redis-server ../redis.conf

1、WARNING 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.

2、WARNING: The TCP backlog setting of 511 cannot be enforced because
/proc/sys/net/core/somaxconn is set to the lower value of 128.

解决方法其实按照上边的说明就可以解决:

5.2.1、第一个警告两个方式解决(overcommit_memory)

echo "vm.overcommit_memory=1" > /etc/sysctl.conf  或 vi /etcsysctl.conf

然后reboot重启机器,重启之后执行下边的内容

echo 1 > /proc/sys/vm/overcommit_memory   不需要启机器就生效

5.2.2、第二个警告解决

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

其实在报错信息的时候已经给出了解决的方法,按照给定的具体的方法解决即可。

5.2.3、在上述 2 中的解决方法的一些参数说明

(1)overcommit_memory参数说明:

设置内存分配策略(可选,根据服务器的实际情况进行设置)

/proc/sys/vm/overcommit_memory 

可选值:0、1、2。

0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存

注意:redis在dump数据的时候,会fork出一个子进程,理论上child进程所占用的内存和parent是一样的,比如parent占用 的内存为8G,这个时候也要同样分配8G的内存给child,如果内存无法负担,往往会造成redis服务器的down机或者IO负载过高,效率下降。所 以这里比较优化的内存分配策略应该设置为 1(表示内核允许分配所有的物理内存,而不管当前的内存状态如何)。

(2)这里又涉及到Overcommit和OOM。
什么是Overcommit和OOM,在Unix中,当一个用户进程使用malloc()函数申请内存时,假如返回值是NULL,则这个进程知道当前没有可用内存空间,就会做相应的处理工作。许多进程会打印错误信息并退出。
Linux使用另外一种处理方式,它对大部分申请内存的请求都回复”yes”,以便能跑更多更大的程序。因为申请内存后,并不会马上使用内存。这种技术叫做Overcommit。
当内存不足时,会发生OOM killer(OOM=out-of-memory)。它会选择杀死一些进程(用户态进程,不是内核线程),以便释放内存。

(3)Overcommit的策略

Linux下overcommit有三种策略(Documentation/vm/overcommit-accounting):

启发式策略。合理的overcommit会被接受,不合理的overcommit会被拒绝。
任何overcommit都会被接受。
当系统分配的内存超过swap+N%*物理RAM(N%由vm.overcommit_ratio决定)时,会拒绝commit。
overcommit的策略通过vm.overcommit_memory设置。
overcommit的百分比由vm.overcommit_ratio设置。

echo 2 > /proc/sys/vm/overcommit_memory
echo 80 > /proc/sys/vm/overcommit_ratio

当oom-killer发生时,linux会选择杀死哪些进程选择进程的函数是oom_badness函数(在mm/oom_kill.c中),该函数会计算每个进程的点数(0~1000)。点数越高,这个进程越有可能被杀死。每个进程的点数跟oom_score_adj有关,而且oom_score_adj可以被设置(-1000最低,1000最高)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章