Redis 是一种内存中数据结构存储,用作分布式内存中键值数据库、缓存和消息代理,具有可选的持久性。Redis 支持各种抽象数据结构,如字符串、列表、映射、集合、排序集、HyperLogLogs、位图、流、空间索引等。
在本教程中,我们将学习如何在 FreeBSD 13 上安装 Redis 6。
#先决条件
要继续操作,请确保您具有:
更新的 FreeBSD 13 服务器
访问互联网
对服务器或具有 sudo 访问权限的用户的 root 访问权限
#更新 FreeBSD 13 服务器
在继续操作之前,请确保使用以下命令更新服务器(作为 root 用户):
pkg update
pkg upgrade
我们还确保使用此命令安装 vim,因为我们稍后会使用它:
pkg install -y vim
#安装 redis
Redis 在默认的 FreeBSD 仓库中可用。要安装它,请使用以下命令:
pkg install -y redis
这是我服务器上的输出
# pkg install -y redis
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 1 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
redis: 6.2.6
Number of packages to be installed: 1
The process will require 6 MiB more space.
729 KiB to be downloaded.
[1/1] Fetching redis-6.2.6.pkg: 100% 729 KiB 746.3kB/s 00:01
Checking integrity... done (0 conflicting)
[1/1] Installing redis-6.2.6...
===> Creating groups.
Creating group 'redis' with gid '535'.
===> Creating users
Creating user 'redis' with uid '535'.
[1/1] Extracting redis-6.2.6: 100%
=====
Message from redis-6.2.6:
--
To setup "redis" you need to edit the configuration file:
/usr/local/etc/redis.conf
To run redis from startup, add redis_enable="YES"
in your /etc/rc.conf.
Use this command to confirm the redis package installed:
# pkg info redis
redis-6.2.6
Name : redis
Version : 6.2.6
Installed on : Fri Nov 19 10:01:00 2021 UTC
Origin : databases/redis
Architecture : FreeBSD:13:amd64
Prefix : /usr/local
Categories : databases
Licenses : BSD3CLAUSE
Maintainer : osa@FreeBSD.org
WWW : https://redis.io/
Comment : Persistent key-value database with built-in net interface
Options :
JEMALLOC : off
LUA : off
LUAJIT : off
LUAJITOR : off
TESTS : off
TLS : off
TRIB : off
Annotations :
FreeBSD_version: 1300139
repo_type : binary
repository : FreeBSD
Flat size : 6.10MiB
Description :
Redis is an open source, 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.
WWW: https://redis.io/
Now that the service has been installed, let’s start it with this command:
service redis onestart
Starting redis.
Enable the service so it starts on boot:
# sysrc redis_enable="YES"
redis_enable: -> YES
After the service starts, use this command to check the status of the service:
# service redis status
redis is running as pid 33056.
The above indicates that the service has been started successfully.
#Configuring Redis
The redis configuration file is located in this path . In this section, we are going to update the redis configuration file to allow remote access, to set an authentication password, to add a pid file and to Set Persistent Store for Recovery./usr/local/etc/redis.conf
Edit redis config file using this:
vim /usr/local/etc/redis.conf
To allow remote access to the redis instance, bind redis to using this line:0.0.0.0
bind * -::*
要在 redis 中设置密码,请使用以下命令:
requirepass j2GfJuLFR8
要向 redis 添加 pid 文件:
pidfile /var/run/redis/redis-server.pid
通过将 appendonlyvalue 更改为 yes 来设置用于恢复的持久存储
appendonly yes
appendfilename "appendonly.aof"
重新启动 redis 服务以应用更改:
# service redis restart
Stopping redis.
Waiting for PIDS: 33056.
Starting redis.
#在 Redis 中连接和执行基本操作
在本地连接到 redis:
redis-cli
要进行身份验证,请执行以下操作:
# redis-cli
127.0.0.1:6379> auth j2GfJuLFR8
OK
127.0.0.1:6379>
您应该在输出中收到。如果输入了错误的密码,身份验证将失败。OK
检查redis信息。
127.0.0.1:6379> INFO
这将输出一长串数据。您可以通过将 Section 作为参数传递来限制输出。例如
127.0.0.1:6379> INFO Server
# Server
redis_version:6.2.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:74f6bda298c45b9e
redis_mode:standalone
os:FreeBSD 13.0-RELEASE-p4 amd64
arch_bits:64
multiplexing_api:kqueue
atomicvar_api:atomic-builtin
gcc_version:4.2.1
process_id:33134
process_supervised:no
run_id:7363d70d4e75fdaf28e8738a987f4b2ef2da6d8d
tcp_port:6379
server_time_usec:1637316624510229
uptime_in_seconds:56
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:9926672
executable:/usr/local/bin/redis-server
config_file:/usr/local/etc/redis.conf
io_threads_active:0
#执行 Redis 基准测试
使用并行连接运行基准测试,针对本地 redis 的请求总数,以测试其性能。1510k
$ redis-benchmark -h 127.0.0.1 -p 6379 -n 10000 -c 15 -a j2GfJuLFR8
====== PING_INLINE ======
10000 requests completed in 0.23 seconds
15 parallel clients
3 bytes payload
keep alive: 1
host configuration "save": 3600 1 300 100 60 10000
host configuration "appendonly": no
multi-thread: no
Latency by percentile distribution:
0.000% <= 0.031 milliseconds (cumulative count 1)
50.000% <= 0.095 milliseconds (cumulative count 6342)
75.000% <= 0.103 milliseconds (cumulative count 7740)
..........
Cumulative distribution of latencies:
29.540% <= 0.103 milliseconds (cumulative count 2954)
99.810% <= 0.207 milliseconds (cumulative count 9981)
99.990% <= 0.303 milliseconds (cumulative count 9999)
100.000% <= 0.407 milliseconds (cumulative count 10000)
Summary:
throughput summary: 91743.12 requests per second
latency summary (msec):
avg min p50 p95 p99 max
0.121 0.032 0.127 0.167 0.183 0.319
有关更多选项和示例,请使用:
redis-benchmark --help
#结论
在本指南中, 我们设法在 FreeBSD 13 中安装和配置 Redis 6。