Redis 安装

Window 下安装

下载地址 //github.com/MSOpenTech/redis/releases

Redis 支持 32 位和 64 位。根据你系统平台选择,打开文件夹,打开一个 cmd 窗口  使用 cd 命令切换目录到 d:\redis  运行:

redis-server.exe redis.windows.conf
 可把 redis 的路径加到系统的环境变量,省得再输路径,后面的那个 redis.windows.conf 可以省略,默认的。输入之后,会显示如下界面:另启一个 cmd 窗口,原来的不要关闭,不然就无法访问服务端了。切换到 redis 目录下运行:
redis-cli.exe -h 127.0.0.1 -p 6379

设置键值对:

set myKey abc

取出键值对:

get myKey

Redis 安装


Linux 下安装

下载 //redis.io/download,下载最新稳定版本。

本文使用的最新文档版本为 5.3.2,下载并安装:

$ wget http://download.redis.io/releases/redis-5.3.2.tar.gz
$ tar xzf redis-5.3.2.tar.gz
$ cd redis-5.3.2
$ make

make完后 redis-5.3.2目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下:

下面启动redis服务.

$ cd src
$ ./redis-server

注意这种方式启动redis 使用的是默认配置。也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动。

$ cd src
$ ./redis-server ../redis.conf

redis.conf 是一个默认的配置文件。可以根据需要使用自己的配置文件。

启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了。比如:

$ cd src
$ ./redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

Centos7安装Redis

通过安装脚本自动化安装 setup.redis.sh
#!/bin/sh
main_path=/usr/local/redis
install_name=redis-6.2.5.tar.gz
install_name_dir=redis-6.2.5
install_config_file=$main_path/etc/redis.conf
install_path=/usr/local/redis/
env_fun() {
    echo "--- 检测本机环境 ---"
    cd /usr/local/redis/redis-4.0.10     
    if [[ $? -ne 0 ]]; then
        echo "--- redis不存在 ---"
        return 10
    else
        echo "--- redis已存在 ---"
        return 12
    fi
}
install_fun(){
    echo "--- 建立redis文件夹:/usr/local/redis ---"
    mkdir -p /usr/local/redis
    if [ ! -f "$install_name" ]; then
      echo "----下载安装包 redis-6.2.5.tar.gz ---"
      wget https://download.redis.io/releases/redis-6.2.5.tar.gz   
    fi
    echo "--- 正在解压安装文件 ---"
    tar -zxvf $install_name
    cd $install_name_dir
    echo "--- 开始编译 安装---"
    make install PREFIX=$main_path
    cp ./redis.conf $install_config_file
}
main(){
    echo "--- 安装 redis-6.2.5  --"
    sleep 1
    env_fun
    re=$?
    if [ 10 -eq $re ] ;then
        install_fun        
        cp ./redis.service /etc/systemd/system/redis.service
        ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis
        echo "--- 开启后台运行 ---"
        sed -i 's|daemonize no|daemonize yes|' $install_config_file
        echo 'requirepass ferryferry' >> $install_config_file
        echo "------------------------------开启远程访问------------------------------------------"
        sed -i 's|bind 127.0.0.1|#bind 127.0.0.1|' $install_config_file
        echo "--- 启动redis ---"
       /usr/local/redis/bin/redis-server $install_config_file
        sleep 2        
        wc=`ps -ef|grep redis-server |wc -l`
        if [ $wc -gt 0 ]; then        
            systemctl daemon-reload
            systemctl stop redis.service
            systemctl start redis.service
            systemctl enable redis.service            
            echo "--- redis启动成功 配置密码 ferryferry ---"
        else
            echo "--- redis启动失败 ---"
        fi
    else
       echo "--- redis已存在 不需要安装 ---"
    fi
}
main
exit 0
redis.service
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target

一、安装gcc依赖 redis 是 C 语言开发,先确认是否安装 gcc 环境(gcc -v),如果没有安装,先 安装# yum install -y gcc
二、下载  # wget http://download.redis.io/releases/redis-5.3.2.tar.gz
# tar -zxvf redis-5.3.2.tar.gz

三、cd切换到redis解压目录下,执行编译
# cd redis-5.3.2
# make
四、安装并指定安装目录
# make install PREFIX=/usr/local/redis
五、启动服务
5.1 可在前台启动
# cd /usr/local/redis/bin/
# ./redis-server
5.2 也可在后台启动
从 redis 的源码目录中复制 redis.conf 到 redis 的安装目录
# cp /usr/local/redis-5.3.2/redis.conf /usr/local/redis/etc/
修改 redis.conf 文件
把 daemonize no 改为 daemonize yes
# vi redis.conf
后台启动 # ./redis-server redis.conf

六、可以设置开机启动 添加开机启动服务
# vi /etc/systemd/system/redis.service
以下内容
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
注意ExecStart 配置成自己的路径
设置开机启动
# systemctl daemon-reload
# systemctl start redis.service
# systemctl enable redis.service
创建 redis 命令软链接
# ln -s /usr/local/redis/bin/redis-cli /usr/bin/redis
测试 redis

服务操作命令
systemctl start redis.service   #启动redis服务
systemctl stop redis.service   #停止redis服务
systemctl restart redis.service   #重新启动服务
systemctl status redis.service   #查看服务当前状态
systemctl enable redis.service   #设置开机自启动
systemctl disable redis.service   #停止开机自启动

问题列表
1.Redis (error) NOAUTH Authentication required.解决方法
认证问题 设置了认证密码 输入密码可以 密码是字符串形式!
auth "yourpassword"  
密码是 yourpassword
 
2.设置Redis最大占用内存
Redis需要设置最大占用内存吗?
设置Redis最大占用内存
Redis设置最大占用内存 打开redis配置文件 设置maxmemory参数 maxmemory是bytes字节类型
# In short... if you have slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
# maxmemory <bytes>
maxmemory 268435456

本机服务器redis配置文件路径
/usr/local/redis/etc/redis.conf
由于本机服务器内存只有1G 推荐Redis设置内存为最大物理内存的四分之三 所以设置0.75G 换成byte是751619276
在CentOS下输入命令
find / -name redis
查找redis目录
# find / -name redis
Redis使用超过设置的最大值
如果Redis的使用超过了设置的最大值会怎样 故意把最大值设为1个byte试试。
# output buffers (but this is not needed if the policy is 'noeviction').
# maxmemory <bytes>
maxmemory 1
开debug模式下的页面
提示错误:OOM command not allowed when used memory > ‘maxmemory’
设置了maxmemory的选项 redis内存使用达到上限
可以通过设置LRU算法来删除部分key,释放空间
默认是按照过期时间的,如果set时候没有加上过期时间就会导致数据写满maxmemory
如果不设置maxmemory或设置为0
64位系统不限制内存
32位系统最多使用3GB内存
LRU是Least Recently Used 近期最少使用算法
volatile-lru -> 根据LRU算法生成的过期时间来删除。
allkeys-lru -> 根据LRU算法删除任何key。
volatile-random -> 根据过期设置来随机删除key。
allkeys->random -> 无差别随机删。
volatile-ttl -> 根据最近过期时间来删除(辅以TTL)
noeviction -> 谁也不删,直接在写操作时返回错误。
如果设置了maxmemory 一般都要设置过期策略
打开Redis的配置文件有如下描述
Redis有六种过期策略
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
打开配置文件 添加如下一行 用 volatile-lru 过期策略
maxmemory-policy volatile-lru
保存文件退出 重启redis服务
info命令查看Redis内存使用情况
如服务器Redis所在目录:/usr/local/redis-3.0.7/src
在终端输入./redis-cli,打开Redis客户端,输入info命令。
# ./redis-cli
127.0.0.1:6379> info
# Server
# Clients
# Memory
used_memory:816232
used_memory_human:797.10K
used_memory_rss:7655424
used_memory_peak:816232
used_memory_peak_human:797.10K
used_memory_lua:36864
mem_fragmentation_ratio:9.38
mem_allocator:jemalloc-3.6.0
# Persistence
# Stats
# Replication
# CPU
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=1,expires=1,avg_ttl=425280
其中
used_memory:816232,仅用了0.7M左右