Skip to content

Commit 72090c4

Browse files
author
pptfz
committed
更新redis笔记
1 parent b25ca02 commit 72090c4

1 file changed

Lines changed: 102 additions & 42 deletions

File tree

docs/数据库/redis/redis安装.md

Lines changed: 102 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,97 @@
1010

1111

1212

13-
## 1.下载redis
1413

15-
```python
16-
wget http://download.redis.io/releases/redis-5.0.7.tar.gz
14+
15+
## docker安装
16+
17+
[redis docker hub](https://hub.docker.com/_/redis)
18+
19+
20+
21+
import Tabs from '@theme/Tabs';
22+
import TabItem from '@theme/TabItem';
23+
24+
<Tabs>
25+
<TabItem value="docker" label="docker" default>
26+
27+
```shell
28+
docker run -d \
29+
--name redis \
30+
-p 6379:6379 \
31+
-v /data/docker-volume/redis/data:/data \
32+
-v /data/docker-volume/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
33+
redis:8 \
34+
redis-server /usr/local/etc/redis/redis.conf
1735
```
1836

37+
</TabItem>
38+
<TabItem value="compose" label="compose">
1939

40+
```shell
41+
cat > docker-compose.yml << EOF
42+
services:
43+
redis:
44+
image: redis:8
45+
container_name: redis
46+
restart: always
47+
ports:
48+
- "6379:6379"
49+
volumes:
50+
- ./data:/data
51+
- ./redis.conf:/usr/local/etc/redis/redis.conf
52+
command: >
53+
redis-server /usr/local/etc/redis/redis.conf
54+
EOF
55+
```
2056

21-
## 2.编译安装redis
57+
</TabItem>
58+
</Tabs>
2259

23-
### 2.1 解压缩包
60+
61+
62+
63+
64+
## 源码安装
65+
66+
### 下载源码包
2467

2568
```shell
26-
tar xf redis-5.0.7.tar.gz -C /usr/local
69+
export REDIS_VERSION=8.2.3
70+
wget http://download.redis.io/releases/redis-${REDIS_VERSION}.tar.gz
2771
```
2872

2973

3074

31-
### 2.2 编译安装
75+
### 解压缩包
3276

3377
```shell
34-
cd /usr/local/redis-5.0.7
35-
make
78+
tar xf redis-${REDIS_VERSION}.tar.gz -C /usr/local
3679
```
3780

3881

3982

40-
### 2.3 添加环境变量
83+
### 编译安装
4184

4285
```shell
43-
cat > /etc/profile.d/redis.sh << 'EOF'
44-
export PATH="/usr/local/redis-5.0.7/src:$PATH"
45-
EOF
46-
source /etc/profile
86+
cd /usr/local/redis-${REDIS_VERSION}
87+
make
4788
```
4889

4990

5091

92+
### 添加环境变量
5193

94+
```shell
95+
cat > /etc/profile.d/redis.sh << EOF
96+
export PATH="/usr/local/redis-${REDIS_VERSION}/src:$PATH"
97+
EOF
98+
source /etc/profile
99+
```
52100

53-
## 3.配置redis
54101

55-
### 3.1 创建相关目录
102+
103+
### 创建相关目录
56104

57105
```shell
58106
mkdir -p /etc/redis/6379
@@ -62,13 +110,17 @@ mkdir -p /var/run/redis/6379
62110

63111

64112

65-
### 3.2 创建redis配置文件
113+
### 创建redis配置文件
66114

67115
```shell
68-
cat >/etc/redis/6379/redis.conf << EOF
69-
# 守护进程模式启动
70-
daemonize yes
116+
cat > /etc/redis/6379/redis.conf << EOF
117+
# 是否守护进程模式启动
118+
daemonize no
119+
120+
# 使用 systemd 监管
121+
supervised systemd
71122
123+
# 端口 日志 pid文件
72124
port 6379
73125
logfile /var/log/redis/6379/redis.log
74126
pidfile /var/run/redis/redis_6379.pid
@@ -85,28 +137,28 @@ EOF
85137

86138

87139

88-
## 4.使用systemd管理redis
140+
### 使用systemd管理redis
89141

90142
:::tip 说明
91143

92-
这边并没有使用 `ExecStop=/bin/kill -s QUIT $MAINPID` 这样的命令来停止redis, 因为使用这个语句在运行`systemctl stop redis`, redis并未执行关闭动作, 而是直接退出. 这时候用 `systemctl status redis` 查看状态是failed. 只有用`ExecStop=/install_path/bin/redis-cli -p 16379 shutdown` 才能正确停止redis
144+
这边并没有使用 `ExecStop=/bin/kill -s QUIT $MAINPID` 这样的命令来停止redis因为使用这个语句在运行 `systemctl stop redis`redis并未执行关闭动作而是直接退出这时候用 `systemctl status redis` 查看状态是failed只有用 `ExecStop=/install_path/bin/redis-cli -p 6379 shutdown` 才能正确停止redis
93145

94146
:::
95147

96148

97149

98150
```shell
99-
cat >/usr/lib/systemd/system/redis.service<<'EOF'
151+
cat > /usr/lib/systemd/system/redis.service << EOF
100152
[Unit]
101153
Description=Redis
102154
After=network.target
103155
104156
[Service]
105157
# Type=forking
106158
PIDFile=/var/run/redis/redis_6379.pid
107-
ExecStart=/usr/local/redis-5.0.7/src/redis-server /etc/redis/6379/redis.conf
159+
ExecStart=/usr/local/redis-${REDIS_VERSION}/src/redis-server /etc/redis/6379/redis.conf
108160
ExecReload=/bin/kill -s HUP $MAINPID
109-
ExecStop=/usr/local/redis-5.0.7/src/redis-cli -p 6379 shutdown
161+
ExecStop=/usr/local/redis-${REDIS_VERSION}/src/redis-cli -p 6379 shutdown
110162
PrivateTmp=true
111163
112164
[Install]
@@ -116,7 +168,7 @@ EOF
116168

117169

118170

119-
## 5.启动redis
171+
### 启动redis
120172

121173
```shell
122174
# 重载系统服务
@@ -128,17 +180,17 @@ systemctl enable redis && systemctl start redis
128180

129181

130182

131-
## 6.使用systemd管理redis遇到的问题
183+
### 使用systemd管理redis遇到的问题
132184

133-
**问题一:重载系统服务后启动redis卡住不动**
185+
#### 重载系统服务后启动redis卡住不动
134186

135-
**解决方法**
187+
解决方法
136188

137-
> **注释 `/usr/lib/systemd/system/redis.service` `Type=forking` 一项**
189+
注释 `/usr/lib/systemd/system/redis.service` 文件中 `Type=forking` 一项
138190

139191

140192

141-
**网上解释原因**
193+
网上解释原因
142194

143195
> **If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main daemon process. This is the behavior of traditional UNIX daemons. If this setting is used, it is recommended to also use the PIDFile= option, so that systemd can identify the main process of the daemon. systemd will proceed with starting follow-up units as soon as the parent process exits.**
144196
>
@@ -148,35 +200,43 @@ systemctl enable redis && systemctl start redis
148200

149201

150202

151-
**问题二:pid原先路径为 `/var/run/redis_6379.pid` ,报错 `Failed at step EXEC spawning /usr/local/redis-5.0.7/src: Permission denied`**
152-
153-
154-
155-
**解决方法:**
156-
157-
> **将pid路径改为 `/var/run/redis/redis_6379.pid` 就可以了,原因未知**
203+
### redis安全配置
158204

205+
`protected-mode`
159206

207+
:::tip 说明
160208

161-
## 7.redis安全配置
209+
保护模式,是否只允许本地访问,默认是 `yes`
162210

163-
**protected-mode 保护模式,是否只允许本地访问,默认是yes**
211+
:::
164212

165213
```shell
166214
protected-mode no
167215
```
168216

169217

170218

171-
**bind 指定IP进行监听**
219+
`bind`
220+
221+
:::tip 说明
222+
223+
指定IP进行监听
224+
225+
::;
172226

173227
```shell
174228
bind 127.0.0.1
175229
```
176230

177231

178232

179-
**requirepass 增加密码**
233+
`requirepass`
234+
235+
:::tip 说明
236+
237+
增加密码
238+
239+
:::
180240

181241
```shell
182242
requirepass 1

0 commit comments

Comments
 (0)