本文教你在Linux系统上安装和配置Redis服务器,适用RHEL 8/CentOS 8系统。Redis可以用作数据库服务器,消息代理或用于在内存中缓存数据以便更快地检索。
在RHEL 8/CentOS 8上安装Redis RHEL 8上的Redis可在AppStream存储库中找到: $ yum module list redis Updating Subscription Management repositories. Updating Subscription Management repositories. Red Hat Enterprise Linux 8 for x86_64 - AppStream Beta (RPMs) Name Stream Profiles Summary redis 4.0 [d] default [d] Redis persistent key-value database Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled 从输出中可以看出,Appisream上提供了Redis 4.0,使用yum包管理器安装它: sudo yum install -y @redis 安装软件包后,启动并启用Redis服务以在引导时启动: sudo systemctl enable --now redis 服务状态应显示正在运行,如下: $ sudo systemctl status redis redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Sat 2018-12-29 10:00:40 EAT; 5s ago Main PID: 30348 (redis-server) Tasks: 4 (limit: 11510) Memory: 6.4M CGroup: /system.slice/redis.service └─30348 /usr/bin/redis-server 127.0.0.1:6379 Dec 29 10:00:40 rhel8.local systemd[1]: Starting Redis persistent key-value database… Dec 29 10:00:40 rhel8.local systemd[1]: Started Redis persistent key-value database.
在RHEL 8上配置Redis服务器 现在安装了Redis服务器,下一部分是配置,你可以设置许多可调参数以适合你的需求,现在将介绍使用所需的基本设置。请继续阅读。
启用Redis服务以侦听所有接口 默认情况下,Redis服务侦听127.0.0.1,如果需要远程客户端连接,请允许服务侦听所有网络接口: $ ss -tunelp | grep 6379 tcp LISTEN 0 128 127.0.0.1:6379 0.0.0.0:* users:(("redis-server",pid=30348,fd=6)) uid:986 ino:71091 sk:4 <-> 使用vim文本编辑器打开文件/etc/redis.conf: sudo vim /etc/redis.conf 然后更改第69行将127.0.0.1绑定到下面: bind 0.0.0.0 进行更改后重新启动Redis: sudo systemctl restart redis 确认新的绑定地址: $ ss -tunelp | grep 6379 tcp LISTEN 0 128 0.0.0.0:6379 0.0.0.0:* users:(("redis-server",pid=30348,fd=6)) uid:986 ino:71091 sk:4 <->
配置Redis身份验证 在处理任何其他命令之前,为客户端配置Redis身份验证AUTH <PASSWORD>: requirepass <AuthPassword> 例: requirepass StrongPassword
设置持久存储以进行恢复 通过将appendonly设置为yes来设置持久性模式: appendonly yes appendfilename "appendonly.aof" 进行更改后重新启动redis服务: sudo systemctl restart redis 如果有活动的firewalld服务,请允许端口6379: sudo firewall-cmd --add-port=6379/tcp --permanenent sudo firewall-cmd --reload 检查redis服务状态: $ sudo systemctl status redis redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled) Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Sat 2018-12-29 10:11:56 EAT; 9s ago Process: 30485 ExecStop=/usr/libexec/redis-shutdown (code=exited, status=0/SUCCESS) Main PID: 30500 (redis-server) Tasks: 4 (limit: 11510) Memory: 6.4M CGroup: /system.slice/redis.service └─30500 /usr/bin/redis-server 0.0.0.0:6379 Dec 29 10:11:56 rhel8.local systemd[1]: Stopped Redis persistent key-value database. Dec 29 10:11:56 rhel8.local systemd[1]: Starting Redis persistent key-value database… Dec 29 10:11:56 rhel8.local systemd[1]: Started Redis persistent key-value database.
连接到Redis 确认可以在本地连接到Redis服务器: $ redis-cli 127.0.0.1:6379> INFO NOAUTH Authentication required. 测试认证: 127.0.0.1:6379> AUTH <AuthPassword> OK 你应该在输出中看到返回OK字样,如果输入错误的密码,则身份验证应失败: 127.0.0.1:6379> AUTH WrongPassword (error) ERR invalid password 检查redis信息: 127.0.0.1:6379> INFO 这将输出一长串数据,可以通过将Section作为参数传递来限制输出,例如: 127.0.0.1:6379> INFO Server Server redis_version:4.0.10 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:fdf31b4ab3504500 redis_mode:standalone os:Linux 4.18.0-32.el8.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:8.2.1 process_id:30500 run_id:d8c5ba56a0735a6831a0b3467c3efa95ac174cdd tcp_port:6379 uptime_in_seconds:222 uptime_in_days:0 hz:10 lru_clock:2563866 executable:/usr/bin/redis-server config_file:/etc/redis.conf
执行Redis基准测试 使用10个并行连接运行基准测试,总计100k请求,针对本地redis测试其性能: # redis-benchmark -h 127.0.0.1 -p 6379 -n 100000 -c 10 ........................... 100.00% <= 0 milliseconds 85470.09 requests per second ====== LRANGE_500 (first 450 elements) ====== 100000 requests completed in 1.17 seconds 10 parallel clients 3 bytes payload keep alive: 1 100.00% <= 0 milliseconds 85397.09 requests per second ====== LRANGE_600 (first 600 elements) ====== 100000 requests completed in 1.18 seconds 10 parallel clients 3 bytes payload keep alive: 1 100.00% <= 0 milliseconds 84530.86 requests per second ====== MSET (10 keys) ====== 100000 requests completed in 1.18 seconds 10 parallel clients 3 bytes payload keep alive: 1 100.00% <= 0 milliseconds 84961.77 requests per second 有关更多选项和示例,请使用: $ redis-benchmark --help 要显示已连接的客户端,请使用: 127.0.0.1:6379> client list id=185 addr=127.0.0.1:54300 fd=8 name= age=75 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
使用Redis和Python 要在Python中使用redis,请安装Python Redis客户端库: sudo yum -y install python-redis
使用Redis和PHP 要使用PHP连接到Redis服务器,请安装PHP Redis客户端模块: sudo yum -y install php-pecl-redis
相关主题 |