安装redis访问密码,学习之常用命令及平安体制

 

该文使用centos陆.伍 陆12位  
 redis3.二.八 

在服务器上,这里以linux服务器为例,为redis配置密码。

建一个Maven项目,

 

一.第二种艺术
(当前这种linux配置redis密码的办法是1种一时的,如若redis重启之后密码就能够失效,)

pom里加下jedis依赖,

一、redis常用命令

键值常用命令:
1、 keys 返回满足pattern的所有key。
127.0.0.1:6379> keys my*
127.0.0.1:6379> keys *

2、exits 确认key是否存在。 返回1表示存在  0表示不存在
127.0.0.1:6379> exists name

3、del :删除一个键 返回1:删除成功 0:失败
127.0.0.1:6379> del name
(integer) 1
4、expire 设置key(该key必须存在)的过期时间 返回1表示设置成功 0 失败
5、ttl 查看键的过期时间 如果该键已经过期(销毁)则返回负数
127.0.0.1:6379> expire myset2 10
(integer) 1
127.0.0.1:6379> ttl myset2
(integer) 4
127.0.0.1:6379> ttl myset2
(integer) -2
6、选择数据库 
Redis中一共有16个数据库他们分别是0-15
select 0 表示当前数据库
7、move 将当前数据库中的key转移到其他数据库中 返回1表示成功 0 失败
127.0.0.1:6379> move age 1
(integer) 1
8、persiste:移除给定key的过期时间 返回1表示取消成功 0 失败
127.0.0.1:6379[1]> expire age 100
(integer) 1
127.0.0.1:6379[1]> ttl age
(integer) 94
127.0.0.1:6379[1]> persist age
(integer) 1
127.0.0.1:6379[1]> get age
"25"
9、randomkey:随机数据库中的一个key
127.0.0.1:6379[1]> randomkey
"age"
10、rename:重命名key
127.0.0.1:6379[1]> keys *
1) "age"
127.0.0.1:6379[1]> rename age age_now
OK
127.0.0.1:6379[1]> keys *
1) "age_now"
11、返回key的数据类型
127.0.0.1:6379> type height
string
127.0.0.1:6379> type myzset1
Zset
服务器相关命令
1、Ping :测试链接redis是否存活 返回 PONG 表示链接存活
127.0.0.1:6379> ping
PONG
2、echo :在命令行打印数据
127.0.0.1:6379> echo hello
"hello"
3、select 选择数据库。Redis数据库编号是0-15,我们可以选择任意一个数据库进行数据存储
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> select 15
OK
127.0.0.1:6379[15]> select 16
(error) ERR invalid DB index
127.0.0.1:6379[15]>
4、quit 退出客户端与redis服务器的连接
5、dbsize 返回当前数据库中key的数量
127.0.0.1:6379> dbsize
(integer) 15
6、info 获取redis服务的相关信息和统计
127.0.0.1:6379> info
7、config get 查看redis服务器相关配置参数
127.0.0.1:6379> config get *
127.0.0.1:6379> config get port
1) "port"
2) "6379"
7、flushdb 删除当前数据库中所有的key
127.0.0.1:6379[1]> keys *
1) "age_now"
127.0.0.1:6379[1]> flushdb
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
8、删除所有数据库中所有的key
127.0.0.1:6379[1]> flushall

(一)首先进入redis,假诺未有张开redis则须要先展开:
[root@iZ94jzcra1hZ bin]# redis-cli -p 6379
127.0.0.1:6379> 
(二)查看当前redis有未有设置密码:
127.0.0.1:6379> config get requirepass
1) “requirepass”
2) “”
(叁)为上述展现表达没有密码,那么未来来安装密码:
127.0.0.1:6379> config set requirepass abcdefg
OK
127.0.0.1:6379> 
(四)再度翻开当前redis就提醒需要密码:
安装redis访问密码,学习之常用命令及平安体制。127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

二、 redis安全机制

997755.com澳门葡京 ,安装redis连接密码

Redis速度飞速,所以在壹台相比好的劳动起下,四个外表的用户能够在1分钟进行150k次的密码尝试连接,所以大家供给安装特别有力的密码来幸免暴力破解。

设置密码:在redis的陈设文件中 增多requirepass password

################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
requirepass jalja

重启redis服务器:[root@localhost bin]#  ./redis-server
/usr/local/redis/etc/redis.conf

一、测试密码是还是不是可用:

[root@localhost bin]# ./redis-cli

127.0.0.1:6379> keys *

(error) NOAUTH Authentication required.(操作被驳回)

动用密码(授权):auth password

127.0.0.1:6379> auth jalja

OK

二、使用密码登6redis服务器:[root@localhost bin]# ./redis-cli -a
jalja

贰.次之种艺术 (永恒格局)
亟需永恒配置密码的话就去redis.conf的配置文件中找到requirepass那些参数,如下配置:

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(); // 释放连接资源
    }
}

 

 

修改redis.conf配置文件  

测试代码,

# requirepass foobared
requirepass 1二叁   内定密码1贰3

运行 报错了

保留后重启redis就可以了

997755.com澳门葡京 1

 

连年超时,

连接redis

 

1.redis-cli连接redis

大家配备下防火墙 开贰个6379端口权限

[root@iZ2ze3zda3caeyx6pn7c5zZ bin]# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
12七.0.0.一:637玖> auth 12三        //钦定密码
OK
127.0.0.1:6379> keys *
1) “a”
2) “cit”
3) “clist”
4) “1”
127.0.0.1:6379>

firewall-cmd –zone=public –add-port=6379/tcp –permanent

 

firewall-cmd –reload

2.Jedis连接redis
java 代码方式

 

//连接redis服务器,192.168.0.100:6379
 jedis = new Jedis(“ip”, 6379);
 //权限认证
jedis.auth(“password”);

接轨运维 依旧报错 连接超时 错误;

 

 

布局文件措施

咱俩布置下 redis配置文件

<bean id=”jedisConnectionFactory”
class=”org.springframework.data.redis.connection.jedis.JedisConnectionFactory”>
<property name=”hostName” value=”${redis.host}” />
<property name=”port” value=”${redis.port}” />
<property name=”password” value=”${redis.pass}” />
</bean>

[root@localhost redis]# vi /usr/local/redis/redis.conf

 

 

 

997755.com澳门葡京 2

redis的别样命令。
假设须要关闭redis:
[root@iZ94jzcra1hZ bin]# pkill redis
倘诺急需敞开redis:
[root@iZ94jzcra1hZ bin]# redis-server &
加&符号的法力是为着让此进程转换为后台进度,不占用shell的服务。

 

 

此处绑定了本机,大家把那么些备注掉;

转载于:

# 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

997755.com澳门葡京 3

 

改成 no即可

 

编纂后 重启redis服务,然后运维 ,结果出来了

997755.com澳门葡京 4

 

其次种 设置redis连接密码

进去客户端

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> config set requirepass 123456

安装密码 12345陆

 

127.0.0.1:6379> quit

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> auth 123456

OK

 

证实设置成功

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(); // 释放连接资源
    }
}

997755.com澳门葡京 5

 

这样就OK了

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website