Redis可以直接在机器上安装也可以用docker镜像安装。这里直接在机器上安装,对于Docker镜像下载和运行在基础配置掌握后更容易理解,我们在以后的章节中介绍。
我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。
安装环境
我这里采用的是Centos 7的环境
[root@testmachine redis]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
安装前提条件
因为Redis是基于C语言的,所以首先需要gcc环境
[root@testmachine redis]# yum -y install gcc automake autoconf libtool make
安装步骤
我们从官网下载最新稳定版的源码压缩包,然后解压安装。源码及解压包我们放在~/redis/
目录,安装后的二进制文件我们放在/usr/local/redis/
目录。
- 官网下载最新稳定版本安装包
直接在官网的下载页找到Stable版本进行下载,本博客完成之时最新稳定版本为5.0.7
[root@testmachine redis]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
- 解压安装包
[root@testmachine redis]# tar -zxvf redis-5.0.7.tar.gz
解压后目录~/redis
的结构如下
[root@testmachine redis]# ll
total 1940
drwxrwxr-x 6 root root 334 Nov 20 01:05 redis-5.0.7
-rw-r--r-- 1 root root 1984203 Nov 20 01:06 redis-5.0.7.tar.gz
- 进入解压包先进行编译
[root@testmachine redis-5.0.7]# make MALLOC=libc
出现下面的画面说明编译成功,如果编译有问题说明上面的gcc环境安装的有问题
Hint: It's a good idea to run 'make test' ;)
- 安装到指定目录
[root@testmachine redis-5.0.7]# make install PREFIX=/usr/local/redis
如果目标目录不存在会被自动创建。
进入安装目标目录,发现只有一个存储二进制文件的bin
目录
[root@testmachine redis-5.0.7]# cd /usr/local/redis
[root@testmachine redis]# ll
total 0
drwxr-xr-x 2 root root 134 Feb 4 23:07 bin
bin
目录下有如下文件
[root@testmachine redis]# cd bin
[root@testmachine bin]# ll
total 13024
-rwxr-xr-x 1 root root 353864 Feb 4 23:07 redis-benchmark
-rwxr-xr-x 1 root root 4058472 Feb 4 23:07 redis-check-aof
-rwxr-xr-x 1 root root 4058472 Feb 4 23:07 redis-check-rdb
-rwxr-xr-x 1 root root 799360 Feb 4 23:07 redis-cli
lrwxrwxrwx 1 root root 12 Feb 4 23:07 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 4058472 Feb 4 23:07 redis-server
各个文件的说明如下
命令 | 说明 |
---|---|
redis-benchmark | redis性能测试工具 |
redis-check-aof | AOF文件修复工具 |
redis-check-rdb | RDB文件修复工具 |
redis-cli | redis命令行客户端 |
redis.conf | redis配置文件 |
redis-sentinal | redis集群管理工具 |
redis-server | redis服务进程 |
其中,启动服务用到的时redis-server
命令,要连接服务用到的时redis-cli
客户端
默认方式启动服务
只用作体验,不要用在生产环境!!
如果只是简单体验下redis,我们可以直接利用上面的redis-server
命令启动一个服务。不过需要注意的是,这时候并不是以daemon方式启动,所以启动后当前的terminal就被占用了,需要提前准备另一个terminal去运行客户端。
[root@testmachine bin]# ./redis-server
出现下面画面说明运行成功
21325:C 04 Feb 2020 23:20:07.837 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
21325:C 04 Feb 2020 23:20:07.837 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=21325, just started
21325:C 04 Feb 2020 23:20:07.837 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
21325:M 04 Feb 2020 23:20:07.839 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.7 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 21325
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
21325:M 04 Feb 2020 23:20:07.840 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
21325:M 04 Feb 2020 23:20:07.840 # Server initialized
21325:M 04 Feb 2020 23:20:07.840 # 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.
21325:M 04 Feb 2020 23:20:07.840 # 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.
21325:M 04 Feb 2020 23:20:07.840 * Ready to accept connections
可以看到redis服务端监听的是默认的6379端口,我们在另一个terminal运行客户端
[root@testmachine bin]# ./redis-cli
127.0.0.1:6379>
发现直接连上了本地的6379端口,输入quit
退出客户端
自定义配置启动服务
当然生产环境的服务必须要以daemon的方式后台运行,并且还需要对端口等等参数进行优化,这些都可以通过修改启动配置文件来实现。
在前面的安装解压包里面将默认配置文件redis.conf
拷贝到/usr/local/redis/
目录下,以后我们就以这个文件作为启动配置文件。
[root@testmachine redis-5.0.7]# cp redis.conf /usr/local/redis/
这里我们只是简单修改一个配置项做为演示,更详细的配置文件详解我们在下一篇来详细介绍。
[root@testmachine redis]# cat redis.conf | grep ^daemon
daemonize yes
可以看到我们把daemonize no
修改为了daemon yes
,也就是以daemon方式启动。
指定配置文件的启动格式为
./redis-server /path/to/redis.conf
这里我们启动如下
[root@testmachine bin]# ./redis-server /usr/local/redis/redis.conf
21916:C 04 Feb 2020 23:37:59.590 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
21916:C 04 Feb 2020 23:37:59.590 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=21916, just started
21916:C 04 Feb 2020 23:37:59.590 # Configuration loaded
通过检查进程和端口都能知道进程已经成功起来
[root@testmachine bin]# ps aux | grep redis
root 21917 0.1 0.1 144016 2040 ? Ssl 23:37 0:00 ./redis-server 127.0.0.1:6379
root 21930 0.0 0.0 112704 972 pts/1 R+ 23:38 0:00 grep --color=auto redis
[root@testmachine bin]# netstat -ntlp | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 21917/./redis-serve
通过运行客户端来正确关掉redis以确保数据不会丢失
[root@testmachine bin]# ./redis-cli shutdown
设置服务以及开机启动
centos采用的是systemd来进行系统初始化的,原理这里就不赘述了,我们直接创建service文件/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/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
检查下redis这个服务的状态
[root@testmachine bin]# systemctl status redis
● redis.service - redis-server
Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: disabled)
Active: inactive (dead)
启动一下服务
[root@testmachine bin]# systemctl start redis
再检查就发现服务已经被启动
[root@testmachine bin]# systemctl status redis
● redis.service - redis-server
Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2020-02-04 23:46:38 +08; 5s ago
Process: 22111 ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf (code=exited, status=0/SUCCESS)
Main PID: 22113 (redis-server)
Tasks: 4
Memory: 1.0M
CGroup: /system.slice/redis.service
└─22113 /usr/local/redis/bin/redis-server 127.0.0.1:6379
Feb 04 23:46:38 testmachine systemd[1]: Starting redis-server...
Feb 04 23:46:38 testmachine systemd[1]: Started redis-server.
Feb 04 23:46:38 testmachine redis-server[22111]: 22111:C 04 Feb 2020 23:46:38.103 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
Feb 04 23:46:38 testmachine redis-server[22111]: 22111:C 04 Feb 2020 23:46:38.104 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=22111, just started
Feb 04 23:46:38 testmachine redis-server[22111]: 22111:C 04 Feb 2020 23:46:38.104 # Configuration loaded
将服务设置为开机启动
[root@testmachine bin]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.
检查一下
oot@testmachine bin]# systemctl list-unit-files | grep redis
redis.service enabled
总结
这一篇我们成功安装了redis,并且知道了如何指定配置文件来进行启动。为了方便我们还将redis服务加入到了systemd。准备工作已经就绪,下一篇我们就一起来看看redis的配置文件都有哪些值得关注的配置项。