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 <PASSWORD> 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>
### 安装 Redis 为了在 Linux 系统上安装 Redis,可以采用多种方法。一种常见的做法是在 CentOS 7 上利用 `yum` 工具来简化这一过程: ```bash sudo yum install -y redis ``` 这一步骤会自动处理依赖关系并完成软件包的安装[^1]。 对于那些偏好从源码构建的人来说,则需预先确保系统已配备 GCC 编译器环境,可通过如下命令实现: ```bash yum install gcc-c++ ``` 之后可前往官方网站获取最新版 Redis 的压缩包,并将其解压至目标位置再依照官方文档指导执行编译与安装操作[^4]。 ### 修改配置文件以启用远程访问功能 欲使 Redis 支持来自外部网络节点的数据请求,在成功安装后还需进一步编辑其配置文件 `/etc/redis.conf` 或者其他自定义路径下的相应文件。具体而言就是找到 bind 参数所在行取消注释状态并将 IP 地址设为0.0.0.0表示接受任何来源地连接;另外建议设定密码保护机制提高安全性。 ```conf bind 0.0.0.0 requirepass your_password_here ``` 最后记得重启服务让更改生效: ```bash service redis restart ``` 或者依据不同发行版特性选用 systemctl 命令替代上述指令[^3]。 ### 调整防火墙策略开放端口 为了让外界能够顺利抵达本机上的 Redis 实例,还需要确认服务器的安全组规则允许 TCP 流量通过默认监听端口号6379。如果当前环境中启用了 iptables 或 firewalld ,则应添加例外条目放行该特定流量。 针对使用 firewalld 的场景,可以通过下面这条命令永久开启所需端口: ```bash firewall-cmd --zone=public --add-port=6379/tcp --permanent firewall-cmd --reload ``` 以上措施综合起来即构成了完整的解决方案框架用于实现在 Linux 平台上架设可供远端机器存取资料之 Redis 数据库实例的过程[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值