Redis部署企业级NOSQL数据库

使用仓库安装redis

查看仓库中的redis版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@xcjyc:~# apt update ; apt info redis
Package: redis
Version: 5:6.0.16-1ubuntu1
Priority: optional
Section: universe/database
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Chris Lamb <lamby@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 67.6 kB
Depends: redis-server (<< 5:6.0.16-1ubuntu1.1~), redis-server (>= 5:6.0.16-1ubuntu1)
Homepage: https://redis.io/
Download-Size: 2930 B
APT-Sources: https://mirrors.aliyun.com/ubuntu jammy/universe amd64 Packages
Description: Persistent key-value database with network interface (metapackage)
Redis is a key-value database in a similar vein to memcache but the dataset
is non-volatile. Redis additionally provides native support for atomically
manipulating and querying data structures such as lists and sets.
.
The dataset is stored entirely in memory and periodically flushed to disk.
.
This package installs the main redis-server package.

安装redis,redis默认使用6379端口

1
2
3
4
5
6
7
8
9
10
11
12
13
root@xcjyc:~# apt install -y redis 

root@xcjyc:~# ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 127.0.0.1:6379 0.0.0.0:* users:(("redis-server",pid=1086,fd=6))
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=611,fd=14))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=659,fd=3))
LISTEN 0 511 [::1]:6379 [::]:* users:(("redis-server",pid=1086,fd=7))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=659,fd=4))

root@xcjyc:~# redis-cli
127.0.0.1:6379> ping
PONG

编译安装redis最新稳定版

源码下载地址:https://download.redis.io/releases/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 下载解压源码包
root@ubuntu2204:~# wget https://download.redis.io/releases/redis-7.2.4.tar.gz
root@ubuntu2204:~# tar xvf redis-7.2.4.tar.gz

# 安装依赖包
root@ubuntu2204:~# apt update ; apt install -y gcc make libjemalloc-dev libsystemd-dev

# 检查环境安装状态
root@ubuntu2204:~# apt install -y gcc make libjemalloc-dev libsystemd-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
gcc is already the newest version (4:11.2.0-1ubuntu1).
make is already the newest version (4.3-4.1build1).
libjemalloc-dev is already the newest version (5.2.1-4ubuntu1).
libsystemd-dev is already the newest version (249.11-0ubuntu3.12).
0 upgraded, 0 newly installed, 0 to remove and 56 not upgraded.

# 准备安装目录
root@ubuntu2204:~# mkdir -p /apps/redis

# 查看安装说明
root@ubuntu2204:~# cd redis-7.2.4/
root@ubuntu2204:~/redis-7.2.4# ls
00-RELEASENOTES CONTRIBUTING.md MANIFESTO SECURITY.md redis.conf runtest-moduleapi src
BUGS COPYING Makefile TLS.md runtest runtest-sentinel tests
CODE_OF_CONDUCT.md INSTALL README.md deps runtest-cluster sentinel.conf utils

root@ubuntu2204:~/redis-7.2.4# cat README.md
-- 使用systemd,可在编译时添加参数 USE_SYSTEMD=yes (提应安装环境包)--
...
To build with systemd support, you'll need systemd development libraries (such
as libsystemd-dev on Debian/Ubuntu or systemd-devel on CentOS) and run:

% make USE_SYSTEMD=yes
...

# 编译安装
root@ubuntu2204:~/redis-7.2.4# make -j 2 PREFIX=/apps/redis USE_SYSTEMD=yes install
root@ubuntu2204:~/redis-7.2.4# 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@ubuntu2204:~/redis-7.2.4# echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh
root@ubuntu2204:~/redis-7.2.4# . /etc/profile.d/redis.sh

# 将redis执行程序创建链接放到PATH目录
root@ubuntu2204:~# ln -s /apps/redis/bin/redis* /usr/bin/
root@ubuntu2204:~# ls /usr/bin/redis*
/usr/bin/redis-benchmark /usr/bin/redis-check-rdb /usr/bin/redis-sentinel
/usr/bin/redis-check-aof /usr/bin/redis-cli /usr/bin/redis-server

# 或将redis执行程序加入到PATH
root@ubuntu2204:~/redis-7.2.4# echo $PATH
/apps/redis/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

# 准备redis相关目录和配置文件
root@ubuntu2204:~/redis-7.2.4# mkdir /apps/redis/{etc,log,data,run}
root@ubuntu2204:~/redis-7.2.4# cp redis.conf /apps/redis/etc/

root@ubuntu2204:~/redis-7.2.4# 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
├── data
├── etc
│   └── redis.conf
├── log
└── run

前台启动redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 查看启动帮助
root@ubuntu2204:~# 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>
./redis-server --check-system

Examples:
./redis-server (run the server with default conf)
echo 'maxmemory 128mb' | ./redis-server -
./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@ubuntu2204:~# redis-server /apps/redis/etc/redis.conf
8030:C 14 May 2024 07:54:18.370 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. 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.
8030:C 14 May 2024 07:54:18.370 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8030:C 14 May 2024 07:54:18.370 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=8030, just started
8030:C 14 May 2024 07:54:18.370 * Configuration loaded
8030:M 14 May 2024 07:54:18.371 * Increased maximum number of open files to 10032 (it was originally set to 1024).
8030:M 14 May 2024 07:54:18.371 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 7.2.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8030
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

8030:M 14 May 2024 07:54:18.373 * Server initialized
8030:M 14 May 2024 07:54:18.373 * Ready to accept connections tcp

# 消除启动警告信息,添加内核参数
WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. 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.

root@ubuntu2204:~# echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
root@ubuntu2204:~# sysctl -p
vm.overcommit_memory = 1

# 重新测试启动(警告信息消除)
root@ubuntu2204:~# redis-server /apps/redis/etc/redis.conf
1024:C 15 May 2024 06:11:01.266 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1024:C 15 May 2024 06:11:01.266 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1024, just started
1024:C 15 May 2024 06:11:01.266 * Configuration loaded
1024:M 15 May 2024 06:11:01.266 * Increased maximum number of open files to 10032 (it was originally set to 1024).
1024:M 15 May 2024 06:11:01.266 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 7.2.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1024
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

1024:M 15 May 2024 06:11:01.269 * Server initialized
1024:M 15 May 2024 06:11:01.270 * Loading RDB produced by version 7.2.4
1024:M 15 May 2024 06:11:01.270 * RDB age 77247 seconds
1024:M 15 May 2024 06:11:01.270 * RDB memory usage when created 0.83 Mb
1024:M 15 May 2024 06:11:01.270 * Done loading RDB, keys loaded: 0, keys expired: 0.
1024:M 15 May 2024 06:11:01.270 * DB loaded from disk: 0.001 seconds
1024:M 15 May 2024 06:11:01.270 * Ready to accept connections tcp

# 新开一个窗口,检查端口状态(redis默认使用6379端口)使用redis-cli测试
root@ubuntu2204:~# ss -ntpl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=809,fd=3))
LISTEN 0 511 127.0.0.1:6379 0.0.0.0:* users:(("redis-server",pid=1024,fd=6))
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=761,fd=14))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=809,fd=4))
LISTEN 0 511 [::1]:6379 [::]:* users:(("redis-server",pid=1024,fd=7))

root@ubuntu2204:~# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

创建redis服务启动账户并授权

1
2
3
4
5
6
7
8
9
10
11
12
root@ubuntu2204:~# useradd -r -s /sbin/nologin redis
root@ubuntu2204:~# chown -R redis:redis /apps/redis/

root@ubuntu2204:~# ll /apps/redis/
total 28
drwxr-xr-x 7 redis redis 4096 May 14 07:49 ./
drwxr-xr-x 3 root root 4096 May 14 07:10 ../
drwxr-xr-x 2 redis redis 4096 May 14 07:26 bin/
drwxr-xr-x 2 redis redis 4096 May 14 07:49 data/
drwxr-xr-x 2 redis redis 4096 May 14 07:49 etc/
drwxr-xr-x 2 redis redis 4096 May 14 07:49 log/
drwxr-xr-x 2 redis redis 4096 May 14 07:49 run/

创建 systemd service 文件,可以参考编译包中的service模板,也可参考yum安装生成的systemd service文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
root@ubuntu2204:~# ls ./redis-7.2.4/utils/systemd-redis_server.service 
./redis-7.2.4/utils/systemd-redis_server.service

# 修改好的redis.service文件内容
root@ubuntu2204:~# vim /etc/systemd/system/redis.service
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
LimitNOFILE=1000000
NoNewPrivileges=yes
Type=notify
TimeoutStartSec=infinity
TimeoutStopSec=infinity
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

# 利用cat创建文件
cat > /etc/systemd/system/redis.service <<EOF
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
LimitNOFILE=1000000
NoNewPrivileges=yes
Type=notify
TimeoutStartSec=infinity
TimeoutStopSec=infinity
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF

修改redis配置文件,设置服务IP,PID文件目录,连接密码

1
2
3
4
5
6
# 修改配置文件(先更改如下三项,其它配置参考文档修改)
root@ubuntu2204:~# vim /apps/redis/etc/redis.conf
bind 0.0.0.0 # 监听端口
pidfile /apps/redis/run/redis_6379.pid # PID文件,由于使用redis用户启动服务,需更改到有权限的目录
requirepass "12345678" # 客户端连接密码(若有特殊字符使用引号)
maxmemory

测试systemd启动服务配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
root@ubuntu2204:~# systemctl daemon-reload
root@ubuntu2204:~# systemctl enable --now redis.service
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /etc/systemd/system/redis.service.

root@ubuntu2204:~# systemctl status redis.service
● redis.service - Redis data structure server
Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2024-05-15 07:21:59 UTC; 25s ago
Docs: https://redis.io/documentation
Main PID: 23950 (redis-server)
Status: "Ready to accept connections"
Tasks: 5 (limit: 4515)
Memory: 2.1M
CPU: 97ms
CGroup: /system.slice/redis.service
└─23950 "/apps/redis/bin/redis-server 0.0.0.0:6379" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
"" "" "" "" "" "" "" "" "" "" "" "" ""

May 15 07:21:59 ubuntu2204 systemd[1]: Starting Redis data structure server...
May 15 07:21:59 ubuntu2204 redis-server[23950]: 23950:C 15 May 2024 07:21:59.143 * Supervised by systemd. Please make sure you set
appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.
May 15 07:21:59 ubuntu2204 redis-server[23950]: 23950:C 15 May 2024 07:21:59.143 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
May 15 07:21:59 ubuntu2204 redis-server[23950]: 23950:C 15 May 2024 07:21:59.143 * Redis version=7.2.4, bits=64, commit=00000000,
modified=0, pid=23950, just started
May 15 07:21:59 ubuntu2204 redis-server[23950]: 23950:C 15 May 2024 07:21:59.143 * Configuration loaded
May 15 07:21:59 ubuntu2204 redis-server[23950]: 23950:M 15 May 2024 07:21:59.144 * monotonic clock: POSIX clock_gettime
May 15 07:21:59 ubuntu2204 redis-server[23950]: 23950:M 15 May 2024 07:21:59.145 * Running mode=standalone, port=6379.
May 15 07:21:59 ubuntu2204 redis-server[23950]: 23950:M 15 May 2024 07:21:59.146 * Server initialized
May 15 07:21:59 ubuntu2204 redis-server[23950]: 23950:M 15 May 2024 07:21:59.146 * Ready to accept connections tcp
May 15 07:21:59 ubuntu2204 systemd[1]: Started Redis data structure server.

验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
root@ubuntu2204:~# ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:6379 0.0.0.0:* users:(("redis-server",pid=23950,fd=6))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=809,fd=3))
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=761,fd=14))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=809,fd=4))

# 使用redis-cli测试
# 格式:redis-cli -h <redis服务器> -p <PORT> -a <PASSWORD>

root@ubuntu2204:~# redis-cli -h 10.0.0.7 -p 6379 -a 12345678
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.7:6379> ping
PONG
10.0.0.7:6379> info
# Server
redis_version:7.2.4
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:cfa841a5bc8661c5
redis_mode:standalone
os:Linux 5.15.0-102-generic x86_64
arch_bits:64
monotonic_clock:POSIX clock_gettime
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:11.4.0
process_id:23986
process_supervised:systemd
run_id:a1f26808d76b9e2b984e46979c55d9af2bd8a161
tcp_port:6379
server_time_usec:1715758640316473
uptime_in_seconds:132
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:4482608
executable:/apps/redis/bin/redis-server
config_file:/apps/redis/etc/redis.conf
io_threads_active:0
listener0:name=tcp,bind=0.0.0.0,port=6379

# Clients
connected_clients:1
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:20480
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0
total_blocking_keys:0
total_blocking_keys_on_nokey:0

# Memory
used_memory:965424
used_memory_human:942.80K
used_memory_rss:7987200
used_memory_rss_human:7.62M
used_memory_peak:1158792
used_memory_peak_human:1.11M
used_memory_peak_perc:83.31%
used_memory_overhead:888576
used_memory_startup:865992
used_memory_dataset:76848
used_memory_dataset_perc:77.29%
allocator_allocated:1811968
allocator_active:1966080
allocator_resident:4943872
total_system_memory:4064628736
total_system_memory_human:3.79G
used_memory_lua:31744
used_memory_vm_eval:31744
used_memory_lua_human:31.00K
used_memory_scripts_eval:0
number_of_cached_scripts:0
number_of_functions:0
number_of_libraries:0
used_memory_vm_functions:32768
used_memory_vm_total:64512
used_memory_vm_total_human:63.00K
used_memory_functions:184
used_memory_scripts:184
used_memory_scripts_human:184B
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.09
allocator_frag_bytes:154112
allocator_rss_ratio:2.51
allocator_rss_bytes:2977792
rss_overhead_ratio:1.62
rss_overhead_bytes:3043328
mem_fragmentation_ratio:8.29
mem_fragmentation_bytes:7024192
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_total_replication_buffers:0
mem_clients_slaves:0
mem_clients_normal:22400
mem_cluster_links:0
mem_aof_buffer:0
mem_allocator:jemalloc-5.3.0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0

# Persistence
loading:0
async_loading:0
current_cow_peak: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:1715758508
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_saves:0
rdb_last_cow_size:0
rdb_last_load_keys_expired:0
rdb_last_load_keys_loaded: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_rewrites:0
aof_rewrites_consecutive_failures:0
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:3
instantaneous_ops_per_sec:0
total_net_input_bytes:83
total_net_output_bytes:205217
total_net_repl_input_bytes:0
total_net_repl_output_bytes:0
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
instantaneous_input_repl_kbps:0.00
instantaneous_output_repl_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:4
evicted_keys:0
evicted_clients:0
total_eviction_exceeded_time:0
current_eviction_exceeded_time:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
pubsubshard_channels: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
total_active_defrag_time:0
current_active_defrag_time:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:0
dump_payload_sanitizations:0
total_reads_processed:4
total_writes_processed:5
io_threaded_reads_processed:0
io_threaded_writes_processed:0
reply_buffer_shrinks:1
reply_buffer_expands:0
eventloop_cycles:1315
eventloop_duration_sum:313813
eventloop_duration_cmd_sum:1688
instantaneous_eventloop_cycles_per_sec:9
instantaneous_eventloop_duration_usec:268
acl_access_denied_auth:0
acl_access_denied_cmd:0
acl_access_denied_key:0
acl_access_denied_channel:0

# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:cbcfc8bbb3ca49ffe80f7d402ae7d55fb280400d
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.102829
used_cpu_user:0.331917
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
used_cpu_sys_main_thread:0.090418
used_cpu_user_main_thread:0.343591

# Modules

# Errorstats

# Cluster
cluster_enabled:0

# Keyspace
10.0.0.7:6379>

编译安装脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash

REDIS_VERSION=redis-7.2.4
PASSWORD=12345678
INSTALL_DIR=/apps/redis

CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'`

. /etc/os-release

color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}


prepare(){
if [ $ID = "centos" -o $ID = "rocky" ];then
yum -y install gcc make jemalloc-devel systemd-devel wget
else
apt update
apt -y install gcc make libjemalloc-dev libsystemd-dev wget
fi
if [ $? -eq 0 ];then
color "安装软件包成功" 0
else
color "安装软件包失败,请检查网络配置" 1
exit
fi
}
install() {
if [ ! -f ${REDIS_VERSION}.tar.gz ];then
wget http://download.redis.io/releases/${REDIS_VERSION}.tar.gz || { color "Redis 源码下载失败" 1 ; exit; }
fi
tar xf ${REDIS_VERSION}.tar.gz -C /usr/local/src
cd /usr/local/src/${REDIS_VERSION}
make -j $CUPS USE_SYSTEMD=yes PREFIX=${INSTALL_DIR} USE_SYSTEMD=yes install && color "Redis 编译安装完成" 0 || { color "Redis 编译安装失败" 1 ;exit ; }

ln -s ${INSTALL_DIR}/bin/redis-* /usr/bin/

mkdir -p ${INSTALL_DIR}/{etc,log,data,run}

cp redis.conf ${INSTALL_DIR}/etc/

sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/# requirepass/a requirepass $PASSWORD" -e "/^dir .*/c dir ${INSTALL_DIR}/data/" -e "/logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log" -e "/^pidfile .*/c pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf


if id redis &> /dev/null ;then
color "Redis 用户已存在" 1
else
useradd -r -s /sbin/nologin redis
color "Redis 用户创建成功" 0
fi

chown -R redis:redis ${INSTALL_DIR}

cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
sysctl -p
if [ $ID = "centos" -o $ID = "rocky" ];then
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
/etc/rc.d/rc.local
else
echo -e '#!/bin/bash\necho never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
chmod +x /etc/rc.local
/etc/rc.local
fi


cat > /lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis data structure server
Documentation=https://redis.io/documentation
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
LimitNOFILE=1000000
NoNewPrivileges=yes
Type=notify
TimeoutStartSec=infinity
TimeoutStopSec=infinity
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

EOF
systemctl daemon-reload
systemctl enable --now redis &> /dev/null
if [ $? -eq 0 ];then
color "Redis 服务启动成功,Redis信息如下:" 0
else
color "Redis 启动失败" 1
exit
fi
sleep 2
redis-cli -a $PASSWORD INFO Server 2> /dev/null
}

prepare
install

Redis部署企业级NOSQL数据库
https://www.xcjyc.top/2024/05/10/Redis部署企业级NOSQL数据库/
作者
XCJYC
发布于
2024年5月10日
许可协议