51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Ubuntu 22.04安装Redis

一、方式一

1、安装Redis

1)更新系统

root@ubuntu:~# apt update -y

2)安装Redis

root@ubuntu:~# apt -y install redis-server

注意:Redis安装完成后,默认已启动,默认开机启动。

3)查看Redis状态

root@ubuntu:~# systemctl status redis

2、验证Redis

1)登录Redis

root@ubuntu:~# redis-cli

127.0.0.1:6379> keys *

(empty array)

3、配置Redis

1)配置密码

root@ubuntu:~# vim /etc/redis/redis.conf

2)开启远程链接

root@ubuntu:~# vim /etc/redis/redis.conf

3)重启服务

root@ubuntu:~# systemctl restart redis

root@ubuntu:~# systemctl status redis

4)远程连接验证

使用Redis Desktop Manager进行连接测试

Redis Desktop Manager下载地址:https://github.com/uglide/RedisDesktopManager/releases/download/0.9.3/redis-desktop-manager-0.9.3.817.exe

5)服务管理

1、启动服务

root@ubuntu:~# systemctl start redis

2、停止服务

root@ubuntu:~# systemctl stop redis

3、重启服务

root@ubuntu:~# systemctl restart redis

4、开机启动

root@ubuntu:~# systemctl enable redis-server

5、禁止开机启动

root@ubuntu:~# systemctl disable redis-server

二、方式二

1、安装Redis

1)安装依赖包

root@ubuntu:~# apt -y install wget make gcc pkg-config

2)下载Redis安装包

root@ubuntu:~# wget http://download.redis.io/releases/redis-6.2.16.tar.gz

3)解压软件包

root@ubuntu:~# tar xf redis-6.2.16.tar.gz

4)编译

root@ubuntu:~# cd redis-6.2.16

root@ubuntu:~/redis-6.2.16# make install PREFIX=/usr/local/redis

2、配置Redis

1)配置Redis

root@ubuntu:~/redis-6.2.16# cp redis.conf /usr/local/redis/

2)配置软连接

root@ubuntu:~/redis-6.2.16# ln -sf /usr/local/redis/bin/redis-* /usr/bin/

3)配置访问密码

root@ubuntu:~/redis-6.2.16# Passwd=$(openssl rand -base64 12)

root@ubuntu:~/redis-6.2.16# sed -i "s/# requirepass foobared/requirepass ${Passwd}/" /usr/local/redis/redis.conf

4)开启远程连接

root@ubuntu:~/redis-6.2.16# sed -i "s/127.0.0.1/0.0.0.0/g" /usr/local/redis/redis.conf

5)开启后台运行

root@ubuntu:~/redis-6.2.16# sed -i "/daemonize/s/no/yes/" /usr/local/redis/redis.conf

6)配置系统服务

root@ubuntu:~/redis-6.2.16# vim /usr/lib/systemd/system/redis.service

# 基础信息
[Unit]
# 描述
Description=Redis Server
# 在哪个服务之后启动
After=syslog.target network.target remote-fs.target nss-lookup.target
服务信息
[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
启动命令
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
安装相关信息
[Install]
WantedBy=multi-user.target

7)启动Redis

root@ubuntu:~/redis-6.2.16# systemctl daemon-reload

root@ubuntu:~/redis-6.2.16# systemctl start redis

root@ubuntu:~/redis-6.2.16# systemctl status redis

8)远程连接验证

使用Redis Desktop Manager进行连接测试

Redis Desktop Manager下载地址:https://github.com/uglide/RedisDesktopManager/releases/download/0.9.3/redis-desktop-manager-0.9.3.817.exe

获取Redis密码

root@ubuntu:~/redis-6.2.16# grep "^requirepass" /usr/local/redis/redis.conf

requirepass 54dtfu2OZPQBtrAz

9)服务管理

1、启动服务

root@ubuntu:~/redis-6.2.16# systemctl start redis

2、停止服务

root@ubuntu:~/redis-6.2.16# systemctl stop redis

3、重启服务

root@ubuntu:~/redis-6.2.16# systemctl restart redis

4、开机启动

root@ubuntu:~/redis-6.2.16# systemctl enable redis

5、禁止开机启动

root@ubuntu:~/redis-6.2.16# systemctl disable redis
继续阅读 Redis最后更新:2024-11-7

赞(1)
未经允许不得转载:工具盒子 » Ubuntu 22.04安装Redis