第一步:安装gcc
gcc编译c的,因为redis是c编写的,所以我们先安装下gcc
yum install gcc-c++
第二步:wget方式 下载redis压缩包,并解压,以及编译
下载
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
解压
tar -zxvf redis-5.0.3.tar.gz
[root@localhost ~]# ll
总用量 1548
-rw-------. 1 root root 1261 6月 27 19:54 anaconda-ks.cfg
-rw-r--r--. 1 root root 25680 4月 27 18:45 mysql57-community-release-el7-11.noarch.rpm
drwxrwxr-x. 6 root root 4096 5月 17 23:39 redis-5.0.3
-rw-r--r--. 1 root root 1547695 5月 17 23:40 redis-5.0.3.tar.gz
[root@localhost ~]#
这样/root/目录下就有redis了
编译:
cd redis-5.0.3
进入目录,
make
编译
第三步:安装redis
make PREFIX=/usr/local/redis install
安装到/usr/local/redis/
已经有redis
[root@localhost local]# cd redis/
[root@localhost redis]# ll
总用量 0
drwxr-xr-x. 2 root root 134 7月 2 16:44 bin
[root@localhost redis]#
redis里有个bin
[root@localhost redis]# cd bin/
[root@localhost bin]# ll
总用量 15060
-rwxr-xr-x. 1 root root 2431832 7月 2 16:44 redis-benchmark
-rwxr-xr-x. 1 root root 25168 7月 2 16:44 redis-check-aof
-rwxr-xr-x. 1 root root 5181840 7月 2 16:44 redis-check-rdb
-rwxr-xr-x. 1 root root 2584848 7月 2 16:44 redis-cli
lrwxrwxrwx. 1 root root 12 7月 2 16:44 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5181840 7月 2 16:44 redis-server
[root@localhost bin]#
bin里是一些工具
cd回到root,我们需要把一个配置文件(redis.config) 复制到 redis下 后台启动用到
[root@localhost redis-5.0.3]# cp redis.conf /usr/local/redis/
[root@localhost redis-5.0.3]# ll /usr/local/redis/
总用量 48
drwxr-xr-x. 2 root root 134 7月 2 16:44 bin
-rw-r--r--. 1 root root 46695 7月 2 16:49 redis.conf
[root@localhost redis-5.0.3]#
第四步:启动和关闭redis服务
启动redis就是执行redis里的bin里的redis-server命令
进入redis目录 执行
[root@localhost redis-5.0.3]# cd /usr/local/redis/
[root@localhost redis]# bin/redis-server
出现这种图标,说明启动成功;
但是 ,这种启动是前端或者前台启动,假如退出 程序就终止或者退出了。
所以这种服务程序,必须后端运行;
我们通过修改配置文件redis.conf
操作,
我们ctrl+c 退出当前程序;
vi打开redis.conf vi /usr/local/redis/redis.conf
找到
把no改成yes
esc退出 wq!保存;
然后进入redis目录,然后加载配置文件运行;
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ./bin/redis-server ./redis.conf
我们通过ps -ef | grep -i redis命令来搜索redis服务
[root@localhost redis]# ps -ef | grep -i redis
root 8692 1 0 16:52 ? 00:00:19 bin/redis-server *:6379
root 8954 8930 0 20:30 pts/0 00:00:00 grep --color=auto -i redi
[root@localhost redis]#
我们通过shutdown命令来停止redis服务的运行
[root@localhost redis]# ./bin/redis-cli shutdown
[root@localhost redis]# ps -ef | grep -i redis
root 8959 8930 0 20:35 pts/0 00:00:00 grep --color=auto -i redi
[root@localhost redis]#
这个时候并不支持java的连接需要:
我们配置下防火墙 开一个6379端口权限
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
继续运行 还是报错 连接超时 错误;
我们配置下 redis配置文件
[root@localhost redis]# vi /usr/local/redis/redis.conf
这里绑定了本机,我们把这个备注掉;
# bind 127.0.0.1
配置完后
[root@localhost redis]# ./bin/redis-cli shutdown
[root@localhost redis]# ./bin/redis-server ./redis.conf
要重启下redis服务;
继续运行 又报错了
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: 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.
at redis.clients.jedis.Protocol.processError(Protocol.java:127)
at redis.clients.jedis.Protocol.process(Protocol.java:161)
at redis.clients.jedis.Protocol.read(Protocol.java:215)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
at redis.clients.jedis.Jedis.set(Jedis.java:121)
at com.java1234.redis.JedisTest.main(JedisTest.java:14)
这个是因为远程连接redis redis自我保护 拒绝访问;
有两种方法 解决
第一种 直接去掉自我保护功能(不推荐)
[root@localhost redis]# vi /usr/local/redis/redis.conf
进入配置
找到 protected-mode yes
改成 no即可
编辑后 重启redis服务,然后运行
第二种 设置redis连接密码
进入客户端
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> config set requirepass 123456
设置密码 123456
127.0.0.1:6379> quit
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> auth 123456
OK
说明设置成功
这样就OK了