1 简介
Redis 是内存中的键值存储,以其灵活性、性能和广泛的语言支持而著称。 在本指南中,我们将演示如何在 Ubuntu 18.04.6服务器上安装和配置 Redis。
2 安装生成和测试依赖项
为了得到 Redis 的最新版本,我们将从源头编译和安装该软件。 在下载代码之前,我们需要满足构建依赖性,以便能够编译软件。
代码语言:javascript复制sudo apt-get install gcc make
# 下载的deb包在这个目录
/var/cache/apt/archives
# 如果是要在无网环境部署可以将离线deb包拷贝到服务器然后
sudo dpkg -i *.deb
3 下载,编译和安装redis
代码语言:javascript复制wget http://download.redis.io/releases/redis-6.0.1.tar.gz
tar xf redis-6.0.1.tar.gz
cd redis-6.0.1/
make && make MALLOC=libc
3.1 配置Redis
代码语言:javascript复制sudo mkdir /data/jsdesign/apps/redis/{data,bin,conf} -p
sudo cp /tmp/redis-6.0.1/redis.conf /data/jsdesign/apps/redis/conf/
sudo vim /data/jsdesign/apps/redis/conf/redis.conf
216 supervised systemd
345 dir /data/jsdesign/apps/redis/data
3.2 创建一个Redis系统单元文件
代码语言:javascript复制sudo vim /lib/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/data/jsdesign/apps/redis/bin/redis-server /data/jsdesign/apps/redis/conf/redis.conf
ExecStop=/data/jsdesign/apps/redis/bin/redis-cli shutdown
Restart=always
[Install]
WantedBy=multi-user.target
3.3 创建redis用户,组
代码语言:javascript复制sudo adduser --system --group --no-create-home redis
sudo chown -R redis:redis /data/jsdesign/apps/redis
4 启动和测试redis
4.1 启动redis服务
代码语言:javascript复制sudo systemctl start redis
ubuntu-test-240@ubuntu-test-240:~/redis-6.0.1$ sudo systemctl status redis
● redis.service - Redis In-Memory Data Store
Loaded: loaded (/etc/systemd/system/redis.service; static; vendor preset: enabled)
Active: active (running) since Thu 2022-01-06 07:41:16 UTC; 1h 9min ago
Main PID: 9034 (redis-server)
Tasks: 4 (limit: 4656)
CGroup: /system.slice/redis.service
└─9034 /data/jsdesign/apps/redis/bin/redis-server 127.0.0.1:6379
Jan 06 07:41:16 ubuntu-test-240 redis-server[9034]: 9034:M 06 Jan 2022 07:41:16.041 # Server initialized
Jan 06 07:41:16 ubuntu-test-240 redis-server[9034]: 9034:M 06 Jan 2022 07:41:16.041 # WARNING overcommit_memory i
Jan 06 07:41:16 ubuntu-test-240 redis-server[9034]: 9034:M 06 Jan 2022 07:41:16.041 # WARNING you have Transparen
Jan 06 07:41:16 ubuntu-test-240 redis-server[9034]: 9034:M 06 Jan 2022 07:41:16.041 * Loading RDB produced by ver
Jan 06 07:41:16 ubuntu-test-240 redis-server[9034]: 9034:M 06 Jan 2022 07:41:16.041 * RDB age 0 seconds
Jan 06 07:41:16 ubuntu-test-240 redis-server[9034]: 9034:M 06 Jan 2022 07:41:16.041 * RDB memory usage when creat
Jan 06 07:41:16 ubuntu-test-240 redis-server[9034]: 9034:M 06 Jan 2022 07:41:16.041 * DB loaded from disk: 0.000
Jan 06 07:41:16 ubuntu-test-240 redis-server[9034]: 9034:M 06 Jan 2022 07:41:16.041 * Ready to accept connections
Jan 06 07:41:16 ubuntu-test-240 redis-server[9034]: 9034:M 06 Jan 2022 07:41:16.041 # systemd supervision request
Jan 06 08:50:26 ubuntu-test-240 systemd[1]: redis.service: Current command vanished from the unit file, execution
lines 1-18/18 (END)
sudo systemctl enable redis
5 测试Redis实例功能
代码语言:javascript复制ubuntu-test-240@ubuntu-test-240:~/redis-6.0.1$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set test "redsi is ok"
OK
127.0.0.1:6379> get test
"redsi is ok"
ubuntu-test-240@ubuntu-test-240:~/redis-6.0.1$ sudo systemctl restart redis
ubuntu-test-240@ubuntu-test-240:~/redis-6.0.1$ redis-cli
127.0.0.1:6379> get test
"redsi is ok"