在Ubuntu24.04LTS系统上如何安装redis

科技一点鑫得 2024-05-13 04:54:44
安装redis命令

Ubuntu 24.0 LTS版本软件源中已内置Redis 7.0.15版本,安装非常简单

sudo apt updatesudo apt install redis-server

安装完成之后,redis-server会自动启动,下面的命令可以查看服务的状态

sudo systemctl status redis-server配置远程访问

默认情况下,redis不能远程访问,注释掉/etc/redis/redis.conf文件中这一行配置

# bind 127.0.0.1 -::1

然后重启redis服务

sudo systemctl restart redis-server

重新查看redis服务状态,绑定端口信息为*:6379,表示已支持远程连接

root@zx-VirtualBox:/home/zx# systemctl status redis-server● redis-server.service - Advanced key-value store Loaded: loaded (/usr/lib/systemd/system/redis-server.service; enabled; preset: enabled) Active: active (running) since Sun 2024-05-12 23:20:18 CST; 3min 28s ago Docs: http://redis.io/documentation, man:redis-server(1) Main PID: 1247 (redis-server) Status: "Ready to accept connections" Tasks: 5 (limit: 7027) Memory: 6.6M (peak: 7.1M) CPU: 281ms CGroup: /system.slice/redis-server.service └─1247 "/usr/bin/redis-server *:6379"配置访问密码

不过,这个时候如果尝试在另外一个机器上连接redis不会成功,可能会遇到错误Client On Error: Error: write ECONNRESET Config right?,这个报错信息对解决问题帮助不大,可以在安装redis的机器上使用其真实ip地址借助redis-cli命令行进行连接(注意不要使用127.0.0.1地址),随便执行一个简单的命令,发现给出了错误提示,意思是启用了保护模式,如果不设置密码远程机器是不能使用的。

root@zx-VirtualBox:/home/zx# redis-cli -h 192.168.56.101192.168.56.101:6379> set key_test value_test(error) DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. 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 an authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.192.168.56.101:6379>

默认配置下redis并没有启用密码验证,接下来,编辑/etc/redis/redis.conf配置文件,设置redis的访问密码,取消注释下面的内容,并将foobared设置为自己指定的密码,再重启redis服务

requirepass foobared

再次远程连接redis就正常了

0 阅读:27
猜你喜欢

科技一点鑫得

简介:感谢大家的关注