概述

每次安装总是会忘记一些命令或遇到问题,考虑到每次搜索然后看到不那么靠谱的文章导致浪费不少时间,故有此文,仅仅是记录一下,持续更新。

单机版

Linux

注:此文Linux版本为CentOS 8。
先去Redis官网下载压缩包,如redis-5.0.9.tar.gz,然后上传到root用户的默认目录:/root,解压到/usr/local目录:
tar zxvf redis-5.0.9.tar.gz -C /usr/local 目录重命名:
mv redis-5.0.9/ redis 进入redis目录:
cd redis 执行make,然后执行make install,至此安装成功。

如果执行make报错:/bin/sh: cc: 未找到命令,则执行命令:yum -y install gcc automake autoconf libtool make

如果执行make报错:

In file included from adlist.c:34:0:
zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录

则执行命令:make MALLOC=libc

pwd查看当前目录,输出:
/usr/local/redisll查看所有文件,输出:

total 284
-rw-rw-r--.  1 root root 119270 Apr 17  2020 00-RELEASENOTES
-rw-rw-r--.  1 root root     53 Apr 17  2020 BUGS
-rw-rw-r--.  1 root root   2381 Apr 17  2020 CONTRIBUTING
-rw-rw-r--.  1 root root   1487 Apr 17  2020 COPYING
drwxrwxr-x.  6 root root    192 Feb 19 01:50 deps
-rw-r--r--.  1 root root     92 Feb 19 02:58 dump.rdb
-rw-rw-r--.  1 root root     11 Apr 17  2020 INSTALL
-rw-rw-r--.  1 root root    151 Apr 17  2020 Makefile
-rw-rw-r--.  1 root root   6888 Apr 17  2020 MANIFESTO
-rw-rw-r--.  1 root root  20555 Apr 17  2020 README.md
-rw-rw-r--.  1 root root  61819 Feb 19 03:12 redis.conf
-rwxrwxr-x.  1 root root    275 Apr 17  2020 runtest
-rwxrwxr-x.  1 root root    280 Apr 17  2020 runtest-cluster
-rwxrwxr-x.  1 root root    373 Apr 17  2020 runtest-moduleapi
-rwxrwxr-x.  1 root root    281 Apr 17  2020 runtest-sentinel
-rw-rw-r--.  1 root root   9710 Apr 17  2020 sentinel.conf
drwxrwxr-x.  3 root root   8192 Feb 19 01:51 src
drwxrwxr-x. 11 root root    182 Apr 17  2020 tests
drwxrwxr-x.  8 root root   4096 Apr 17  2020 utils

启动Redis:
/usr/local/redis/src/redis-server /usr/local/redis/redis.conf 看到熟悉的控制台输出:

36705:C 19 Feb 2021 02:57:50.909 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
36705:C 19 Feb 2021 02:57:50.909 # Redis version=5.0.9, bits=64, commit=00000000, modified=0, pid=36705, just started
36705:M 19 Feb 2021 02:57:50.910 * Increased maximum number of open files to 10032 (it was originally set to 1024).
36705:M 19 Feb 2021 02:57:50.910 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
36705:M 19 Feb 2021 02:57:50.910 # Server initialized
36705:M 19 Feb 2021 02:57:50.910 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 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.
36705:M 19 Feb 2021 02:57:50.911 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
36705:M 19 Feb 2021 02:57:50.911 * Ready to accept connections

但是这种方式是前台启动,修改配置文件:
vim /usr/local/redis/redis.conf 找到daemonize,设置为daemonize yes 命令太长不方便,创建软链接:
ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server 启动:
redis-server /usr/local/redis/redis.conf

问题

如果执行make最后输出:
Hint: It's a good idea to run 'make test' ;) 一般是没有完全安装成功,执行命令:
make test 根据报错信息来安装相应的包。

遇到的问题:

  1. make test报错You need tcl 8.5 or newer in order to run the Redis test 解决方法就是下载安装tcl
  2. wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
    tar xzvf tcl8.6.1-src.tar.gz
    cd tcl8.6.1/unix/
    ./configure
    make
    make install
  3. make test报错:I/O error reading reply while executing 解决方案也是安装tcl。

客户端连接

现在已经成功安装并启动,如何通过客户端如RedisPlus连接。
修改配置文件:
vim /usr/local/redis/redis.conf 更新配置:
protected-mode no 仅仅修改如上配置并不行。
还需要修改bind 127.0.0.1bind 192.168.20.149
当然,这种方式是配置无密码。

Windows

下载地址

注册为本地服务:

./redis-server.exe --service-install redis.windows.conf --service-name redis --port 6379 打开 任务管理器-服务列表,即可看到Redis服务,右键打开服务,打开服务管理器,右键属性,可以设置启动类型,默认即为自动:

redis安装指定安装路径和配置文件路径 redis make install指定安装目录_redis


使用一段时间后,发现Redis服务已停止

redis安装指定安装路径和配置文件路径 redis make install指定安装目录_Redis_02


不能启动,在服务管理器启动时的报错信息为 本地计算机上的Redis服务启动后停止

redis安装指定安装路径和配置文件路径 redis make install指定安装目录_Redis_03


卸载重新安装:

./redis-server.exe --service-uninstall redis.windows.conf --service-name redis --port 6379

安装时需要追加参数--maxmemory 200m,即完整命令为:

./redis-server.exe --service-install redis.windows.conf --service-name redis --port 6379 --maxmemory 200m

Mac

# 安装
brew install redis
brew services start redis
# 以后台进程的形式启动
redis-server --daemonize yes
# 查看信息
brew services info redis
# 停止
brew services stop redis
brew services restart redis

设置开机自启动

创建.plist配置文件:
sudo vim /Library/LaunchDaemons/io.redis.redis-server.plistia进入编辑模式,粘帖文本如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>io.redis.redis-server</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/redis-server</string>       
        <string>/usr/local/etc/redis.conf</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

注:上面提到我是使用brew来安装Redis,redis-server默认位置就是/usr/local/bin/redis-server,这里应该根据你的安装方式和位置做相应调整。

如果不知道redis-server的路径,可使用命令which查询:which redis-server
如果不知道redis.conf的路径,可使用locate命令查询:locate redis.conf

使用launchctl将配置加入launchd
sudo launchctl load /Library/LaunchDaemons/io.redis.redis-server.plist 此时Mac开机或重启都会自动启动redis

使用launchctl手动启动/关闭redis
启动redis

sudo launchctl start io.redis.redis-server
关闭redis

sudo launchctl stop io.redis.redis-server

Failed opening the temp RDB file temp-80987.rdb (in server root dir /opt/ada) for saving: Permission denied

查找redis.conf文件:locate redis.conf

注意使用locate命令前,需要先执行:
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist Mac终端有提醒。

集群版