redis安装和启动
Redis从一开始就只支持Linux,后面虽然有团队搞出Window版本,但是我还是建议大伙安装到Linux中。
准备工作 VMware 以及Xshell
redis官方安装教程,但是比较简单,不适合生产环境的安装部署;
我们这边详细点,
第一步:安装gcc
gcc编译c的,因为redis是c编写的,所以我们先安装下gcc
yum install gcc-c++
第二步:wget方式 下载redis压缩包,并解压,以及编译
下载
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
解压
tar -zxvf redis-3.2.9.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-3.2.9
-rw-r--r--. 1 root root 1547695 5月 17 23:40 redis-3.2.9.tar.gz
[root@localhost ~]#
这样/root/目录下就有redis了
编译:
cd redis-3.2.9
进入目录,
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下 后台启动用到
[root@localhost redis-3.2.9]# cp redis.conf /usr/local/redis/
[root@localhost redis-3.2.9]# 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-3.2.9]#
第四步:启动和关闭redis服务
启动redis就是执行redis里的bin里的redis-server命令
进入redis目录 执行
[root@localhost redis-3.2.9]# 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]#
第五步:redis基本使用
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379>
进入客户端
我们存储 name:java1234
key:value格式
127.0.0.1:6379> set name java1234
OK
127.0.0.1:6379> get name
"java1234"
通过set设置,通过get获取
127.0.0.1:6379> keys *
1) "name"
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
通过del删除key,keys * 显示所有keys
==============================================================================================================================
建一个Maven项目,
pom里加下jedis依赖,
1
2
3
4
5
|
< dependency > < groupId >redis.clients</ groupId > < artifactId >jedis</ artifactId > < version >2.9.0</ version > </ dependency > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.java1234.redis; import redis.clients.jedis.Jedis; /** * 测试类 * @author user * */ public class JedisTest { public static void main(String[] args) { Jedis jedis= new Jedis( "192.168.1.107" , 6379 ); // 创建客户端 设置IP和端口 jedis.set( "name" , "java知识分享网" ); // 设置值 String value=jedis.get( "name" ); // 获取值 System.out.println(value); jedis.close(); // 释放连接资源 } } |
测试代码,
运行 报错了
连接超时,
我们配置下防火墙 开一个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
b.配置文件修改<推荐>
打开redis.conf ,找到requirepass值修改密码,如下
# requirepass foobared requirepass yourpassword //此处注意,行前不能有空格
3.2 重新设置密码后,重新登录才能获取操作权限
redis-cli.exe -h 127.0.0.1 -p 6379 -a 123456 //需添加密码参数
说明设置成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.java1234.redis; import redis.clients.jedis.Jedis; /** * 测试类 * @author user * */ public class JedisTest { public static void main(String[] args) { Jedis jedis= new Jedis( "192.168.1.107" , 6379 ); // 创建客户端 设置IP和端口 jedis.auth( "123456" ); // 设置密码 jedis.set( "name" , "java知识分享网" ); // 设置值 String value=jedis.get( "name" ); // 获取值 System.out.println(value); jedis.close(); // 释放连接资源 } } |
这样就OK了