下载地址:http://download.redis.io/releases/
tar -zxvf redis-3.2.11.tar.gz -C /解压目录/
比如:tar -zxvf redis-3.2.11.tar.gz -C /home/hadoop/app/
cd /home/hadoop/app/redis-3.2.11/
make && make install
注意:
这里直接编译可能会报错,缺少依赖包。那是因为如果虚拟机是最小化安装的话,会缺少gcc依赖(c语言的编译器(redis是用C语言编写的))
解决办法:
配置本地YUM源并安装redis依赖的rpm包:
yum -y install gcc
然后继续执行make && make install
编译安装可能还会报错,这个错误的原因是没有安装jemalloc内存分配器,可以安装jemalloc或直接输入:
sudo make MALLOC=libc && make instal
cd /home/hadoop/app/redis-3.2.11
sudo vim redis.conf
只需要修改四个选项就可以了:
daemonize yes #redis后台运行
#cluster-enabled yes #默认是没有开启集群模式,如果要开启集群模式,将注释去掉
protected-mode no #关闭保护模式,使其他机器也可以访问本机redis
appendonly yes #开启aof日志,它会每次写操作都记录一条日志
bind 192.168.1.207(本机IP)
然后还需要修改日志文件的目录:
(1).新建一个文件redis_log.log,并修改他的权限:
sudo touch /home/hadoop/app/redis-3.2.11/redis_log.log
sudo chmod 777 /home/hadoop/app/redis-3.2.11/redis_log.log
(2).新建一个目录redis_dbfile,并修改他的权限:
sudo mkdir /home/hadoop/app/redis-3.2.11/redis_dbfile
sudo chmod 777 /home/hadoop/app/redis-3.2.11/redis_dbfile
然后修改redis.conf文件:
1.将logfile ""
修改为logfile /home/hadoop/app/redis-3.2.11/redis_log.log
2.将dir /.
修改为dir /home/hadoop/app/redis-3.2.11/redis_dbfile/
修改完成后就可以了。
cd /home/hadoop/app/redis-3.2.11/
./src/redis-server redis.conf
ps -ef | grep redis
./src/redsi-cli -h 连接IP(刚刚修改redis.conf文件中bind后面的IP)
或者:
./src/redsi-cli -p 6379
连接上redis后,执行:
config set requirepass 123(密码)
修改完密码之后,再次连接redis时要输入密码才可以进行操作。
>AUTH 密码
./src/redis-cli shutdown
在执行该命令时可能会出现如下错误:
DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no’ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no’, and then restarting the server. 3) If you started the server manually just for testing, restart it with the ‘–protected-mode no’ option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
此时,一定要查看redis.conf文件中的protected-mode是否为no,还要查看刚才创建的redis_log.log文件和redsi_dbfile目录的权限是否为777.
1.查看key:
>keys *
2.添加数据
>set key value
3.获取数据
>get key
3.value的加减操作
一次加1:
>INCR key
一次多加:
>INCRBY key 100
减得话和加一样,命令是DECRBY
如果在maven中使用redis,要添加redis的Java依赖:
redis.clients
jedis
2.9.0
jedis是用来连接redis的jar包,在Github上都可以找到,不再详细阐述。