Redis入门、Redis教程(Windows平台)

Redis入门、Redis教程(Windows平台)
一、安装Redis
1、下载 Redis
https://github.com/tporadowski/redis/releases。

2、启动 Redis
cmd 
完整路径\redis-server

注意:可以配置环境变量,这样不需要输入完整的路径


启动日志:
[3364] 10 Jan 10:56:22.939 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
[3364] 10 Jan 10:56:22.969 # Redis version=5.0.14, bits=64, commit=a7c01ef4, modified=0, pid=3364, just started
[3364] 10 Jan 10:56:22.969 # Warning: no config file specified, using the default config. In order to specify a config file use c:\users\sunxn\desktop\redis\redis-x64-5.0.14\redis-server.exe /path/to/redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.14 (a7c01ef4/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 3364
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[3364] 10 Jan 10:56:22.969 # Server initialized
[3364] 10 Jan 10:56:22.969 * Ready to accept connections

3、首次安装后,配置Redis

注意这行启动日志:
 Warning: no config file specified, using the default config. In order to specify a config file use xxx\redis-x64-5.0.14\redis-server.exe /path/to/redis.conf

启动Redis时,指定配置文件
C:\Users\livePC>redis-server C:\Users\livePC\Desktop\redis\redis.windows.conf
[27700] 10 Jan 15:05:16.254 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
[27700] 10 Jan 15:05:16.254 # Redis version=5.0.14, bits=64, commit=a7c01ef4, modified=0, pid=27700, just started
[27700] 10 Jan 15:05:16.255 # Configuration loaded
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.14 (a7c01ef4/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 27700
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[27700] 10 Jan 15:05:16.258 # Server initialized
[27700] 10 Jan 15:05:16.272 * DB loaded from disk: 0.012 seconds
[27700] 10 Jan 15:05:16.272 * Ready to accept connections


4、Redis 环境变量(非必须)
 Redis_Home:
 Path:%Redis_Home%

cmd输入>redis-server
[4660] 10 Jan 11:22:16.261 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
[4660] 10 Jan 11:22:16.261 # Redis version=5.0.14, bits=64, commit=a7c01ef4, modified=0, pid=4660, just started
[4660] 10 Jan 11:22:16.261 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.14 (a7c01ef4/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4660
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

[4660] 10 Jan 11:22:16.277 # Server initialized
[4660] 10 Jan 11:22:16.277 * DB loaded from disk: 0.000 seconds
[4660] 10 Jan 11:22:16.277 * Ready to accept connections

二、Redis 客户端

1、客户端连接(cmd)
redis-cli
等同于
redis-cli -h 127.0.0.1 -p 6379


三、Redis 开启鉴权(密码)

1、设置redis密码(两种方式)


方式一:by配置文件
requirepass foobared    设置 Redis 连接密码,如果配置了连接密码,客户端在连接 Redis 时需要通过 AUTH <password> 命令提供密码,默认关闭


方式二:不需要重启redis服务的密码设置方式

连接redis之后,通过命令设置,如下:
config set requirepass 123456    //将密码设置成了123456
设置之后,可通过以下指令查看密码
config get requirepass

注意:通过命令行修改了密码之后,配置文件的requirepass字段后面的密码是不会随之修改的。

2、设置密码后,客户端需要鉴权(有两种方式输入密码)

127.0.0.1:6379> KEYS *
(error) NOAUTH Authentication required.

登陆方式一:连接的时候直接输入密码(会警告,这种方式登陆不安全,我猜是通过history可以看到完整的密码,而auth不会被记录history)
C:\Users\hqpc0431>redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

登陆方式二:连接上之后再输入密码
C:\Users\hqpc0431>redis-cli
127.0.0.1:6379> select k
(error) NOAUTH Authentication required.

127.0.0.1:6379> select get
(error) NOAUTH Authentication required.

127.0.0.1:6379> auth tjhq123456
OK


3、其它机器的客户端访问时报错:
(error) DENIED Redis is running in protected mode because protected mode is enab
led, no bind address was specified, no authentication password is requested to c
lients. 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 acc
essible from internet if you do so. Use CONFIG REWRITE to make this change perma
nent. 2) Alternatively you can just disable the protected mode by editing the Re
dis 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, r
estart it with the '--protected-mode no' option. 4) Setup a bind address or an a
uthentication password. NOTE: You only need to do one of the above things in ord
er for the server to start accepting connections from the outside.

四、Redis 几个问题
1、Redis默认端口为 6379,有什么故事?
作者在自己的一篇博文中解释了为什么选用 6379 作为默认端口,因为 6379 在手机按键上 MERZ 对应的号码,而 MERZ 取自意大利歌女 Alessia Merz 的名字

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值