redis under Redis Introduction Safety-related installation and security configuration centos

 

redis advantage

Copy the code
High performance, 100,000 times per second reading speed 
write 80,000 times per second, the speed 
of all operations supported atomic 

used as a cache database, data in memory
replace mysql in certain scenarios, such as a social networking app
large system, can store session information, shopping cart order
Copy the code

 

yum install redis

1.yum installation

Copy the code
# Premise was configured Ali cloud yum source, epel source 
# package to see if there redis 
yum List redis 
# install redis
yum -y install redis
# installed, start redis
systemctl Start redis
Copy the code

2. Detection is working redis

redis-cli #redis client tool 
# into the interactive environment, perform ping, pong returns indicate a successful installation 
127.0.0.1:6379> the ping 
PONG

Source installation redis, compile and install

I used yum, it is quite easy to save trouble, why should learning source installation?

Some people say that good compiler installation performance? wrong

Compile installation advantages are:

  • You can specify when you compile the extension is installed module (module), php, apache, nginx is the same there are many third-party extension modules, such as mysql, compile and install time, if you need a custom storage engine (innodb, or MyIASM)
  • Can compile and install a unified installation path, linux software Conventions installation directory in / opt / below
  • The depot version is generally low, the compiler source code can be installed on demand, install the latest version
Copy the code
1.下载redis源码
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
2.解压缩
tar -zxf redis-4.0.10.tar.gz
3.切换redis源码目录
cd redis-4.0.10.tar.gz
4.编译源文件
make
5.编译好后,src/目录下有编译好的redis指令
6.make install 安装到指定目录,默认在/usr/local/bin
Copy the code

redis可执行文件

Copy the code
./redis-benchmark //用于进行redis性能测试的工具
./redis-check-dump //用于修复出问题的dump.rdb文件
./redis-cli //redis的客户端
./redis-server //redis的服务端
./redis-check-aof //用于修复出问题的AOF文件
./redis-sentinel //用于集群管理
Copy the code

redis配置文件

redis配置文件名为
redis.conf 
这个文件可以自定义

redis.conf核心配置项

Copy the code
绑定ip,如需要远程访问,需要填写服务器ip
bind 127.0.0.1  

端口,redis启动端口
port 

守护进程方式运行
daemonize yes

rdb数据文件
dbfilename dump.rdb

数据文件存放路径
dir /var/lib/redis/

日志文件
logfile /var/log/redis/redis-server.log

主从复制
slaveof 
Copy the code

启动redis服务端

启动redis非常简单,直接./redis-server就可以启动服务端了,还可以用下面的方法指定要加载的配置文件:
./redis-server ../redis.conf
默认情况下,redis-server会以非daemon的方式来运行,且默认服务端口为6379。

使用redis客户端

Copy the code
#执行客户端命令即可进入
./redis-cli  
#测试是否连接上redis
127.0.0.1:6379 > ping
返回pong代表连接上了

//用set来设置key、value
127.0.0.1:6379 > set name "chaoge"
OK
//get获取name的值
127.0.0.1:6379 > get name
"chaoge"
Copy the code

redis数据结构

Copy the code
redis是一种高级的key:value存储系统,其中value支持五种数据类型
字符串(strings)
散列(hashes)
列表(lists)
集合(sets)
有序集合(sorted sets)
Copy the code

基本命令

Copy the code
keys *         查看所有key
type key      查看key类型
expire key seconds    过期时间
ttl key     查看key过期剩余时间        -2表示key已经不存在了
persist     取消key的过期时间   -1表示key存在,没有过期时间

exists key     判断key存在    存在返回1    否则0
del keys     删除key    可以删除多个
dbsize         计算key的数量
Copy the code

 

数据结构示例

1.strings类型

  • set   设置key
  • get   获取key
  • append  追加string
  • mset   设置多个键值对
  • mget   获取多个键值对
  • del  删除key
  • incr  递增+1
  • decr  递减-1
Copy the code
127.0.0.1:6379> set name 'yu'   #设置key
OK
127.0.0.1:6379> get name    #获取value
"yu"
127.0.0.1:6379> set name 'yuchao'  #覆盖key
OK
127.0.0.1:6379> get name    #获取value
"yuchao"
127.0.0.1:6379> append name ' dsb'   #追加key的string
(integer) 10
127.0.0.1:6379> get name  #获取value "yuchao dsb"

127.0.0.1:6379> mset user1 'alex' user2 'xiaopeiqi'    #设置多个键值对 OK 127.0.0.1:6379> get user1    #获取value "alex" 127.0.0.1:6379> get user2    #获取value "xiaopeiqi" 127.0.0.1:6379> keys *      #找到所有key 1) "user2" 2) "name" 3) "user1" 127.0.0.1:6379> mget user1 user2 name #获取多个value 1) "alex" 2) "xiaopeiqi" 3) "yuchao dsb" 127.0.0.1:6379> del name        #删除key (integer) 1 127.0.0.1:6379> get name        #获取不存在的value,为nil (nil) 127.0.0.1:6379> set num 10    #string类型实际上不仅仅包括字符串类型,还包括整型,浮点型。redis可对整个字符串或字符串一部分进行操作,而对于整型/浮点型可进行自增、自减操作。 OK     127.0.0.1:6379> get num "10" 127.0.0.1:6379> incr num    #给num string 加一 INCR 命令将字符串值解析成整型,将其加一,最后将结果保存为新的字符串值,可以用作计数器 (integer) 11 127.0.0.1:6379> get num   "11" 127.0.0.1:6379> decr num      #递减1 (integer) 10 127.0.0.1:6379> decr num    #递减1 (integer) 9 127.0.0.1:6379> get num "9"
Copy the code

2.list类型

  • lpush         从列表左边插
  • rpush         从列表右边插
  • lrange          获取一定长度的元素  lrange key  start stop
  • ltrim               截取一定长度列表
  • lpop                 删除最左边一个元素
  • rpop                     删除最右边一个元素
  • lpushx/rpushx                key存在则添加值,不存在不处理
Copy the code
lpush duilie 'alex' 'peiqi' 'ritian'  #新建一个duilie,从左边放入三个元素

llen duilie  #查看duilie长度

lrange duilie 0 -1  #查看duilie所有元素

rpush duilie 'chaoge'   #从右边插入chaoge

lpushx duilie2  'dsb'  #key存在则添加 dsb元素,key不存在则不作处理

ltrim duilie 0 2  #截取队列的值,从索引0取到2,删除其余的元素

lpop #删除左边的第一个
rpop #删除右边的第一个
Copy the code

3.sets集合类型

redis的集合,是一种无序的集合,集合中的元素没有先后顺序。

集合相关的操作也很丰富,如添加新元素、删除已有元素、取交集、取并集、取差集等。我们来看例子:

  • sadd/srem   添加/删除 元素
  • sismember   判断是否为set的一个元素
  • smembers    返回集合所有的成员
  • sdiff             返回一个集合和其他集合的差异
  • sinter           返回几个集合的交集
  • sunion          返回几个集合的并集
Copy the code
sadd zoo  wupeiqi yuanhao  #添加集合,有三个元素,不加引号就当做字符串处理

smembers zoo  #查看集合zoo成员

srem zoo  wupeiqi #删除zoo里面的alex

sismember zoo wupeiqi  #返回改是否是zoo的成员信息,不存在返回0,存在返回1

sadd zoo wupeiqi   #再把wupeiqi加入zoo

smembers zoo  #查看zoo成员

sadd zoo2 wupeiqi mjj #添加新集合zoo2

sdiff zoo zoo2 #找出集合zoo中有的,而zoo2中没有的元素

sdiff zoo2  zoo  #找出zoo2中有,而zoo没有的元素

sinter zoo zoo1   #找出zoo和zoo1的交集,都有的元素

sunion  zoo zoo1  #找出zoo和zoo1的并集,所有的不重复的元素
Copy the code

4.有序集合

都是以z开头的命令

zset的每一个成员都有一个分数与之对应,并且分数是可以重复的。有序集合的增删改由于有啦排序,执行效率就是非常快速的,即便是访问集合中间的数据也是非常高效的。

用来保存需要排序的数据,例如排行榜,成绩,工资等。

实例

利用有序集合的排序,排序学生的成绩

127.0.0.1:6379> ZADD mid_test 70 "alex"
(integer) 1
127.0.0.1:6379> ZADD mid_test 80 "wusir"
(integer) 1
127.0.0.1:6379> ZADD mid_test 99 "yuyu"

排行榜,zreverange 倒叙   zrange正序

Copy the code
127.0.0.1:6379> ZREVRANGE mid_test 0 -1 withscores
1) "yuyu"
2) "99"
3) "wusir"
4) "80"
5) "xiaofneg"
6) "75"
7) "alex"
8) "70"
127.0.0.1:6379> ZRANGE mid_test 0 -1 withscores
1) "alex"
2) "70"
3) "xiaofneg"
4) "75"
5) "wusir"
6) "80"
7) "yuyu"
8) "99"
Copy the code

移除有序集合mid_test中的成员,xiaofeng给移除掉

Copy the code
127.0.0.1:6379> ZREM mid_test xiaofneg
(integer) 1
127.0.0.1:6379> ZRANGE mid_test 0 -1 withscores
1) "alex"
2) "70"
3) "wusir"
4) "80"
5) "yuyu"
6) "99"
Copy the code

返回有序集合mid_test的基数

127.0.0.1:6379> ZCARD mid_test
(integer) 3

返回成员的score值

127.0.0.1:6379> ZSCORE mid_test alex
"70"

zrank返回有序集合中,成员的排名。默认按score,从小到大排序。

Copy the code
127.0.0.1:6379> ZRANGE mid_test 0 -1 withscores
1) "alex"
2) "70"
3) "wusir"
4) "80"
5) "yuyu"
6) "99"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZRANK mid_test wusir
(integer) 1
127.0.0.1:6379> ZRANK mid_test yuyu
(integer) 2
Copy the code

5.哈希数据结构

哈希结构就是  k1   ->  k1 : v1     如同字典 套字典  { k1 : { k2: v2 }  }   ,取出v2 必须  k1,取出k2

hashes即哈希。哈希是从redis-2.0.0版本之后才有的数据结构。

hashes存的是字符串和字符串值之间的映射,比如一个用户要存储其全名、姓氏、年龄等等,就很适合使用哈希。

  • hset 设置散列值
  • hget  获取散列值
  • hmset  设置多对散列值
  • hmget  获取多对散列值
  • hsetnx   如果散列已经存在,则不设置(防止覆盖key)
  • hkeys     返回所有keys
  • hvals     返回所有values
  • hlen      返回散列包含域(field)的数量
  • hdel     删除散列指定的域(field)
  • hexists    判断是否存在
Copy the code
redis hash是一个string类型的field和value的映射表

语法  hset key field value  

hset news1   title "first news title" #设置第一条新闻 news的id为1,添加数据title的值是"first news title"

hset news1 content "news content"    #添加一个conntent内容

hget news1 title   #获取news:1的标题

hget news1  content  #获取news的内容

hmget news1 title content #获取多对news:1的 值 hmset news2 title "second news title" content "second Contents2" #设置第二条新闻news:2 多个field hmget news2 title content #获取news:2的多个值 hkeys news1 #获取新闻news:1的所有key hvals news1 #获取新闻news:1的所有值 hlen news1 #获取新闻news:1的长度 hdel news1 title #删除新闻news:1的title hlen news1 #看下新闻news:1的长度 hexists news1 title #判断新闻1中是否有title,不存在返回0,存在返回1
Copy the code

 

centos下redis安全相关

 

博文背景:

由于发现众多同学,在使用云服务器时,安装的redis3.0+版本都关闭了protected-mode,因而都遭遇了挖矿病毒的攻击,使得服务器99%的占用率!!

因此我们在使用redis时候,最好更改默认端口,并且使用redis密码登录。

 

(1)redis没有用户概念,redis只有密码
(2)redis默认在工作在保护模式下。不允许远程任何用户登录的(protected-mode)

 

 

redis.conf设置

protected-mode yes   #打开保护模式
port 6380  #更改默认启动端口
requirepass xxxxxx   #设置redis启动密码,xxxx是自定义的密码

启动redis服务端

redis-server /opt/redis-4.0.10/redis.conf &     #指定配置文件启动redis,且后台启动

使用密码登录redis,使用6380端口

方法1,使用这个

[root@oldboy_python ~ 09:48:41]#redis-cli -p 6380
127.0.0.1:6380> auth xxxx
OK

方法2,此方案不安全,容易暴露密码

[root@oldboy_python ~ 09:49:46]#redis-cli -p 6380 -a xxxx
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6380> ping
PONG

 

补充

检查redis是否设置了密码

127.0.0.1:6380> CONFIG get requirepass
1) "requirepass"
2) "xxxxxx"

如果没有,也可以给redis设置密码(命令方式)

CONFIG set requirepass "xxxxxx"

 

因此你的redis就不容易被黑客入侵了。

 

redis优势

Copy the code
性能高,读取速度10万次每秒
写入速度8万次每秒
所有操作支持原子性

用作缓存数据库,数据放在内存中
替代某些场景下的mysql,如社交类app
大型系统中,可以存储session信息,购物车订单
Copy the code

 

yum安装redis

1.yum安装

Copy the code
#前提得配置好阿里云yum源,epel源
#查看是否有redis包
yum list redis
#安装redis
yum install redis -y
#安装好,启动redis
systemctl start redis
Copy the code

2.检测redis是否工作

redis-cli    #redis 客户端工具
#进入交互式环境后,执行ping,返回pong表示安装成功
127.0.0.1:6379> ping
PONG

源码安装redis,编译安装

大家用过yum,是相当省事好用吧,为什么还要学习源码安装?

有人说编译安装性能好?错

编译安装的优势是:

  • 编译安装时可以指定扩展的module(模块),php、apache、nginx都是一样有很多第三方扩展模块,如mysql,编译安装时候,如果需要就定制存储引擎(innodb,还是MyIASM)
  • 编译安装可以统一安装路径,linux软件约定安装目录在/opt/下面
  • 软件仓库版本一般比较低,编译源码安装可以根据需求,安装最新的版本
Copy the code
1.下载redis源码
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
2.解压缩
tar -zxf redis-4.0.10.tar.gz
3.切换redis源码目录
cd redis-4.0.10.tar.gz
4.编译源文件
make
5.编译好后,src/目录下有编译好的redis指令
6.make install 安装到指定目录,默认在/usr/local/bin
Copy the code

redis可执行文件

Copy the code
./redis-benchmark //用于进行redis性能测试的工具
./redis-check-dump //用于修复出问题的dump.rdb文件
./redis-cli //redis的客户端
./redis-server //redis的服务端
./redis-check-aof //用于修复出问题的AOF文件
./redis-sentinel //用于集群管理
Copy the code

redis配置文件

redis配置文件名为
redis.conf 
这个文件可以自定义

redis.conf核心配置项

Copy the code
绑定ip,如需要远程访问,需要填写服务器ip
bind 127.0.0.1  

端口,redis启动端口
port 

守护进程方式运行
daemonize yes

rdb数据文件
dbfilename dump.rdb

数据文件存放路径
dir /var/lib/redis/

日志文件
logfile /var/log/redis/redis-server.log

主从复制
slaveof 
Copy the code

启动redis服务端

启动redis非常简单,直接./redis-server就可以启动服务端了,还可以用下面的方法指定要加载的配置文件:
./redis-server ../redis.conf
默认情况下,redis-server会以非daemon的方式来运行,且默认服务端口为6379。

使用redis客户端

Copy the code
#执行客户端命令即可进入
./redis-cli  
#测试是否连接上redis
127.0.0.1:6379 > ping
返回pong代表连接上了

//用set来设置key、value
127.0.0.1:6379 > set name "chaoge"
OK
//get获取name的值
127.0.0.1:6379 > get name
"chaoge"
Copy the code

redis数据结构

Copy the code
redis是一种高级的key:value存储系统,其中value支持五种数据类型
字符串(strings)
散列(hashes)
列表(lists)
集合(sets)
有序集合(sorted sets)
Copy the code

基本命令

Copy the code
keys *         查看所有key
type key      查看key类型
expire key seconds    过期时间
ttl key     查看key过期剩余时间        -2表示key已经不存在了
persist     取消key的过期时间   -1表示key存在,没有过期时间

exists key     判断key存在    存在返回1    否则0
del keys     删除key    可以删除多个
dbsize         计算key的数量
Copy the code

 

数据结构示例

1.strings类型

  • set   设置key
  • get   获取key
  • append  追加string
  • mset   设置多个键值对
  • mget   获取多个键值对
  • del  删除key
  • incr  递增+1
  • decr  递减-1
Copy the code
127.0.0.1:6379> set name 'yu'   #设置key
OK
127.0.0.1:6379> get name    #获取value
"yu"
127.0.0.1:6379> set name 'yuchao'  #覆盖key
OK
127.0.0.1:6379> get name    #获取value
"yuchao"
127.0.0.1:6379> append name ' dsb'   #追加key的string
(integer) 10
127.0.0.1:6379> get name  #获取value "yuchao dsb"

127.0.0.1:6379> mset user1 'alex' user2 'xiaopeiqi'    #设置多个键值对 OK 127.0.0.1:6379> get user1    #获取value "alex" 127.0.0.1:6379> get user2    #获取value "xiaopeiqi" 127.0.0.1:6379> keys *      #找到所有key 1) "user2" 2) "name" 3) "user1" 127.0.0.1:6379> mget user1 user2 name #获取多个value 1) "alex" 2) "xiaopeiqi" 3) "yuchao dsb" 127.0.0.1:6379> del name        #删除key (integer) 1 127.0.0.1:6379> get name        #获取不存在的value,为nil (nil) 127.0.0.1:6379> set num 10    #string类型实际上不仅仅包括字符串类型,还包括整型,浮点型。redis可对整个字符串或字符串一部分进行操作,而对于整型/浮点型可进行自增、自减操作。 OK     127.0.0.1:6379> get num "10" 127.0.0.1:6379> incr num    #给num string 加一 INCR 命令将字符串值解析成整型,将其加一,最后将结果保存为新的字符串值,可以用作计数器 (integer) 11 127.0.0.1:6379> get num   "11" 127.0.0.1:6379> decr num      #递减1 (integer) 10 127.0.0.1:6379> decr num    #递减1 (integer) 9 127.0.0.1:6379> get num "9"
Copy the code

2.list类型

  • lpush         从列表左边插
  • rpush         从列表右边插
  • lrange          获取一定长度的元素  lrange key  start stop
  • ltrim               截取一定长度列表
  • lpop                 删除最左边一个元素
  • rpop                     删除最右边一个元素
  • lpushx/rpushx                key存在则添加值,不存在不处理
Copy the code
lpush duilie 'alex' 'peiqi' 'ritian'  #新建一个duilie,从左边放入三个元素

llen duilie  #查看duilie长度

lrange duilie 0 -1  #查看duilie所有元素

rpush duilie 'chaoge'   #从右边插入chaoge

lpushx duilie2  'dsb'  #key存在则添加 dsb元素,key不存在则不作处理

ltrim duilie 0 2  #截取队列的值,从索引0取到2,删除其余的元素

lpop #删除左边的第一个
rpop #删除右边的第一个
Copy the code

3.sets集合类型

redis的集合,是一种无序的集合,集合中的元素没有先后顺序。

集合相关的操作也很丰富,如添加新元素、删除已有元素、取交集、取并集、取差集等。我们来看例子:

  • sadd/srem   添加/删除 元素
  • sismember   判断是否为set的一个元素
  • smembers    返回集合所有的成员
  • sdiff             返回一个集合和其他集合的差异
  • sinter           返回几个集合的交集
  • sunion          返回几个集合的并集
Copy the code
sadd zoo  wupeiqi yuanhao  #添加集合,有三个元素,不加引号就当做字符串处理

smembers zoo  #查看集合zoo成员

srem zoo  wupeiqi #删除zoo里面的alex

sismember zoo wupeiqi  #返回改是否是zoo的成员信息,不存在返回0,存在返回1

sadd zoo wupeiqi   #再把wupeiqi加入zoo

smembers zoo  #查看zoo成员

sadd zoo2 wupeiqi mjj #添加新集合zoo2

sdiff zoo zoo2 #找出集合zoo中有的,而zoo2中没有的元素

sdiff zoo2  zoo  #找出zoo2中有,而zoo没有的元素

sinter zoo zoo1   #找出zoo和zoo1的交集,都有的元素

sunion  zoo zoo1  #找出zoo和zoo1的并集,所有的不重复的元素
Copy the code

4.有序集合

都是以z开头的命令

zset的每一个成员都有一个分数与之对应,并且分数是可以重复的。有序集合的增删改由于有啦排序,执行效率就是非常快速的,即便是访问集合中间的数据也是非常高效的。

用来保存需要排序的数据,例如排行榜,成绩,工资等。

实例

利用有序集合的排序,排序学生的成绩

127.0.0.1:6379> ZADD mid_test 70 "alex"
(integer) 1
127.0.0.1:6379> ZADD mid_test 80 "wusir"
(integer) 1
127.0.0.1:6379> ZADD mid_test 99 "yuyu"

排行榜,zreverange 倒叙   zrange正序

Copy the code
127.0.0.1:6379> ZREVRANGE mid_test 0 -1 withscores
1) "yuyu"
2) "99"
3) "wusir"
4) "80"
5) "xiaofneg"
6) "75"
7) "alex"
8) "70"
127.0.0.1:6379> ZRANGE mid_test 0 -1 withscores
1) "alex"
2) "70"
3) "xiaofneg"
4) "75"
5) "wusir"
6) "80"
7) "yuyu"
8) "99"
Copy the code

移除有序集合mid_test中的成员,xiaofeng给移除掉

Copy the code
127.0.0.1:6379> ZREM mid_test xiaofneg
(integer) 1
127.0.0.1:6379> ZRANGE mid_test 0 -1 withscores
1) "alex"
2) "70"
3) "wusir"
4) "80"
5) "yuyu"
6) "99"
Copy the code

返回有序集合mid_test的基数

127.0.0.1:6379> ZCARD mid_test
(integer) 3

返回成员的score值

127.0.0.1:6379> ZSCORE mid_test alex
"70"

zrank返回有序集合中,成员的排名。默认按score,从小到大排序。

Copy the code
127.0.0.1:6379> ZRANGE mid_test 0 -1 withscores
1) "alex"
2) "70"
3) "wusir"
4) "80"
5) "yuyu"
6) "99"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> ZRANK mid_test wusir
(integer) 1
127.0.0.1:6379> ZRANK mid_test yuyu
(integer) 2
Copy the code

5.哈希数据结构

哈希结构就是  k1   ->  k1 : v1     如同字典 套字典  { k1 : { k2: v2 }  }   ,取出v2 必须  k1,取出k2

hashes即哈希。哈希是从redis-2.0.0版本之后才有的数据结构。

hashes存的是字符串和字符串值之间的映射,比如一个用户要存储其全名、姓氏、年龄等等,就很适合使用哈希。

  • hset 设置散列值
  • hget  获取散列值
  • hmset  设置多对散列值
  • hmget  获取多对散列值
  • hsetnx   如果散列已经存在,则不设置(防止覆盖key)
  • hkeys     返回所有keys
  • hvals     返回所有values
  • hlen      返回散列包含域(field)的数量
  • hdel     删除散列指定的域(field)
  • hexists    判断是否存在
Copy the code
redis hash是一个string类型的field和value的映射表

语法  hset key field value  

hset news1   title "first news title" #设置第一条新闻 news的id为1,添加数据title的值是"first news title"

hset news1 content "news content"    #添加一个conntent内容

hget news1 title   #获取news:1的标题

hget news1  content  #获取news的内容

hmget news1 title content #获取多对news:1的 值 hmset news2 title "second news title" content "second Contents2" #设置第二条新闻news:2 多个field hmget news2 title content #获取news:2的多个值 hkeys news1 #获取新闻news:1的所有key hvals news1 #获取新闻news:1的所有值 hlen news1 #获取新闻news:1的长度 hdel news1 title #删除新闻news:1的title hlen news1 #看下新闻news:1的长度 hexists news1 title #判断新闻1中是否有title,不存在返回0,存在返回1
Copy the code

 

博文背景:

由于发现众多同学,在使用云服务器时,安装的redis3.0+版本都关闭了protected-mode,因而都遭遇了挖矿病毒的攻击,使得服务器99%的占用率!!

因此我们在使用redis时候,最好更改默认端口,并且使用redis密码登录。

 

(1)redis没有用户概念,redis只有密码
(2)redis默认在工作在保护模式下。不允许远程任何用户登录的(protected-mode)

 

 

redis.conf设置

protected-mode yes   #打开保护模式
port 6380  #更改默认启动端口
requirepass xxxxxx   #设置redis启动密码,xxxx是自定义的密码

启动redis服务端

redis-server /opt/redis-4.0.10/redis.conf &     #指定配置文件启动redis,且后台启动

使用密码登录redis,使用6380端口

方法1,使用这个

[root@oldboy_python ~ 09:48:41]#redis-cli -p 6380
127.0.0.1:6380> auth xxxx
OK

方法2,此方案不安全,容易暴露密码

[root@oldboy_python ~ 09:49:46]#redis-cli -p 6380 -a xxxx
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6380> ping
PONG

 

supplement

Check whether the password is set redis

127.0.0.1:6380> CONFIG get requirepass
1) "requirepass"
2) "xxxxxx"

If not, you can also set a password (command mode) to redis

CONFIG set requirepass "xxxxxx"

 

So your redis not easily hacked.

 

Guess you like

Origin www.cnblogs.com/cou1d/p/12409501.html