Ubuntu的安装单机Redis

53 阅读1分钟

一、前言

Redis是一种基于内存的高性能键值数据库,支持极速读写和丰富的数据结构,如字符串、哈希和列表。其卓越的响应速度和持久化能力使其成为缓存、会话存储和消息队列等场景的首选。借助Redis,企业能够显著提升应用性能,并轻松应对高并发需求。

二、安装过程

2.1 安装编译工具

 yum -y install gcc automake autoconf libtool make

 yum install gcc gcc-c++

2.2 安装Redis

去官网下载安装包,本地以redis-6.2.14为例。


wget https://download.redis.io/releases/redis-6.2.14.tar.gz

# 解压 
tar -xvf redis-6.2.14.tar.gz

cd redis-6.2.14.tar.gz

make 

make PREFIX=/usr/local/redis install  
-----------------------------------------
Hint: It's a good idea to run 'make test' ;)
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install

### 测试验证
make test

2.3 修改配置

cp redis.conf /etc/redis

cd /usr/local/redis/

mkdir data

vim //etc/redis/redis.conf

# 1.后台启动,d
aemonize yes      

# 2.绑定IP,
bind 192.168.XX.XXX    

# 3.数据存放路径,
dir /usr/local/redis/data rdb存放的路径 

# 4.指定持久化方式
appendonly yes

2.4 配置启动服务

sudo vim /etc/systemd/system/redis.service

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
# 最重要的一行:指定配置文件启动
ExecStart=/usr/local/redis/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli shutdown
# 另一种停止方式(可选):
# ExecStop=/bin/kill -s TERM $MAINPID
PIDFile=/var/run/redis/redis-server.pid
Restart=always
# 重启间隔(可选)
RestartSec=10

[Install]
WantedBy=multi-user.target

2.5 服务启动

sudo adduser --system --no-create-home --shell /bin/false --group --disabled-login redis
sudo chown -R redis:redis /var/run/redis

#重新加载 systemd 配置
sudo systemctl daemon-reload
# 启用 Redis 服务开机自启动
sudo systemctl enable redis.service

# 立即启动 Redis 服务
sudo systemctl start redis.service

# 检查 Redis 服务状态,确认是否运行成功
sudo systemctl status redis.service

参考资料

Redis的性能优化