系列文章目录
python+vue3+fastapi+ai 学习_浪淘沙jkp的博客-CSDN博客https://blog.csdn.net/jiangkp/category_12623996.html
前言
安装redis 我会以三种方式安装,
第一、直接最简单安装,适用于测试环境玩玩
第二、conf配置安装
第三、集群环境安装
一、redis用途
Redis是一个开源的内存数据库,常用于缓存、消息队列、会话存储、实时分析等方面。其主要用途包括:
-
缓存:Redis可以将常用的数据存储在内存中,以提高数据访问速度,减轻后端数据库的压力。
-
消息队列:Redis支持发布/订阅模式,可以用作消息队列系统,实现异步通信。
-
会话存储:Redis可以用来存储用户会话信息,实现分布式会话管理。
-
实时分析:Redis支持多种数据结构,包括列表、集合、哈希表等,可以用于实时数据分析和统计。
-
分布式锁:Redis提供了分布式锁的实现方式,可以用来控制并发访问。
总的来说,Redis具有高性能、高可用性和丰富的数据结构,适用于多种场景下的数据存储和处理需求
二、简单安装
1、安装
docker run -d --name redis_test --restart=always -p 6379:6379 redis --requirepass 123456
-d 后台运行
--name redis_test 设置容器名为 redis_test
--restart=always 开机自启
-p 6379:6379 端口映射
redis 镜像名 直接获取光网lastest,可以指定版本
--requirepass 123456 密码为 123456
victor_t400@victort400:~$ docker run -d --restart=always --name victorredis redis -p 6379:6379 --requirepass 123456
Unable to find image 'redis:latest' locally
latest: Pulling from library/redis
b0a0cf830b12: Pull complete
214d0afb35ca: Pull complete
16a9d12e7a2c: Pull complete
cb9709829e8b: Pull complete
00e912971fa2: Pull complete
f7ebca356832: Pull complete
4f4fb700ef54: Pull complete
c16c264be546: Pull complete
Digest: sha256:f14f42fc7e824b93c0e2fe3cdf42f68197ee0311c3d2e0235be37480b2e208e6
Status: Downloaded newer image for redis:latest
b8922e41585aff4de14edc02b8caab80a6bef91decaecd5c0b50ac593a4fe275
注意:上面这条命令网上拷贝的不行,放在文件里编译了也不行,直接敲好了,我找谁去
ps下是肯定的
victor_t400@victort400:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8922e41585a redis "docker-entrypoint.s…" About a minute ago Restarting (1) 34 seconds ago victorredis
restarting啥意思?
victor_t400@victort400:~$ docker logs b8922e41585a
*** FATAL CONFIG FILE ERROR (Redis 7.2.4) ***
Reading the configuration file, at line 2
>>> '"-p" "6379:6379"'
Bad directive or wrong number of arguments*** FATAL CONFIG FILE ERROR (Redis 7.2.4) ***
Reading the configuration file, at line 2
>>> '"-p" "6379:6379"'
Bad directive or wrong number of arguments*** FATAL CONFIG FILE ERROR (Redis 7.2.4) ***
Reading the configuration file, at line 2
>>> '"-p" "6379:6379"'
Bad directive or wrong number of arguments
victor_t400@victort400:~$ sudo lsof -nP -iTCP -sTCP:LISTEN #6379 没有被占用
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root 93u IPv6 5638 0t0 TCP *:22 (LISTEN)
systemd-r 586 systemd-resolve 15u IPv4 7612 0t0 TCP 127.0.0.53:53 (LISTEN)
systemd-r 586 systemd-resolve 17u IPv4 7614 0t0 TCP 127.0.0.54:53 (LISTEN)
container 733 root 8u IPv4 8382 0t0 TCP 127.0.0.1:37721 (LISTEN)
sshd 1200 root 3u IPv6 5638 0t0 TCP *:22 (LISTEN)
victor_t400@victort400:~$ ^C
victor_t400@victort400:~$ ^C
victor_t400@victort400:~$ docker stop b8922e41585a
b8922e41585a
victor_t400@victort400:~$ docker kill b8922e41585a
Error response from daemon: Cannot kill container: b8922e41585a: Container b8922e41585aff4de14edc02b8caab80a6bef91decaecd5c0b50ac593a4fe275 is not running
victor_t400@victort400:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
victor_t400@victort400:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8922e41585a redis "docker-entrypoint.s…" 50 minutes ago Exited (1) 36 seconds ago victorredis
a18ad488d054 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago elegant_edison
victor_t400@victort400:~$ docker rm b8922e41585a
b8922e41585a
victor_t400@victort400:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a18ad488d054 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago elegant_edison
victor_t400@victort400:~$ ^C
victor_t400@victort400:~$ docker run -d --restart=always --name victorredis -p 6379:6379 redis --requirepass 123456
341e11bffc02971d177b23d32779b5ff11da559832b050bf585985918439a76b
victor_t400@victort400:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
341e11bffc02 redis "docker-entrypoint.s…" 7 seconds ago Up 5 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp victorredis
a18ad488d054 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago elegant_edison
victor_t400@victort400:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
341e11bffc02 redis "docker-entrypoint.s…" 11 seconds ago Up 9 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp victorredis
victor_t400@victort400:~$
有问题
docker run -d --restart=always --name victorredis redis -p 6379:6379 --requirepass 123456
没有问题,看出问题了吗,反正我是没有
docker run -d --restart=always --name victorredis -p 6379:6379 redis --requirepass 123456
2、测试
本地测试
victor_t400@victort400:~$ docker exec -it 341e11bffc02 bash
root@341e11bffc02:/data# redis-cli
127.0.0.1:6379> set a 1
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> get a
"1"
127.0.0.1:6379>
版本 7.2.4 这应该是最新的
root@341e11bffc02:/data# redis-cli --version
redis-cli 7.2.4
外部连接
用nvicat 16版本,需要的可以留言,我可以给个百度网盘地址
连接不上,估计是端口没有开放吧!去试试再说。
victor_t400@victort400:~$ sudo ufw allow 6379
Rule added
Rule added (v6)
victor_t400@victort400:~$ sudo ufw allow 3306
Rule added
Rule added (v6)
victor_t400@victort400:~$ sudo ufw status
Status: activeTo Action From
-- ------ ----
22 ALLOW Anywhere
6379 ALLOW Anywhere
3306 ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
6379 (v6) ALLOW Anywhere (v6)
3306 (v6) ALLOW Anywhere (v6)victor_t400@victort400:~$ sudo ufw reload
Firewall reloaded
再连接
点击底下的ok,不是中间的,进入下面界面,说明连到redis了,可以操作的
测试用测试用,开发玩着用,生产是不能上的,就数据不能安全的保存,就不能用
三、单机指定 redis.conf 配置文件安装
这也是我们一般情况下的安装方式,测试生产两不误
我们把前面安装的redis删除掉,免得还要改端口号
1、删除上一节安装的redis
victor_t400@victort400:~/redis$ docker stop 341e11bffc02
341e11bffc02
victor_t400@victort400:~/redis$ docker rm 341e11bffc02
341e11bffc02
victor_t400@victort400:~/redis$ ps
PID TTY TIME CMD
2378 pts/1 00:00:00 bash
3060 pts/1 00:00:00 ps
victor_t400@victort400:~/redis$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
victor_t400@victort400:~/redis$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a18ad488d054 hello-world "/hello" 8 hours ago Exited (0) 8 hours ago elegant_edison
victor_t400@victort400:~/redis$
2、建映射目录,下载redis.conf
版本7.2.4,前面得到过
redis.conf里修改3个地方
port 6379===》port 6479
bind 127.0.0.1 -::1===》bind 0.0.0.0
requirepass foobared===》requirepass 123456
appendonly no===》appendonly yes
victor_t400@victort400:/home$ cd victor_t400/
victor_t400@victort400:~$ mkdir redisvictor_t400@victort400:~$ cd redis
victor_t400@victort400:~/redis$ mkdir data
victor_t400@victort400:~/redis$ ls
datavictor_t400@victort400:~/redis$ wget https://raw.githubusercontent.com/redis/redis/7.2/redis.conf
3、启动redis容器
docker run --restart=always --privileged=true \
-p 6379:6379 \
--name redis-conf \
-v /home/victor_t400/redis/redis.conf:/etc/redis/redis.conf \
-v /home/victor_t400/redis/data:/data \
-d redis:latest redis-server /etc/redis/redis.conf
victor_t400@victort400:~/redis$ docker run --restart=always \
-p 6379:6379 \
--name redis-conf \
-v /home/victor_t400/redis/redis.conf:/etc/redis/redis.conf \
-v /home/victor_t400/redis/data:/data \
-d redis:latest redis-server /etc/redis/redis.conf
0aec664502c3013bcb3b2fa2cee12d5f2272e49ec125d35bbc1fa2ad522aa918
victor_t400@victort400:~/redis$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0aec664502c3 redis:latest "docker-entrypoint.s…" 7 seconds ago Up 6 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis-conf
4、测试
其实上面那个“爱了爱了“连接也是可以连接的,端口 IP 密码都一样
5、我们看看log,改了下
victor_t400@victort400:~/redis$ docker logs redis-conf
1:C 03 May 2024 10:18:03.574 # 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.
1:C 03 May 2024 10:18:03.574 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 03 May 2024 10:18:03.574 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 03 May 2024 10:18:03.574 * Configuration loaded
1:M 03 May 2024 10:18:03.575 * monotonic clock: POSIX clock_gettime
1:M 03 May 2024 10:18:03.576 # Failed to write PID file: Permission denied
1:M 03 May 2024 10:18:03.576 * Running mode=standalone, port=6379.
1:M 03 May 2024 10:18:03.576 * Server initialized
1:M 03 May 2024 10:18:03.576 * Ready to accept connections tcp
victor_t400@victort400:~/redis$ sudo vi /etc/sysctl.conf在最后添加vm.overcommit_memory = 1
victor_t400@victort400:~/redis$ sudo sysctl -p
vm.overcommit_memory = 1
非常意外的发现data下没有数据,我还重新装了一遍,还是没有,结果在里面加了点数据,就有了,哎,我能怎么说呢,这个玩意是dockers里面的redis引起映射的data目录变化,有数据才会有映射吧
总结
安装虽然顺利,但是还是碰到一些问题,不停查,不停装。。。。。