一、Linux环境
1、下载
官网提供的源码下载地址:
https://github.com/redis/redis/archive/7.0.5.tar.gz
2、将源码上传至服务器
3、解压缩
# 将解压缩后的文件放置在同目录的source文件夹下
tar -zxvf redis-7.0.5.tar.gz -C ./source
4、编译安装
对源码进行编译、安装
# PREFIX参数表示安装在哪个目录下
make PREFIX=/home/ubuntu/redis/redis-7.0.5 install
5、复制配置文件
# 将源码提供的配置文件复制到安装目录下
cp /home/ubuntu/redis/source/redis-7.0.5/redis.conf /home/ubuntu/redis/redis-7.0.5/bin/
6、启动redis
# 启动redis时需要指定配置文件
./redis-server /home/ubuntu/redis/redis-7.0.5/bin/redis.conf
7、修改配置文件
# 将下面一行配置添加注释,使其他主机可以访问redis服务
# bind 127.0.0.1 -::1
# 将以下配置取消注释,修改密码
requirepass ********
# 下面配置必须打开,密码才能生效
protected-mode yes
# 修改日志文件
logfile "/home/ubuntu/redis/redis.7.0.5/bin/redis.log"
8、设置服务
# 将源码文件中utils/install_server.sh文件以下脚本注释
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
-
创建服务脚本
- vim install_server_command.sh
sudo REDIS_PORT=6379 \ REDIS_CONFIG_FILE=/home/ubuntu/redis/redis-7.0.5/bin/redis.conf \ REDIS_LOG_FILE=/home/ubuntu/redis/redis.7.0.5/bin/redis.log \ REDIS_DATA_DIR=/home/ubuntu/redis/redis.7.0.5/bin/ \ REDIS_EXECUTABLE=`command -v /home/ubuntu/redis/redis-7.0.5/bin/redis-server` \ /home/ubuntu/redis/source/redis-7.0.5/utils/install_server.sh
-
创建服务文件
- cd /lib/systemd/system
- sudo touch redis.service
- sudo chmod 644 redis.service
- sudo vim redis.service
[Unit] Description=Redis After=network.target [Service] ExecStart=/home/ubuntu/redis/redis-7.0.5/bin/redis-server /home/ubuntu/redis/redis-7.0.5/bin/redis.conf --daemonize no ExecStop=/home/ubuntu/redis/redis-7.0.5/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown [Install] WantedBy=multi-user.target
-
创建软链接,为服务自启动准备
ln -s /lib/systemd/system/redis.service /etc/systemd/system/multi-user.target.wants/redis.service
- 刷新配置
systemctl daemon-reload
- 杀死已存在的redis进程
ps aux|grep redis
kill -9 [pid]
- 启动redis
systemctl start redis