Centos redis安装及配置

来源:互联网 发布:专业光束灯光编程 编辑:程序博客网 时间:2024/05/20 15:36

团队在用,安装之后,从其他服务器telnet不通,自己实际安装配置了一下,并做了测试。

首先在redis官网下载了最新的版本,https://redis.io/;

将该文件拷贝到/usr/local下,进行解压,tar -zxvf  redis-4.0.6.tar.gz;

解压后得到redis-4.0.6目录,cd 到该目录下,执行make命令;

在redis-4.0.6目录下,新建一个redis目录,将src下的redis-server,redis-benchmark,redis-cli,及redis配置文件redis.conf拷贝到redis目录下;

cd 到redis目录下,

./redis-server redis.conf来启动redis服务

测试是否启动成功:

./redis-cli

redis> set foo bar

OK

redis>get foo

"bar"

以上能够顺利执行,说明安装配置成功了。

注意:这时通过telnet测试端口连接,会出现下列情况:

telnet localhost 6379   ok,能够连接

telnet 127.0.0.1 6379  ok,能够连接

telnet 192.168.56.102(本机的ip) 6379  bad,连接不上

在其它主机上,执行telnet 192.168.56.102 6379 bad,也是连接不上的

这时怎么回事呢,因为我们是用的redis的默认配置,所以需要修改redis.conf文件。

打开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 yes修改为protected-mode no。

仅仅修改完上述地方,还是不可以的,我们再看看这么一段描述:

################################## NETWORK #####################################


# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1

这一段是说默认情况下,只能监听来自127.0.0.1 6379的请求,当然localhost也可以,其他理解不了,比如通过主机名或者ip地址,那么要实现通过主机名或者IP地址连接,该如何做呢?

我们看到Examples:这个地方了吗?按照这个提示,我们将最后一行bind 127.0.0.1 修改 bind 127.0.0.1 192.168.56.102(本机的IP地址),这样就可以从其他任何主机上通过IP地址连接redis服务啦(当然,本机也是可以的)。

参考文档:http://www.runoob.com/redis/redis-install.html,https://segmentfault.com/q/1010000007547672?_ea=1380149

原创粉丝点击