Linux下Redis安装配置
旧文。整理发布
系统:Centos7
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
下载
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SWPH8Jb5-1571123773689)(Linux下Redis安装配置.assets/1571109558514.png)]
本文下载最新稳定版本:http://download.redis.io/releases/redis-5.0.5.tar.gz
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DipYJRVD-1571123773690)(Linux下Redis安装配置.assets/1571109619718.png)]
下载获得redis-xxx.Tar.gz后将它放入我们的Linux目录/opt
解压
[root@localhost opt]# pwd
/opt
[root@localhost opt]# ls
redis-5.0.5.tar.gz
[root@localhost opt]# tar -zxvf redis-5.0.5.tar.gz
[root@localhost opt]# cd redis-5.0.5
[root@localhost redis-5.0.5]# ll
总用量 264
-rw-rw-r--. 1 root root 106874 5月 16 00:07 00-RELEASENOTES
-rw-rw-r--. 1 root root 53 5月 16 00:07 BUGS
-rw-rw-r--. 1 root root 2381 5月 16 00:07 CONTRIBUTING
-rw-rw-r--. 1 root root 1487 5月 16 00:07 COPYING
drwxrwxr-x. 6 root root 124 5月 16 00:07 deps
-rw-rw-r--. 1 root root 11 5月 16 00:07 INSTALL
-rw-rw-r--. 1 root root 151 5月 16 00:07 Makefile
-rw-rw-r--. 1 root root 6888 5月 16 00:07 MANIFESTO
-rw-rw-r--. 1 root root 20555 5月 16 00:07 README.md
-rw-rw-r--. 1 root root 61797 5月 16 00:07 redis.conf
-rwxrwxr-x. 1 root root 275 5月 16 00:07 runtest
-rwxrwxr-x. 1 root root 280 5月 16 00:07 runtest-cluster
-rwxrwxr-x. 1 root root 341 5月 16 00:07 runtest-moduleapi
-rwxrwxr-x. 1 root root 281 5月 16 00:07 runtest-sentinel
-rw-rw-r--. 1 root root 9710 5月 16 00:07 sentinel.conf
drwxrwxr-x. 3 root root 4096 5月 16 00:07 src
drwxrwxr-x. 11 root root 182 5月 16 00:07 tests
drwxrwxr-x. 8 root root 4096 5月 16 00:07 utils
备份配置文件:
[root@localhost redis-5.0.5]# cp redis.conf redis.conf.backup
[root@localhost redis-5.0.5]# ls
00-RELEASENOTES deps README.md runtest-cluster src
BUGS INSTALL redis.conf runtest-moduleapi tests
CONTRIBUTING Makefile redis.conf.backup runtest-sentinel utils
COPYING MANIFESTO runtest sentinel.conf
[root@localhost redis-5.0.5]#
修改配置文件redis.conf
-
daemonize no 改成 yes,让服务在后台启动
# By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. daemonize yes
安装
在redis目录下执行make
命令
[root@localhost redis-5.0.5]# make
可能出现的错误:
如果执行
make
命令不成功,错误提示为gcc未安装,则需要
1、 联网使用yum install gcc-c++
命令安装
2、 gcc安装完成后,再一次make
命令之后
3、 如果报错:会报错Jemalloc/jemalloc.h:没有那个文件或目录,可以执行
。需要进行第四步
4、解决办法—>运行make distclean
之后再make
重要提示:Redis Test(可以不用执行),不然会很麻烦。
make完成后继续执行make install
[root@localhost redis-5.0.5]# make install
cd src && make install
make[1]: 进入目录“/opt/redis-5.0.5/src”
CC Makefile.dep
make[1]: 离开目录“/opt/redis-5.0.5/src”
make[1]: 进入目录“/opt/redis-5.0.5/src”
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: 离开目录“/opt/redis-5.0.5/src”
redis 默认安装路径 是/usr/local/bin
[root@localhost /]# cd /usr/local/bin/
[root@localhost bin]# ll
总用量 32748
-rwxr-xr-x. 1 root root 4367344 10月 15 11:38 redis-benchmark
-rwxr-xr-x. 1 root root 8117880 10月 15 11:38 redis-check-aof
-rwxr-xr-x. 1 root root 8117880 10月 15 11:38 redis-check-rdb
-rwxr-xr-x. 1 root root 4807680 10月 15 11:38 redis-cli
lrwxrwxrwx. 1 root root 12 10月 15 11:38 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 8117880 10月 15 11:38 redis-server
[root@localhost bin]#
/usr/local/bin
下文件介绍
redis-benchmark
: 性能测试工具redis-check-aof
:修复有问题的AOF文件redis-check-rdb
:修复有问题的dump.rdb文件redis-sentinel
:redis集群使用redis-server
:redis服务器启动命令redis-cli
: 客户端,操作入口
启动
redis-server
可启动redis服务,可以指定启动配置文件
[root@localhost bin]# redis-server /opt/redis-5.0.5/redis.conf
6327:C 15 Oct 2019 11:42:58.658 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6327:C 15 Oct 2019 11:42:58.658 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=6327, just started
6327:C 15 Oct 2019 11:42:58.658 # Configuration loaded
[root@localhost bin]#
启动客户端redis-cli
默认端口号是6379
[root@localhost bin]# redis-cli -p 6379
127.0.0.1:6379> keys 8
(empty list or set)
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> quit
查看进程:ps -ef | grep redis
[root@localhost bin]# ps -ef | grep redis
root 6328 1 0 11:42 ? 00:00:00 redis-server 127.0.0.1:6379
root 6337 1238 0 11:47 pts/0 00:00:00 grep --color=auto redis
[root@localhost bin]#
以上就是入门级别安装了,关于如何配置redis及其他的高级用法,请查阅相关文档。
相关的操作命令可以参考 http://redisdoc.com/>
实战系列
redis 远程连接
修改配置文件redis.conf
bind 0.0.0.0
# 在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,还是无法访问
protected-mode no
开放端口号6379
本文直接把防火墙关闭了
[root@localhost bin]# systemctl stop firewalld.service
关闭redis
[root@localhost bin]# pwd
/usr/local/bin
[root@localhost bin]# redis-cli -h 127.0.0.1 -p 6379 shutdown
# 测试是否关闭
[root@localhost bin]# redis-cli -h 127.0.0.1 -p 6379
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
重启redis
[root@localhost bin]# redis-server /opt/redis-5.0.5/redis.conf
6644:C 15 Oct 2019 14:01:18.754 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6644:C 15 Oct 2019 14:01:18.754 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=6644, just started
6644:C 15 Oct 2019 14:01:18.754 # Configuration loaded
[root@localhost bin]#
测试成功
redis-cli -h 192.168.113.128 -p 6379
192.168.113.128:6379>
客户端推荐:Redis Desktop Manager
- Redis Desktop Manager是一款适用于Mac OS X,Windows和Linux的GUI应用程序。
设置redis密码
修改配置文件参数 requirepass
默认没有设置密码
################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
设置密码为 redis
requirepass redis
- 测试 如下
# 关闭
[root@localhost redis-5.0.5]# redis-cli
127.0.0.1:6379> shutdown save
not connected> quit
[root@localhost redis-5.0.5]# redis-server ./redis.conf
5932:C 25 Oct 2019 14:57:40.437 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5932:C 25 Oct 2019 14:57:40.437 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=5932, just started
5932:C 25 Oct 2019 14:57:40.437 # Configuration loaded
# 启动
[root@localhost redis-5.0.5]# redis-cli -p 6379
127.0.0.1:6379> get
(error) ERR wrong number of arguments for 'get' command
127.0.0.1:6379> get k
(error) NOAUTH Authentication required.
# 输入密码
127.0.0.1:6379> auth redis
OK
127.0.0.1:6379> get k
(nil)
127.0.0.1:6379>
设置redis为服务
redis为我们提供了一个很方便的脚本注册为服务,位于utils/install_server.sh
[root@localhost utils]# ./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] 6379
# 指定redis服务的配置文件
Please select the redis config file name [/etc/redis/6379.conf] /opt/redis/redis-5.0.5/redis.conf
# 指定服务的日志文件
Please select the redis log file name [/var/log/redis_6379.log] /opt/redis/redis-5.0.5/log/redis.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 : /opt/redis/redis-5.0.5/redis.conf
Log file : /opt/redis/redis-5.0.5/log/redis.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!
[root@localhost utils]#
下面我们验证服务是否存在了:
[root@localhost utils]# chkconfig --list | grep redis
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
redis_6379 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@localhost utils]#
# 查看redis的服务状态
[root@localhost utils]# systemctl status redis_6379
● redis_6379.service - LSB: start and stop redis_6379
Loaded: loaded (/etc/rc.d/init.d/redis_6379; bad; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
[root@localhost utils]#
可以看到服务中有redis了
# 使用服务启动redis
[root@localhost redis-5.0.5]# ps -ef | grep redis
root 6317 1305 0 15:15 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-5.0.5]# systemctl start redis_6379
[root@localhost redis-5.0.5]# ps -ef | grep redis
root 6326 1 0 15:15 ? 00:00:00 /usr/local/bin/redis-server 0.0.0.0:6379
root 6331 1305 0 15:15 pts/0 00:00:00 grep --color=auto redis
[root@localhost redis-5.0.5]#
修改redis服务方法,修改 :etc/init.d/redis_6379
文件即可
redis压测示例
100个并发 共1000个请求
redis-benchmark -h 192.168.0.14 -p 6379 -c 100 -n 1000
存取大小为100字节的数据包。
clldeMacBook-Pro:~ cll$ redis-benchmark -h 192.168.0.14 -p 6379 -q -d 100
PING_INLINE: 13544.63 requests per second
PING_BULK: 12963.44 requests per second
SET: 12733.99 requests per second
GET: 12588.12 requests per second
INCR: 12645.42 requests per second
LPUSH: 11906.18 requests per second
RPUSH: 12632.64 requests per second
LPOP: 13289.04 requests per second
RPOP: 13283.74 requests per second
SADD: 13370.77 requests per second
HSET: 13231.01 requests per second
SPOP: 12451.75 requests per second
LPUSH (needed to benchmark LRANGE): 12583.37 requests per second
LRANGE_100 (first 100 elements): 13297.87 requests per second
LRANGE_300 (first 300 elements): 13075.31 requests per second
LRANGE_500 (first 450 elements): 13308.49 requests per second
LRANGE_600 (first 600 elements): 13175.23 requests per second
MSET (10 keys): 13225.76 requests per second
只测试某些操作的性能
clldeMacBook-Pro:~ cll$ redis-benchmark -h 192.168.0.14 -p 6379 -t set,lpush -n 10000 -q
SET: 13793.10 requests per second
LPUSH: 13404.83 requests per second
只测试某些数值存取的功能
clldeMacBook-Pro:~ cll$ redis-benchmark -h 192.168.0.14 -p 6379 -n 10000 -q script load "redis.call('set','sss','ddd')"
script load redis.call('set','sss','ddd'): 13812.16 requests per second