作为软件开发人员在开发系统的时候为了提高系统的响应效率采用缓存技术是必须的,现在redis是使用较多的缓存技术,这里介绍一下如何在centos7环境下配置redis服务。
Redis 安装部署
操作系统环境不用说了(需要配置好gcc环境)
1.下载redis安装文件
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
我是下载到/opt/redis目录
2.解压缩
tar -zxvf redis-4.0.11.tar.gz
3.编译安装
在安装前切记执行这句,不然会报错
<font color="red">make MALLOC=libc</font>
随后进入src目录执行,剩下的就等着就是了
make instal
安装结束了启动一下redis服务
[root@nic-redis redis-4.0.11]# ./src/redis-server
你会看到以下结果
15018:C 16 Sep 20:49:05.729 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15018:C 16 Sep 20:49:05.730 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=15018, just started
15018:C 16 Sep 20:49:05.730 # Warning: no config file specified, using the default config. In order to specify a config file use ./src/redis-server /path/to/redis.conf
15018:M 16 Sep 20:49:05.731 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 15018
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
15018:M 16 Sep 20:49:05.733 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
15018:M 16 Sep 20:49:05.733 # Server initialized
15018:M 16 Sep 20:49:05.733 # 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.
15018:M 16 Sep 20:49:05.733 # 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.
15018:M 16 Sep 20:49:05.733 * DB loaded from disk: 0.000 seconds
15018:M 16 Sep 20:49:05.733 * Ready to accept connections
说明redis已经成功完成安装
Redis配置
安装完后不可能每次要使用的时候都手动启动redis,因此我们需要做一些配置让redis能够作为后台服务在开机的时候自动启动。
1.修改redis的配置文件
[root@nic-redis redis-4.0.11]# vi redis.conf
找到
daemonize no
并将其修改为
daemonize yes
2.将src目录下面的文件同步复制到/usr/local/bin目录下
[root@nic-redis init.d]# cp /opt/redis/redis-4.0.11/src/* -r /usr/local/bin/
3.在/etc下建立redis目录
[root@nic-redis redis-4.0.11]# mkdir /etc/redis
4.复制redis.conf文件到/etc/redis目录下并重命名为6397.conf
[root@nic-redis redis-4.0.11]# cp /opt/redis/redis-4.0.11/redis.conf /etc/redis/6379.conf
5.复制redis启动脚本到/etc/init.d下并重命名为redisd
[root@nic-redis redis-4.0.11]# cp utils/redis_init_script /etc/init.d/redisd
6.设置redis开机自启
[root@nic-redis init.d]# chkconfig redisd on
7.启动测试
[root@nic-redis init.d]# service redisd start
Starting Redis server...
16065:C 16 Sep 21:12:01.590 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16065:C 16 Sep 21:12:01.590 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=16065, just started
16065:C 16 Sep 21:12:01.590 # Configuration loaded
这时候说明redis已经作为后台服务启动了
这时候看下进程
[root@nic-redis init.d]# ps -aux | grep redis
avahi 707 0.0 0.0 62240 2180 ? Ss 9月07 0:05 avahi-daemon: running [nic-redis.local]
root 16066 0.0 0.0 38240 1952 ? Ssl 21:12 0:00 /usr/local/bin/redis-server 127.0.0.1:6379
root 16259 0.0 0.0 112720 936 pts/0 S+ 21:18 0:00 grep --color=auto redis
重启一下再测试
[root@nic-redis ~]# ps -aux | grep redis
avahi 693 0.0 0.0 62236 2180 ? Ss 21:19 0:00 avahi-daemon: running [nic-redis.local]
root 1129 0.0 0.0 141828 1992 ? Ssl 21:19 0:00 /usr/local/bin/redis-server 127.0.0.1:6379
root 2005 0.0 0.0 112720 932 pts/0 S+ 21:21 0:00 grep --color=auto redis
可以看到redis已经能够开机作为后台服务自动启动了,这样我们就完成了redis的安装配置工作!
Redis 密码配置
默认的redis是没有配置密码的,因此为了安全保证我们需要配置相应的密码,redis配置密码也很简单
依然是修改redis.conf,但是这里我们已经修改为6379.conf了,将# requirepass foobared修改为requirepass xxxxxx(你的密码)
[root@nic-redis ~]# vi /etc/redis/6379.conf
修改好后
[root@nic-redis ~]# service redisd stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
[root@nic-redis ~]# service redisd start
Starting Redis server...
3659:C 16 Sep 22:11:31.768 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3659:C 16 Sep 22:11:31.769 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=3659, just started
3659:C 16 Sep 22:11:31.769 # Configuration loaded
[root@nic-redis ~]# red
red redis-check-rdb redis-server
redis-benchmark redis-cli redis-trib.rb
redis-check-aof redis-sentinel redland-db-upgrade
[root@nic-redis ~]# redis-cli
127.0.0.1:6379> key *
(error) ERR unknown command `key`, with args beginning with: `*`,
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379>
这样就完成了redis的密码配置,当然有了密码了在命令行关闭redis就不一样了没法直接通过service redisd stop关闭了会出现以下情况
[root@nic-redis ~]# service redisd stop
Stopping ...
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
这时候可以通过另外的方法来关闭
[root@nic-redis ~]# redis-cli -a 123456 shutdown
这样就能顺利的关闭redis了
Redis 远程访问配置
首先我们在服务器上访问设置一下
[root@nic-redis ~]# redis-cli
127.0.0.1:6379> keys color
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> keys color
(empty list or set)
127.0.0.1:6379> set color red
OK
127.0.0.1:6379> get color
"red"
127.0.0.1:6379>
同样的修改一下redis.conf配置文件
[root@nic-redis ~]# vi /etc/redis/6379.conf
将#bind 127.0.0.1修改为bind 0.0.0.0,意思就是允许从任意地址访问redis,当然也可以限定多个ip地址访问,多个地址之间用空格分隔,这样我们就能通过远程来访问redis了