如何在CentOS 7上安装最新的Redis

时间:2020-02-23 14:30:46  来源:igfitidea点击:

在本教程中,我们将逐步介绍在CentOS 7上安装Redis的方法。Redis是一个开源的内存中数据结构存储。 Redis可用作数据库服务器,消息代理或者将数据缓存在内存中以加快检索速度。 Redis支持的数据结构包括:具有范围查询的哈希表字符串排序的列表超级日志日志位图地理空间索引等

如何在CentOS 7上安装Redis

在本节中,我们将研究如何在CentOS 7上安装最新版本的Redis。

更新CentOS 7

在安装Redis之前,请确保系统已更新

sudo yum -y update

添加REMI副本

Remi存储库上提供了最新版本的Redis,通过执行以下命令将其添加:

sudo yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

在CentOS 7上安装Redis

添加存储库后,从存储库在CentOS 7上安装最新的Redis。

$sudo yum --enablerepo=remi install redis
 Loaded plugins: fastestmirror, ovl
 Loading mirror speeds from cached hostfile
 base: centos.mirrors.proxad.net
 epel: mirrors.ircam.fr
 extras: centos.crazyfrogs.org
 remi: remi.mirror.ate.info
 remi-php72: remi.mirror.ate.info
 remi-safe: remi.mirror.ate.info
 updates: centos.quelquesmots.fr
 rabbitmq_rabbitmq-server/x86_64/signature                                                                                       |  836 B  00:00:00     
 rabbitmq_rabbitmq-server/x86_64/signature                                                                                       | 1.0 kB  00:00:00 !!! 
 rabbitmq_rabbitmq-server-source/signature                                                                                       |  836 B  00:00:00     
 rabbitmq_rabbitmq-server-source/signature                                                                                       | 1.0 kB  00:00:00 !!! 
 Resolving Dependencies
 --> Running transaction check
 ---> Package redis.x86_64 0:5.0.3-1.el7.remi will be installed
 --> Finished Dependency Resolution 
 Dependencies Resolved
 =======================================================================================================================================================
  Package                          Arch                              Version                                      Repository                       Size
 Installing:
  redis                            x86_64                            5.0.3-1.el7.remi                             remi                            919 k
 Transaction Summary
 Install  1 Package
 Total download size: 919 k
 Installed size: 3.0 M
 Is this ok [y/d/N]: y
 Downloading packages:
 redis-5.0.3-1.el7.remi.x86_64.rpm                                                                                               | 919 kB  00:00:00     
 Running transaction check
 Running transaction test
 Transaction test succeeded
 Running transaction
   Installing : redis-5.0.3-1.el7.remi.x86_64                                                                                                       1/1 
   Verifying  : redis-5.0.3-1.el7.remi.x86_64                                                                                                       1/1 
 Installed:
   redis.x86_64 0:5.0.3-1.el7.remi                                                                                                                      
 Complete!

从以下位置获取更多包装详细信息:

$rpm -qi redis 
 Name        : redis
 Version     : 5.0.3
 Release     : 1.el7.remi
 Architecture: x86_64
 Install Date: Thu 10 Jan 2019 05:02:04 PM EAT
 Group       : Applications/Databases
 Size        : 3155665
 License     : BSD
 Signature   : DSA/SHA1, Wed 12 Dec 2016 04:44:08 PM EAT, Key ID 004e6f4700f97f56
 Source RPM  : redis-5.0.3-1.el7.remi.src.rpm
 Build Date  : Wed 12 Dec 2016 04:42:35 PM EAT
 Build Host  : builder.remirepo.net
 Relocations : (not relocatable)
 Packager    : https://blog.remirepo.net/
 Vendor      : Remi Collet
 URL         : http://redis.io
 Bug URL     : https://forum.remirepo.net/
 Summary     : A persistent key-value database
 Description :
 Redis is an advanced key-value store. It is often referred to as a data
 structure server since keys can contain strings, hashes, lists, sets and
 sorted sets.
 You can run atomic operations on these types, like appending to a string;
 incrementing the value in a hash; pushing to a list; computing set
 intersection, union and difference; or getting the member with highest
 ranking in a sorted set.
 In order to achieve its outstanding performance, Redis works with an
 in-memory dataset. Depending on your use case, you can persist it either
 by dumping the dataset to disk every once in a while, or by appending
 each command to a log.
 Redis also supports trivial-to-setup master-slave replication, with very
 fast non-blocking first synchronization, auto-reconnection on net split
 and so forth.
 Other features include Transactions, Pub/Sub, Lua scripting, Keys with a
 limited time-to-live, and configuration settings to make Redis behave like
 a cache.
 You can use Redis from most programming languages also.

在CentOS 7上启动Redis服务

可以使用systemd服务管理器在CentOS 7上启动Redis服务。另外,启用该服务以在系统引导时启动。

sudo systemctl enable --now redis

启用网络侦听Redis服务(可选)

为了使网络客户端连接到Redis服务器,它需要该服务以侦听网络IP地址。

用我们喜欢的文本编辑器打开文件/etc/redis.conf

sudo vim /etc/redis.conf

然后将第61行绑定127.0.0.1更改为服务器IP:

bind 172.21.10.11

配置Redis身份验证(可选,但建议)

配置Redis身份验证以使客户端在处理任何其他命令之前要求AUTH <PASSWORD>。

requirepass  <AuthPassword>

例:

requirepass oobaiY8

设置永久存储进行恢复

通过更改appendonly值玩具来设置持久模式

appendonly yes
appendfilename "appendonly.aof"

进行更改后重新启动Redis服务

sudo systemctl restart redis

检查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 Tue 2016-12-04 15:23:08 UTC; 3s ago
  Process: 3625 ExecStop=/usr/libexec/redis-shutdown (code=exited, status=0/SUCCESS)
 Main PID: 3640 (redis-server)
   CGroup: /system.slice/redis.service
           └─3640 /usr/bin/redis-server 0.0.0.0:6379

Dec 04 15:23:08 cent-01 systemd[1]: Starting Redis persistent key-value database...
Dec 04 15:23:08 cent-01 systemd[1]: Started Redis persistent key-value database.

我们可以使用ss命令查看Redis服务使用的端口和IP:

$sudo ss -tunelp | grep 6379
tcp LISTEN 0 128 *:6379  *:*  users:(("redis-server",pid=28163,fd=4)) uid:995 ino:305

如果我们有活动的firewalld服务,则允许端口" 6379"

sudo firewall-cmd --add-port=6379/tcp --permanent
sudo firewall-cmd --reload

测试与Redis服务器的连接

确认我们可以在本地连接到Redis:

$redis-cli
127.0.0.1:6379>

测试验证:

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:3.2.12
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7897e7d0e13773f
redis_mode:standalone
os:Linux 3.10.0-862.14.4.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:3640
run_id:ef36ca5ae9d561d8d3d3ea979cc8481eab0da874
tcp_port:6379
uptime_in_seconds:145
uptime_in_days:0
hz:10
lru_clock:433261
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

执行Redis基准测试

针对本地redis,使用" 15"个并行连接运行基准测试,总共有10k个请求,以测试其性能。

$redis-benchmark -h 127.0.0.1 -p 6379 -n 10000 -c 15

# Sample output
................................................
====== LRANGE_600 (first 600 elements) ======
  10000 requests completed in 0.15 seconds
  15 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
67114.09 requests per second

====== MSET (10 keys) ======
  10000 requests completed in 0.15 seconds
  15 parallel clients
  3 bytes payload
  keep alive: 1

100.00% <= 0 milliseconds
66666.66 requests per second

有关更多选项和示例,请使用:

$redis-benchmark --help

我们已经在CentOS 7上成功安装了Redis。