linux redis 远程访问

通常来说,生产环境下的Redis服务器只设置为仅本机访问(Redis默认也只允许本机访问)。有时候我们也许需要使Redi能被远程访问。此文介绍配置Redis允许远程访问。

配置

修改Redis配置文件/etc/redis/redis.conf,找到bind那行配置:

vim /etc/redis/redis.conf

去掉#注释并改为:

bind 0.0.0.0

在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,再访问redisd时候还是报错,如下

 

(error) 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.

 

修改办法:protected-mode no

指定配置文件然后重启Redis服务即可:

sudo redis-server /etc/redis/redis.conf

重启 redis 服务

sudo service redis-server restart

关于bind配置的含义,配置文件里的注释是这样说的:

# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# 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

设置永久密码

Redis的安装目录中找到Redis的配置文件redis.conf(或redis.windows.conf),打开配置文件找到SECURITY部分,可以发现如下代码:

# Require clients to issue AUTH  before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared

添加requirepass yourpassword(此处注意,行前不能有空格),保存之后,重启Redis服务。

 

远程连接

配置好Redis服务并重启服务后。就可以使用客户端远程连接Redis服务了。命令格式如下:

$ redis-cli -h {redis_host} -p {redis_port}

其中{redis_host}就是远程的Redis服务所在服务器地址,{redis_port}就是Redis服务端口(Redis默认端口是6379)。例如:

$ redis-cli -h 120.120.10.10 -p 6379
redis>ping
PONG
[root@43-c58542295-0048 ~]# redis-cli -h 127.0.0.1 -p 6379 -a foobared
127.0.0.1:6379> 

 

你可能感兴趣的:(db,redis)