首先创建安装目录
# mkdir /usr/local/redis
下载redis压缩包
wget http://download.redis.io/releases/redis-4.0.2.tar.gz
解压redis的压缩文件
tar -zxvf redis-4.0.2.tar.gz
进入安装文件夹进行编译
[root@bruce-host redis-4.0.2]# cd src/
[root@bruce-host src]# make install
安装成功
启动redis服务器,使用默认配置载入
[root@bruce-host src]# ./redis-server
载入制定配置来启动redis
[root@bruce-host src]# ./redis-server /usr/local/redis/redis-4.0.2/redis.conf
出现上图说明安装成功,但是有可能会导致远程连接有问题。对于服务器上的redis配置需要进行以下配置
[root@bruce-host src]# vim ../redis.conf
把protected-mode yes改为protected-mode no(在没有密码的情况下,关闭保护模式)
注释掉bind 127.0.0.1 (取消绑定本地地址)
把daemonize no改为daemonize yes (是否为进程守护,关闭ssh窗口后即是否在后台继续运行)
然后重启,进行数据测试
[root@bruce-host src]# ./redis-server /usr/local/redis/redis-4.0.2/redis.conf
[root@bruce-host src]# ./redis-cli
127.0.0.1:6379> set ball red
OK
127.0.0.1:6379> get ball
"red"
127.0.0.1:6379>
其中可能遇到的问题有
1.Could not connect to Redis at 127.0.0.1:6379: Connection refused
原因是:在安装好redis扩展 尝试连接redis时,客户端打不开,原因是需要先开启服务端,
即需要先开启redis-server,才能进行redis-cli操作
2.redis基本操作
./redis-server //启动redis服务器
./redis-server /usr/local/redis/redis-4.0.2/redis.conf //启动服务器时加载指定配置
./redis-cli -h 127.0.0.1 -p 6379 //操作端口号为6379的服务器
./redis-cli -h 127.0.0.1 -p 6379 -a root //当有密码时,开启redis的命令
3.如何设置redis密码
[root@bruce-host src]# vim ../redis.conf
把requirepass foobared的注释去掉,将foobared改为需要的密码
把protected-mode no改为protected-mode yes(在有密码的情况下,打开保护模式)
4.如何登录、退出redis
//登录
[root@bruce-host src]# ./redis-server /usr/local/redis/redis-4.0.2/redis.conf
14405:C 13 Apr 15:16:49.806 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14405:C 13 Apr 15:16:49.806 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=14405, just started
14405:C 13 Apr 15:16:49.806 # Configuration loaded
[root@bruce-host src]# ./redis-cli
[root@bruce-host src]# ps -ef | grep redis
root 13437 1 0 14:17 ? 00:00:01 ./redis-server *:6379
root 13825 8950 0 14:45 pts/2 00:00:00 grep --color=auto redis
//退出
[root@bruce-host src]# ./redis-cli shutdown
[root@bruce-host src]# ps -ef|grep redis //查看状态
root 14379 14246 0 15:15 pts/0 00:00:00 grep --color=auto redis
5.有密码之后如何登录、退出redis
[root@bruce-host src]# ./redis-cli -h 127.0.0.1 -p 6379 -a root //ip和端口号选填 auth必填
[root@bruce-host src]# ./redis-cli -h 127.0.0.1 -p 6379 -a root shutdown //ip和端口号选填 auth必填
6.linux服务器端可以连接而远程无法连接的原因
首先# ps -ef|grep redis查看6379前面是否为* 若为127.0.0.1则被限制在本地访问
在redis-conf注释掉bind 127.0.0.1 (取消绑定本地地址)
若还不能访问 则开启制定端口号或者关闭防火墙,在这采用关闭防火墙,由于是centos7,防火墙进行了升级
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动