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
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左右
文人墨客
Windows 安装 Redis(.msi)
Github 下载地址:https://github.com/MicrosoftArchive/redis/tags
下载的时候下载 msi 安装文件:
1.首先双击现在完的安装程序
2.点击 next
3.点击接受,继续 next
4.设置 Redis 的服务端口,默认为 6379,默认就好,单击 next
5.选择安装的路径,并且打上勾(这个非常重要),添加到 path 是把 Redis 设置成 windows 下的服务,不然你每次都要在该目录下启动命令 redis-server redis.windows.conf,但是只要一关闭 cmd 窗口,redis 就会消失,这样就比较麻烦。
6.设置 Max Memory,然后 next 进入安装
如果 redis 的应用场景是作为数据库使用,那不要设置这个选项,因为数据库是不能容忍丢失数据的。
如果作为临时缓存那就得看自己的需要(我这里设置了 1024M 的最大内存限制)
指定 Redis 最大内存限制,Redis 在启动时会把数据加载到内存中,达到最大内存后,Redis 会先尝试清除已到期或即将到期的 Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis 新的 vm 机制,会把 Key 存放内存,Value 会存放在 swap区。
7.安装完成
8.测试所安装的Redis
如果你是和我一样通过msi文件的安装,你可以在 “计算机管理→服务与应用程序→服务” 看到Redis正在运行
你也可以将它停止(不停止会出现错误代码为 18012 的错误,表示本机端口 6379 被占用),然后在 cmd 窗口进入Redis的安装路径的根目录
输入命令 redis-server.exe redis.windows.conf,出现下图证明 Redis 服务启动成功:
下面进行测试:
你可以在 Redis 的安装根目录下找到 redis-cli.exe 文件启动(我用的是这种方法),或在 cmd 中先进入 Redis 的安装根目录用命令 redis-cli.exe -h 192.168.10.61 -p 6379(注意换成自己的 IP,本地可以是 127.0.0.1)的方式打开
测试方法:设置键值对,取出键值对(我这里键值对是 peng)
文人墨客
mac 下安装也可以使用 homebrew,homebrew 是 mac 的包管理器。
1、执行 brew install redis
2、启动 redis,可以使用后台服务启动 brew services start redis。或者直接启动:redis-server /usr/local/etc/redis.conf
文人墨客
Mac 下安装
1. 官网http://redis.io/ 下载最新的稳定版本,这里是3.2.0
2. sudo mv 到 /usr/local/
3. sudo tar -zxf redis-3.2.0.tar 解压文件
4. 进入解压后的目录 cd redis-3.2.0
5. sudo make test 测试编译
6. sudo make install