一、CentOS7下安装redis
1、下载redis安装包
wget http://download.redis.io/releases/redis-4.0.6.tar.gz
[root@ZYL local]# wget http://download.redis.io/releases/redis-4.0.6.tar.gz
--2019-12-07 09:27:50-- http://download.redis.io/releases/redis-4.0.6.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1723533 (1.6M) [application/x-gzip]
Saving to: ‘redis-4.0.6.tar.gz’
100%[=====================================================================================================>] 1,723,533 102KB/s in 16s
2019-12-07 09:28:08 (103 KB/s) - ‘redis-4.0.6.tar.gz’ saved [1723533/1723533]
2、解压压缩包
tar xf redis-4.0.6.tar.gz
[root@ZYL local]# tar xf redis-4.0.6.tar.gz
3、yum安装gcc依赖
yum install gcc
[root@ZYL local]# yum -y install gcc
4、进入到redis的解压目录下
cd redis-4.0.6
[root@ZYL local]# cd redis-4.0.6
5、编译
make
[root@ZYL redis-4.0.6]# make
cd src && make all
......
6、安装
将/usr/local/redis-4.0.6/src目录下的文件加到/usr/local/bin目录
cd src && make install
[root@ZYL redis-4.0.6]# cd src && make install
CC Makefile.dep
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
二、启动redis的三种方式
1、直接启动redis
先切换到redis的src目录下,我的src目录:/usr/local/redis-4.0.6/src/
1)、启动redis服务端
./redis-server
[root@ZYL src]# ./redis-server
34240:C 07 Dec 09:55:05.304 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
34240:C 07 Dec 09:55:05.304 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=34240, just started
34240:C 07 Dec 09:55:05.304 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
34240:M 07 Dec 09:55:05.306 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 34240
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
34240:M 07 Dec 09:55:05.308 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
如上图:redis已经启动成功,但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。按 ctrl + c可以关闭窗口。
1)、进入redis客户端
./redis-cli
[root@ZYL src]# ./redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]>
2、以后台进程方式启动redis
1)、在redis目录下修改redis.conf配置文件
将daemonize no修改为daemonize yes
[root@ZYL redis-4.0.6]# vi redis.conf
2)、指定redis.conf文件启动
./redis-server /usr/local/redis-4.0.6/redis.conf
[root@ZYL redis-4.0.6]# ./redis-server /usr/local/redis-4.0.6/redis.conf
18713:C 13 Dec 13:07:41.109 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18713:C 13 Dec 13:07:41.109 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=18713, just started
18713:C 13 Dec 13:07:41.109 # Configuration loaded
3)、关闭redis进程
首先使用ps -aux | grep redis查看redis进程
[root@ZYL src]# ps -aux | grep redis
root 34240 0.1 0.2 145312 7964 pts/0 Sl+ 09:55 0:01 ./redis-server *:6379
root 34270 0.2 0.0 126416 2040 pts/2 T 09:57 0:00 vi redis.conf
root 34290 0.0 0.0 112712 960 pts/2 S+ 10:03 0:00 grep --color=auto redis
使用kill命令杀死进程
[root@ZYL src]# kill 34240
[root@ZYL src]# ps -aux | grep redis
root 34270 0.2 0.0 126416 2040 pts/2 T 09:57 0:00 vi redis.conf
root 34293 0.0 0.0 112712 960 pts/2 S+ 10:04 0:00 grep --color=auto redis
3、设置redis开机自启动
1)、在/etc目录下新建redis目录
mkdir redis
[root@ZYL etc]# mkdir redis
2)、将/usr/local/redis-4.0.6/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf
[root@ZYL etc]# cp /usr/local/redis-4.0.6/redis.conf /etc/redis/6379.conf
3)、将redis的启动脚本redis_init_script复制一份放到/etc/init.d目录下
[root@ZYL redis]# cp /usr/local/redis-4.0.6/utils/redis_init_script /etc/init.d/
4)、设置redis开机自启动
先切换到/etc/init.d目录下,然后执行自启命令
[root@ZYL redis]# chkconfig redisd on
service redisd does not support chkconfig
能够看到redisd不支持chkconfig
解决方法:
使用vim编辑redis_init_script文件,在第一行加入如下两行注释,保存退出
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
再次执行开机自启命令,成功
[root@ZYL redis]# chkconfig redisd on
现在可以直接以服务的形式启动和关闭redis了
启动:
service redisd start
[root@ZYL redis]# service redisd start
Starting Redis server...
2288:C 13 Dec 13:51:38.087 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2288:C 13 Dec 13:51:38.087 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=2288, just started
2288:C 13 Dec 13:51:38.087 # Configuration loaded
关闭:
方法1:service redisd stop
[root@ZYL redis]# service redisd stop
Stopping ...
Redis stopped
方法2:redis-cli SHUTDOWN
三、参考资料
1、http://blog.csdn.net/zc474235918/article/details/50974483
2、http://blog.csdn.net/gxw19874/article/details/51992125