Redis 的编译安装及多种方式启动

厌伴老儒烹瓠叶,强随举子踏槐花。这篇文章主要讲述Redis 的编译安装及多种方式启动相关的知识,希望能为你提供帮助。
1.源码包准备官网下载最新的redis 源码包 :http://download.redis.io/releases/
官方网站安装方法:https://redis.io/download

【Redis 的编译安装及多种方式启动】

2.编译安装

#### 编译安装redis
[root@CentOS84-IP08 ]#hostnamectl set-hostname Redis-IP08
[root@CentOS84-IP08 ]#exit
[root@Redis-IP08 ]#

# 安装依赖包
[root@Redis-IP08 ]#yum -y install gcc jemalloc-devel
# 支持 systemd 需要安装的包
[root@Redis-IP08 ]#yum -y install gcc jemalloc-devel systemd-devel

[root@Redis-IP08 ]#cd /data/
[root@Redis-IP08 ]#wget http://download.redis.io/releases/redis-6.2.6.tar.gz

[root@Redis-IP08 ]#ll
total 2420
-rw-r--r-- 1 root root 2476542 Oct42021 redis-6.2.6.tar.gz
[root@Redis-IP08 ]#tar xvf redis-6.2.6.tar.gz

[root@Redis-IP08 ]#ll
total 2424
drwxrwxr-x 7 root root4096 Oct42021 redis-6.2.6
-rw-r--r-- 1 root root 2476542 Oct42021 redis-6.2.6.tar.gz

# 编译安装
[root@Redis-IP08 ]#cd redis-6.2.6/
# 指定redis安装目录
[root@Redis-IP08 ]#make PREFIX=/apps/redis install
# 如果支持systemd,需要执行下面
[root@Redis-IP08 ]#make USE_SYSTEMD=yes PREFIX=/apps/redis install

# 配置环境变量
[root@Redis-IP08 ]#echo PATH=/apps/redis/bin:$PATH > /etc/profile.d/redis.sh
[root@Redis-IP08 ]#. /etc/profile.d/redis.sh
[root@Redis-IP08 ]#tree /apps/redis/
/apps/redis/
└── bin
├── redis-benchmark
├── redis-check-aof -> redis-server
├── redis-check-rdb -> redis-server
├── redis-cli
├── redis-sentinel -> redis-server
└── redis-server

1 directory, 6 files
[root@Redis-IP08 ]#

# 准备相关目录和配置文件; 创建配置文件、日志、数据等目录
[root@Redis-IP08 ]#mkdir /apps/redis/etc,log,data,run
[root@Redis-IP08 ]#

[root@Redis-IP08 ]#find / -name redis.conf
/data/redis-6.2.6/redis.conf

[root@Redis-IP08 ]#
[root@Redis-IP08 ]#cp /data/redis-6.2.6/redis.conf /apps/redis/etc/
[root@Redis-IP08 ]#

# redis-server 是 redis 服务器程序
[root@Redis-IP08 ]#
[root@Redis-IP08 ]#
[root@Redis-IP08 ]#redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options] [-]
./redis-server - (read config from stdin)
./redis-server -v or --version
./redis-server -h or --help
./redis-server --test-memory < megabytes>

Examples:
./redis-server (run the server with default conf)
./redis-server /etc/redis/6379.conf
./redis-server --port 7777
./redis-server --port 7777 --replicaof 127.0.0.1 8888
./redis-server /etc/myredis.conf --loglevel verbose -
./redis-server /etc/myredis.conf --loglevel verbose

Sentinel mode:
./redis-server /etc/sentinel.conf --sentinel
[root@Redis-IP08 ]#
[root@Redis-IP08 ]#

3.多种方式启动Redis3.1前台启动
# 前台启动 redis,这种启动方式会看到一些“警告”信息,都可以通过修改配置文件等方式解决掉。
[root@Redis-IP08 ]#redis-server /apps/redis/etc/redis.conf
43932:C 26 Apr 2022 15:23:57.299 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
43932:C 26 Apr 2022 15:23:57.299 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=43932, just started
43932:C 26 Apr 2022 15:23:57.299 # Configuration loaded
43932:M 26 Apr 2022 15:23:57.300 * monotonic clock: POSIX clock_gettime
_._
_.-``__ -._
_.-```.`_.-._Redis 6.2.6 (00000000/0) 64 bit
(,.-`| `,)Running in standalone mode
|`-._`-...-` __...-.``-._|` _.-|Port: 6379
|`-._`._/_.-|PID: 43932
`-._`-._`-./_.-_.-
|`-._`-._`-.__.-_.-_.-|
|`-._`-.__.-_.-|https://redis.io
`-._`-._`-.__.-_.-_.-
|`-._`-._`-.__.-_.-_.-|
|`-._`-.__.-_.-|
`-._`-._`-.__.-_.-_.-
`-._`-.__.-_.-
`-.__.-
`-.__.-

43932:M 26 Apr 2022 15:23:57.302 # Server initialized
43932:M 26 Apr 2022 15:23:57.302 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add vm.overcommit_memory = 1 to /etc/sysctl.conf and then reboot or run the command sysctl vm.overcommit_memory=1 for this to take effect.
43932:M 26 Apr 2022 15:23:57.302 * Ready to accept connections

^C43932:signal-handler (1650957970) Received SIGINT scheduling shutdown...
43932:M 26 Apr 2022 15:26:11.028 # User requested shutdown...
43932:M 26 Apr 2022 15:26:11.028 * Saving the final RDB snapshot before exiting.
43932:M 26 Apr 2022 15:26:11.030 * DB saved on disk
43932:M 26 Apr 2022 15:26:11.030 * Removing the pid file.
43932:M 26 Apr 2022 15:26:11.030 # Redis is now ready to exit, bye bye...
[root@Redis-IP08 ]#

# 再开另外一个窗口可以看到 6379 端口启用监听
[root@Redis-IP08 ]#ss -tln
StateRecv-QSend-QLocal Address:PortPeer Address:PortProcess
LISTEN0511[::1]:6379[::]:*

[root@Redis-IP08 ]#

#### 前台运行redis 开启多实例再打开一个窗口运行
[root@Redis-IP08 ]#redis-server --port 6380

## 在监视终端窗口看到有6379和6380两个redis 的实列开启了
[root@Redis-IP08 ]#ss -tln
StateRecv-QSend-QLocal Address:PortPeer Address:PortProcess
LISTEN0511127.0.0.1:63790.0.0.0:*
LISTEN05110.0.0.0:63800.0.0.0:*

[root@Redis-IP08 ]#
[root@Redis-IP08 ]#ps -ef | grep redis
root4396735920 15:27 pts/000:00:00 redis-server 127.0.0.1:6379
root44051440210 15:29 pts/200:00:00 redis-server *:6380
root44078439730 15:31 pts/100:00:00 grep --color=auto redis

[root@Redis-IP08 ]#redis-cli -p 6380
127.0.0.1:6380>

3.2后台服务方式启动Redis
# 创建 redis 用户和数据目录
[root@Redis-IP08 ]#useradd -r -s /sbin/nologin redis
[root@Redis-IP08 ]#
# 设置目录权限
[root@Redis-IP08 ]#chown -R redis.redis /apps/redis/
[root@Redis-IP08 ]#

# 修改复制过来的 redis.service 文件,但是总觉得这个文件写得不完美,和yum安装出来的STOP后看到的信息不相同(failed),感觉很野蛮的关闭了redis 。有两个可行方法解决这个问题,一是模仿yum安装的启动文件;二是 用redis-cli shutdown 方式优雅关闭redis ,不过都需要自己去修改和定义相关文件。
[root@Redis-IP08 ]#vim /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
#ExecStop=/usr/libexec/redis-shutdown
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

[root@Redis-IP08 ]#
[root@Redis-IP08 ]#systemctl daemon-reload
[root@Redis-IP08 ]#systemctl start redis
[root@Redis-IP08 ]#systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2022-04-26 17:18:31 CST; 25s ago
Main PID: 45436 (redis-server)
Status: "Ready to accept connections"
Tasks: 5 (limit: 23544)
Memory: 8.8M
CGroup: /system.slice/redis.service
└─45436 /apps/redis/bin/redis-server 127.0.0.1:6379

[root@Redis-IP08 ]#systemctl stop redis
[root@Redis-IP08 ]#systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
Active: failed (Result: core-dump) since Tue 2022-04-26 17:19:05 CST; 2s ago
Process: 45481 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
Process: 45436 ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd (code=dumped, >
Main PID: 45436 (code=dumped, signal=QUIT)
Status: "Ready to accept connections"

[root@Redis-IP08 ]#


[root@Redis-IP08 ]#ss -tln
StateRecv-QSend-QLocal Address:PortPeer Address:PortProcess
LISTEN0511127.0.0.1:63790.0.0.0:*

[root@Redis-IP08 ]#redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> info
# Server
redis_version:6.2.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:ad16cde570ca22fd
redis_mode:standalone
os:Linux 4.18.0-305.3.1.el8.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:8.5.0
process_id:45508
process_supervised:systemd
run_id:01c21dbd48885b8d9830c2595f52ef687b2c62bc
tcp_port:6379
server_time_usec:1650964880323752
uptime_in_seconds:49
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:6797712
executable:/apps/redis/bin/redis-server
config_file:/apps/redis/etc/redis.conf
io_threads_active:0

# Clients
connected_clients:1
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:16
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

# Memory
used_memory:873760
used_memory_human:853.28K
used_memory_rss:13819904
used_memory_rss_human:13.18M
used_memory_peak:931856
used_memory_peak_human:910.02K
used_memory_peak_perc:93.77%
used_memory_overhead:830496
used_memory_startup:810000
used_memory_dataset:43264
used_memory_dataset_perc:67.85%
allocator_allocated:1035512
allocator_active:1355776
allocator_resident:4169728
total_system_memory:3917111296
total_system_memory_human:3.65G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.31
allocator_frag_bytes:320264
allocator_rss_ratio:3.08
allocator_rss_bytes:2813952
rss_overhead_ratio:3.31
rss_overhead_bytes:9650176
mem_fragmentation_ratio:16.63
mem_fragmentation_bytes:12988912
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:20496
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0

# Persistence
loading:0
current_cow_size:0
current_cow_size_age:0
current_fork_perc:0.00
current_save_keys_processed:0
current_save_keys_total:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1650964831
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
module_fork_in_progress:0
module_fork_last_cow_size:0

# Stats
total_connections_received:1
total_commands_processed:2
instantaneous_ops_per_sec:0
total_net_input_bytes:77
total_net_output_bytes:20449
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
total_forks:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:2
dump_payload_sanitizations:0
total_reads_processed:5
total_writes_processed:4
io_threaded_reads_processed:0
io_threaded_writes_processed:0

# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:3ccd3cd50e07eec33d9ba3e11f232bc05b9e8018
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.037935
used_cpu_user:0.040818
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
used_cpu_sys_main_thread:0.037333
used_cpu_user_main_thread:0.040589

# Modules

# Errorstats
errorstat_ERR:count=2

# Cluster
cluster_enabled:0

# Keyspace
127.0.0.1:6379> exit
[root@Redis-IP08 ]#

# 创建命令软连接
[root@Redis-IP08 ]#ln -sv /apps/redis/bin/redis-* /usr/bin/
/usr/bin/redis-benchmark -> /apps/redis/bin/redis-benchmark
/usr/bin/redis-check-aof -> /apps/redis/bin/redis-check-aof
/usr/bin/redis-check-rdb -> /apps/redis/bin/redis-check-rdb
/usr/bin/redis-cli -> /apps/redis/bin/redis-cli
/usr/bin/redis-sentinel -> /apps/redis/bin/redis-sentinel
/usr/bin/redis-server -> /apps/redis/bin/redis-server

# 编译安装后的命令
[root@Redis-IP08 ]#ll /apps/redis/bin/
total 24876
-rwxr-xr-x 1 redis redis6549392 Apr 26 15:12 redis-benchmark
lrwxrwxrwx 1 redis redis12 Apr 26 15:12 redis-check-aof -> redis-server
lrwxrwxrwx 1 redis redis12 Apr 26 15:12 redis-check-rdb -> redis-server
-rwxr-xr-x 1 redis redis6765904 Apr 26 15:12 redis-cli
lrwxrwxrwx 1 redis redis12 Apr 26 15:12 redis-sentinel -> redis-server
-rwxr-xr-x 1 redis redis 12154016 Apr 26 15:12 redis-server
[root@Redis-IP08 ]#


    推荐阅读