CentOS 7下安装redis 4.0.6

安装redis

第一步:下载redis安装包

wget http://download.redis.io/releases/redis-4.0.6.tar.gz

第二步:解压压缩包

tar -zxvf redis-4.0.6.tar.gz

第三步:yum安装gcc依赖

yum install gcc

遇到选择,输入y即可

第四步:跳转到redis解压目录下

cd redis-4.0.6

第五步:编译

make

安装,官网并没有这个操作,执行这个的目的就是将可执行文件拷贝到/usr/local/bin/目录下,这样就可以直接执行redis-server 、redis-cli 等命令了。

make install

安装完成,查看版本

[root@localhost redis-4.0.6]# redis-server -v
Redis server v=4.0.6 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=4f1a60f0053726b7

启动 redis-server

[root@localhost redis-4.0.6]# redis-server 
9961:C 12 Dec 05:14:58.101 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9961:C 12 Dec 05:14:58.101 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=9961, just started
9961:C 12 Dec 05:14:58.101 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 9961
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

9961:M 12 Dec 05:14:58.105 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9961:M 12 Dec 05:14:58.105 # Server initialized
9961:M 12 Dec 05:14:58.105 # 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.
9961:M 12 Dec 05:14:58.105 # 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.
9961:M 12 Dec 05:14:58.105 * Ready to accept connections

看到以上页面就启动成功啦,我们新开一个终端去连接redis。

[root@localhost ~]# redis-cli
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> 

可有些童鞋就疑惑了,为什么运行redis-server后,不能退出后台运行呢,如果关闭终端,不就连redis一起关闭了吗?
请继续往下看……

后台运行

拷贝redis.conf配置文件到/etc/redis

在刚解压的redis根目录执行:

[root@localhost redis-4.0.6]# mkdir /etc/redis
[root@localhost redis-4.0.6]# cp redis.conf /etc/redis/

编辑配置文件

[root@localhost redis-4.0.6]# vim /etc/redis/6379.conf

找到daemonize no 改为daemonize 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

[root@localhost redis-4.0.6]# redis-server  /etc/redis/6379.conf 
9999:C 12 Dec 05:31:11.872 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9999:C 12 Dec 05:31:11.872 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=9999, just started
9999:C 12 Dec 05:31:11.872 # Configuration loaded
[root@localhost redis-4.0.6]# 

是不是可以后台启动了呢?

开机启动

1、将redis的启动脚本复制一份放到/etc/init.d目录下

[root@localhost redis-4.0.6]#  cp utils/redis_init_script /etc/init.d/redisd

2、设置redis开机自启动

先切换到/etc/init.d目录下

然后执行自启命令

[root@localhost redis-4.0.6]# chkconfig redisd on
service redisd does not support chkconfig 

看结果是redisd不支持chkconfig

解决方法:

使用vim编辑redisd文件,在第一行加入如下两行注释,保存退出

# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database

注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。

再次执行开机自启命令,成功

[root@localhost redis-4.0.6]# chkconfig redisd on

现在可以直接已服务的形式启动和关闭redis了

启动:

service redisd start 

[root@localhost redis-4.0.6]# 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

关闭:

service redisd stop

[root@localhost redis-4.0.6]# service redisd stop
Stopping ...
Redis stopped

关于bind

翻看网上的文章,此处多翻译为“指定redis只接收来自于该IP地址的请求,如果不进行设置,那么将处理所有请求,在生产环境中最好设置该项”。这种解释会totally搞糊涂初学者,甚至是错误的。该处的英文原文为

# If you want you can bind a single interface, if the bind option is not 
# specified all the interfaces will listen for incoming connections. 
# bind 127.0.0.1

该处说明bind的是interface,也就是说是网络接口。服务器可以有一个网络接口(通俗的说网卡),或者多个。打个比方说机器上有两个网卡,分别为192.168.1.5 和192.168.1.6,如果bind 192.168.1.5,那么只有该网卡地址接受外部请求,如果不绑定,则两个网卡口都接受请求。

OK,不知道讲清楚没有,在举一个例子。在我上面的实验过程中,我是将bind项注释掉了,实际上我还有一种解决方案。由于我redis服务器的地址是 192.168.1.111 。如果我不注释bind项,还有什么办法呢?我可以做如下配置:

# bind 192.168.1.111

这里很多人会误以为绑定的ip应该是请求来源的ip。其实不然,这里应该绑定的是你redis服务器本身接受请求的ip。

猜你喜欢

转载自blog.csdn.net/uisoul/article/details/85231803