跳转至

49. redis安装

1. 下载redis

1.浏览器访问redis官网http://redis.cn/ 点击下载

img.png

2.查看

img_2.png

3.访问下载地址 http://download.redis.io/releases/ 根据需要下载

img_1.png

2. 上传压缩包

mkdir /data
#上传后查看
ls /data

3. 解压缩

cd /data
tar -zxvf redis*
[root@152 ~]# cd /data/
[root@152 data]# ls
redis-3.2.12.tar.gz
[root@152 data]# tar -zxvf redis*
[root@152 data]# ls
redis-3.2.12  redis-3.2.12.tar.gz

4. 改名

cd /data
mv redis* redis
[root@152 data]# mv redis-3.2.12 redis
[root@152 data]# ls
redis  redis-3.2.12.tar.gz

5. 安装

cd /data/redis
make
[root@152 data]# cd /data/redis
[root@152 redis]# make

#大概30秒完成

6. 启动redis

/data/redis/src/redis-server &
[root@152 redis]# /data/redis/src/redis-server &
[1] 62756
[root@152 redis]# 62756:C 09 May 18:43:38.819 # Warning: no config file specifie                                                                              d, using the default config. In order to specify a config file use /data/redis/s                                                                              rc/redis-server /path/to/redis.conf
62756:M 09 May 18:43:38.819 * Increased maximum number of open files to 10032 (i                                                                              t was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.2.12 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 62756
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

62756:M 09 May 18:43:38.820 # WARNING: The TCP backlog setting of 511 cannot be                                                                               enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
62756:M 09 May 18:43:38.820 # Server started, Redis version 3.2.12
62756:M 09 May 18:43:38.820 # WARNING overcommit_memory is set to 0! Background                                                                               save may fail under low memory condition. To fix this issue add 'vm.overcommit_m                                                                              emory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.ove                                                                              rcommit_memory=1' for this to take effect.
62756:M 09 May 18:43:38.820 # WARNING you have Transparent Huge Pages (THP) supp                                                                              ort enabled in your kernel. This will create latency and memory usage issues wit                                                                              h Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transpar                                                                              ent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to reta                                                                              in the setting after a reboot. Redis must be restarted after THP is disabled.
62756:M 09 May 18:43:38.820 * The server is now ready to accept connections on p                                                                              ort 6379

[root@152 redis]#

#查看端口
[root@152 redis]# netstat -tunlp|grep 63
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      62756/redis-server
tcp6       0      0 :::6379                 :::*                    LISTEN      62756/redis-server
[root@152 redis]#
[root@152 redis]#
[root@152 redis]#
[root@152 redis]# ps -ef|grep redis
root      62756  57768  0 18:43 pts/0    00:00:00 /data/redis/src/redis-server *:6379
root      63191  57768  0 18:44 pts/0    00:00:00 grep --color=auto redis
[root@152 redis]#

7. 添加环境变量

echo "export PATH=/data/redis/src:$PATH" >>/etc/profile
source /etc/profile

8. 创建配置文件

mkdir /data/6379 -p

vim /data/6379/redis.conf
#写入如下参数
daemonize yes
port 6379
logfile /data/6379/redis.log
dir /data/6379
dbfilename dump.rdb

9. 重启redis

redis-cli shutdown
redis-server /data/6379/redis.conf

netstat -tunlp|grep 6379
[root@152 redis]# redis-cli shutdown
62756:M 09 May 18:46:14.822 # User requested shutdown...
62756:M 09 May 18:46:14.822 * Saving the final RDB snapshot before exiting.
62756:M 09 May 18:46:14.823 * DB saved on disk
62756:M 09 May 18:46:14.823 # Redis is now ready to exit, bye bye...
[1]+  Done                    /data/redis/src/redis-server
[root@152 redis]# redis-server /data/6379/redis.conf
[root@152 redis]#
[root@152 redis]# netstat -tunlp |grep 6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      63750/redis-server
tcp6       0      0 :::6379                 :::*                    LISTEN      63750/redis-server
[root@152 redis]#
[root@152 redis]#
[root@152 redis]# ps -ef|grep redis
root      63750      1  0 18:46 ?        00:00:00 redis-server *:6379
root      63888  57768  0 18:46 pts/0    00:00:00 grep --color=auto redis
[root@152 redis]#

10. 登录测试

redis-cli

set num 10
get num
[root@152 redis]# redis-cli
127.0.0.1:6379> set num 10
OK
127.0.0.1:6379> get num
"10"
127.0.0.1:6379> exit
[root@152 redis]#

redis-cli基本功能

1. 进入命令行

redis-cli

2. 退出命令行

exit

3. 退出redis

redis-cli
shutdown
#或者
redis-cli shutdown

4. 启动redis

redis-server /data/6379/redis.conf

5. 登录本地redis

redis-cli -p 6379

6. 登录远程redis

#刚安装的redis有安全策略,不可远程登录
redis-cli -h 192.168.178.152 -p 6379
[root@152 redis]# redis-cli -h 192.168.178.152 -p 6379
192.168.178.152:6379> get num
(error) 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.
192.168.178.152:6379> set num1 11
(error) 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.
192.168.178.152:6379>
192.168.178.152:6379> quit
[root@152 redis]#

7. 非交互式命令

redis-cli set num 10

8. 批量执行redis命令

#1.创建redis命令文件
vim /tmp/1.txt
set a 1
set b 2
set c 3

#2.批量执行
cat /tmp/1.txt |redis-cli
[root@152 redis]# vim /tmp/1.txt
[root@152 redis]# cat /tmp/1.txt
set a 1
set b 2
set c 3
[root@152 redis]# cat /tmp/1.txt |redis-cli
OK
OK
OK
[root@152 redis]# redis-cli get a
"1"
[root@152 redis]# redis-cli get b
"2"
[root@152 redis]# redis-cli get c
"3"

9. redis-cli命令参数

redis-cli
    -p:指定端口
    -h:指定IP
    -a:指定密码

redis安全管理

1. 介绍

#redis默认开启了保护模式,只允许本地回环地址登录并访问数据库

2. 参数

protected-mode yes/no   #保护模式,是否只允许本地访问

3. bind:指定IP监听(即白名单)

vim /data/6379/redis.conf

#添加如下一行内容
bind  192.168.178.152   127.0.0.1

4. 设置密码

vim /data/6379/redis.conf

requirepass 123456

5. 如何使用密码

#方法1:命令行中输入密码
redis-cli -a 123456

#方法2:客户端中输入密码
redis-cli
    auth 123456
[root@152 redis]# vim /data/6379/redis.conf
[root@152 redis]# redis-cli shutdown
[root@152 redis]# redis-server /data/6379/redis.conf
[root@152 redis]#


[root@152 redis]# redis-cli
127.0.0.1:6379> set d 1
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set d 1
OK
127.0.0.1:6379> get d
"1"
127.0.0.1:6379> quit


[root@152 redis]# redis-cli -a 123456
127.0.0.1:6379> set e 2
OK
127.0.0.1:6379> get e
"2"
127.0.0.1:6379> exit
[root@152 redis]#

6. 在线查看和修改配置

CONFIG GET * #查看所有配置

CONFIG GET requirepass
CONFIG SET requirepass 123
#重启redis失效
#疑问:必须大写吗?小写可以不?(验证一下)(已验证,不区分大小写)
[root@152 redis]# redis-cli -a 123456
127.0.0.1:6379> config get *
  1) "dbfilename"
  2) "dump.rdb"
  3) "requirepass"
  4) "123456"
  5) "masterauth"
  6) ""
  7) "unixsocket"
  8) ""
  9) "logfile"
 10) "/data/6379/redis.log"
 11) "pidfile"
 12) "/var/run/redis.pid"
 13) "slave-announce-ip"
 14) ""
 15) "maxmemory"
 16) "0"
 17) "maxmemory-samples"
 18) "5"
 19) "timeout"
 20) "0"
 21) "auto-aof-rewrite-percentage"
 22) "100"
 23) "auto-aof-rewrite-min-size"
 24) "67108864"
 25) "hash-max-ziplist-entries"
 26) "512"
 27) "hash-max-ziplist-value"
 28) "64"
 29) "list-max-ziplist-size"
 30) "-2"
 31) "list-compress-depth"
 32) "0"
 33) "set-max-intset-entries"
 34) "512"
 35) "zset-max-ziplist-entries"
 36) "128"
 37) "zset-max-ziplist-value"
 38) "64"
 39) "hll-sparse-max-bytes"
 40) "3000"
 41) "lua-time-limit"
 42) "5000"
 43) "slowlog-log-slower-than"
 44) "10000"
 45) "latency-monitor-threshold"
 46) "0"
 47) "slowlog-max-len"
 48) "128"
 49) "port"
 50) "6379"
 51) "tcp-backlog"
 52) "511"
 53) "databases"
 54) "16"
 55) "repl-ping-slave-period"
 56) "10"
 57) "repl-timeout"
 58) "60"
 59) "repl-backlog-size"
 60) "1048576"
 61) "repl-backlog-ttl"
 62) "3600"
 63) "maxclients"
 64) "10000"
 65) "watchdog-period"
 66) "0"
 67) "slave-priority"
 68) "100"
 69) "slave-announce-port"
 70) "0"
 71) "min-slaves-to-write"
 72) "0"
 73) "min-slaves-max-lag"
 74) "10"
 75) "hz"
 76) "10"
 77) "cluster-node-timeout"
 78) "15000"
 79) "cluster-migration-barrier"
 80) "1"
 81) "cluster-slave-validity-factor"
 82) "10"
 83) "repl-diskless-sync-delay"
 84) "5"
 85) "tcp-keepalive"
 86) "300"
 87) "cluster-require-full-coverage"
 88) "yes"
 89) "no-appendfsync-on-rewrite"
 90) "no"
 91) "slave-serve-stale-data"
 92) "yes"
 93) "slave-read-only"
 94) "yes"
 95) "stop-writes-on-bgsave-error"
 96) "yes"
 97) "daemonize"
 98) "yes"
 99) "rdbcompression"
100) "yes"
101) "rdbchecksum"
102) "yes"
103) "activerehashing"
104) "yes"
105) "protected-mode"
106) "yes"
107) "repl-disable-tcp-nodelay"
108) "no"
109) "repl-diskless-sync"
110) "no"
111) "aof-rewrite-incremental-fsync"
112) "yes"
113) "aof-load-truncated"
114) "yes"
115) "maxmemory-policy"
116) "noeviction"
117) "loglevel"
118) "notice"
119) "supervised"
120) "no"
121) "appendfsync"
122) "everysec"
123) "syslog-facility"
124) "local0"
125) "appendonly"
126) "no"
127) "dir"
128) "/data/6379"
129) "save"
130) ""
131) "client-output-buffer-limit"
132) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
133) "unixsocketperm"
134) "0"
135) "slaveof"
136) ""
137) "notify-keyspace-events"
138) ""
139) "bind"
140) "192.168.178.152 127.0.0.1"
127.0.0.1:6379>
127.0.0.1:6379> CONFIG GET requirepass
1) "requirepass"
2) "123456"
127.0.0.1:6379> CONFIG SET requirepass 123
OK
127.0.0.1:6379>
127.0.0.1:6379> quit
[root@152 redis]# redis-cli -a 123456
127.0.0.1:6379> set cp 1
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123
OK
127.0.0.1:6379> set cp 1
OK
127.0.0.1:6379> get cp
"1"
127.0.0.1:6379>

最后更新: 2022-02-20 08:44:07