Redis是msdp系统依赖的缓存工具,用于快速存取数据。
Centos安装Redis
从Redis官网下载最新版本。https://redis.io/download。
编译
以4.0.8版本为例:
tar xzf redis-4.0.8.tar.gz
cd redis-4.0.8
make
make install
编译后在redis-4.0.8/src文件夹中可以找到若干个可执行程序。安装完后,在/usr/local/bin目录中可以找到刚刚安装的redis可执行文件。
ls /usr/local/bin/redis-*
显示
/usr/local/bin/redis-benchmark
/usr/local/bin/redis-check-rdb
/usr/local/bin/redis-sentinel
/usr/local/bin/redis-check-aof
/usr/local/bin/redis-cli
/usr/local/bin/redis-server
配置redis服务和开机启动
cd redis-4.0.8/utils
sudo ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/bin/redis-server
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service…
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!
在Please select the redis executable path []的时候输入/usr/local/bin/redis-server,其他选项都可以默认。
启动
service redis_6379 start
停止
service redis_6379 stop
也可以使用ps -ef | grep redis查出redis的pid,然后kill pid关闭redis。
基本操作
redis-cli
redis 127.0.0.1:6379> info #查看server版本内存使用连接等信息
redis 127.0.0.1:6379> client list #获取客户连接列表
redis 127.0.0.1:6379> client kill 127.0.0.1:33441 #终止某个客户端连接
redis 127.0.0.1:6379> dbsize #当前保存key的数量
redis 127.0.0.1:6379> save #立即保存数据到硬盘
redis 127.0.0.1:6379> bgsave #异步保存数据到硬盘
redis 127.0.0.1:6379> flushdb #当前库中移除所有key
redis 127.0.0.1:6379> flushall #移除所有key从所有库中
redis 127.0.0.1:6379> lastsave #获取上次成功保存到硬盘的unix时间戳
redis 127.0.0.1:6379> monitor #实时监测服务器接收到的请求
redis 127.0.0.1:6379> slowlog len #查询慢查询日志条数
(integer) 3
redis 127.0.0.1:6379> slowlog get #返回所有的慢查询日志,最大值取决于slowlog-max-len配置
redis 127.0.0.1:6379> slowlog get 2 #打印两条慢查询日志
redis 127.0.0.1:6379> slowlog reset #清空慢查询日志信息
配置密码
停止服务:
service redis_6379 stop
编辑redis配置文件:
vim /etc/redis/6379.conf
找到# requirepass foobared,大概在501行,去掉注释,改为requirepass ecp。
编辑redis服务脚本:
vim /etc/init.d/redis_6379
找到$CLIEXEC -p
R
E
D
I
S
P
O
R
T
s
h
u
t
d
o
w
n
,
大
概
在
43
行
,
改
为
REDISPORT shutdown,大概在43行,改为
REDISPORTshutdown,大概在43行,改为CLIEXEC -a ecp -p $REDISPORT shutdown。
启动服务
service redis_6379 start、
停止服务:
ps -ef|grep redis kill -9 XXX
或者指定密码 -a 指定ip -h 指定端口 -p
./redis-cli -a 密码 -h ip -p 端口 shutdown
特别注意redis启停
启动
cd /home/app/redis-3.2.8/bin
./redis-server ./redis.config
哨兵启动
./redis-server.exe ./sentinel.conf --sentinel
停止服务:
ps -ef|grep redis kill -9 XXX
或者指定密码 -a 指定ip -h 指定端口 -p
./redis-cli -a 密码 -h ip -p 端口 shutdown