Redis入门简介
Redis是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
Redis是一个key-value存储系统。 和Memcached缓存类似,Redis支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、hash(哈希类型)。并且支持在服务器端计算集合的并,交和补集(difference)等,还支持多种排序功能。Redis也被看成是一个数据结构服务器。
编译安装Redis
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://download.redis.io/redis-stable.tar.gz
100%[====================================================================================================>] 2,275,297 50.1KB/s in 51s
2020-08-23 12:43:44 (43.2 KB/s) - ‘redis-stable.tar.gz’ saved [2275297/2275297]
[root@localhost src]# ls
debug kernels redis-stable.tar.gz
[root@localhost src]# tar xf redis-stable.tar.gz
[root@localhost src]# cd redis-stable
#可以看到已经有了Makefile文件不需要使用configure预编译了
这样是别人帮你编译好,安装路径指定好了,安装的时候会安装在被人预编译时候指定的路径下
[root@localhost redis-stable]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-moduleapi sentinel.conf tests utils
BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src TLS.md
[root@localhost redis-stable]# vim src/Makefile --修改安装路径
PREFIX?=/usr/local/redis
INSTALL_BIN=$(PREFIX)/bin
INSTALL=install
或者使用make PREFIX=/usr/local/redis install也可以修改安装目录,但是PREFIX必须大写
[root@localhost redis-stable]# yum install gcc-c++ tcl -y
[root@localhost redis-stable]# make && make install --编译安装
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/usr/src/redis-stable/src'
[root@localhost redis-stable]# cp redis.conf /usr/local/redis/ --将配置文件拷贝到/usr/local/redis下,这个文件在使用下面脚本配置后记得删除
Redis启动
[root@localhost redis-stable]# /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
9544:C 23 Aug 2020 12:58:17.157 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9544:C 23 Aug 2020 12:58:17.158 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=9544, just started
9544:C 23 Aug 2020 12:58:17.158 # Configuration loaded
9544:M 23 Aug 2020 12:58:17.159 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 9544
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
9544:M 23 Aug 2020 12:58:17.160 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9544:M 23 Aug 2020 12:58:17.160 # Server initialized
9544:M 23 Aug 2020 12:58:17.160 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
9544:M 23 Aug 2020 12:58:17.160 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
9544:M 23 Aug 2020 12:58:17.160 * Ready to accept connections
#这里有很多warning需要修改配置文件
[root@localhost redis-stable]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
#让redis以守护进程方式后台运行
[root@localhost redis-stable]# sed -n '/daemonize no/p' /usr/local/redis/redis.conf
daemonize no
[root@localhost redis]# sed -i '/daemonize no/s/no/yes/g' /usr/local/redis/redis.conf
或者使用vim修改
[root@localhost redis-stable]# vim /usr/local/redis/redis.conf
daemonize yes
#可以看到修改daemonize yes之后redis就跑到后端运行了
[root@localhost redis]# /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
10635:C 23 Aug 2020 13:20:48.433 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
10635:C 23 Aug 2020 13:20:48.433 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=10635, just started
10635:C 23 Aug 2020 13:20:48.433 # Configuration loaded
[root@localhost redis]# ps -ef | grep redis | grep -v grep
root 10636 1 0 13:20 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
脚本配置Redis
[root@localhost redis]# pwd
/usr/local/redis
[root@localhost redis]# ls
bin redis.conf
#每次通过这么长的命令/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf去启动redis是很麻烦的
#utils该目录下面放着redis的一些脚本,install_server.sh是用来配置redis的
[root@localhost redis]# cd /usr/src/redis-stable/utils/
[root@localhost utils]# ll install_server.sh
-rwxr-xr-x 1 1000 1000 9567 Mar 12 23:07 install_server.sh
[root@localhost utils]# ./install_server.sh --现在开始配置redis的启动脚本了
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /usr/local/redis/6379.conf
--为什么不叫redis.conf呢,这样配置方便后期做多实例
Please select the redis log file name [/var/log/redis_6379.log] /usr/local/redis/redis_6379.log
--配置日志文件位置
Please select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis/6379
--选择redis的数据目录
Please select the redis executable path [] /usr/local/redis/bin/redis-server
Selected config:
Port : 6379
Config file : /usr/local/redis/6379.conf
Log file : /usr/local/redis/redis_6379.log
Data dir : /usr/local/redis/6379
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!
--如果你的redis没有起来它会帮你起来
[root@localhost redis]# rm -rf redis.conf --
配置好之后记得删除之前的/usr/local/redis/redis.conf的配置文件,因为不再使用这个配置文件了
[root@localhost utils]# pkill redis
[root@localhost utils]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@localhost utils]# ps -ef | grep redis | grep -v grep
root 10747 1 0 13:51 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
Redis密码设置和查看密码
redis没有实现访问控制这个功能,但是它提供了一个轻量级的认证方式,可以编辑redis.conf配置来启用认证。
1、初始化Redis密码:
在配置文件中有个参数: requirepass 这个就是配置redis访问密码的参数;
比如 requirepass test123;
(Ps:需重启Redis才能生效)
redis的查询速度是非常快的,外部用户一秒内可以尝试多大150K个密码;所以密码要尽量长(对于DBA 没有必要必须记住密码);
2、不重启Redis设置密码:
在配置文件中配置requirepass的密码(当redis重启时密码依然有效)。
redis 127.0.0.1:6379> config set requirepass test123
查询密码:
redis 127.0.0.1:6379> config get requirepass
(error) ERR operation not permitted
密码验证:
redis 127.0.0.1:6379> auth test123
OK
再次查询:
redis 127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "test123"
PS:如果配置文件中没添加密码 那么redis重启后,密码失效;
3、登陆有密码的Redis:
在登录的时候的时候输入密码:
redis-cli -p 6379 -a test123
先登陆后验证:
redis-cli -p 6379
redis 127.0.0.1:6379> auth test123
OK
AUTH命令跟其他redis命令一样,是没有加密的;阻止不了攻击者在网络上窃取你的密码;
认证层的目标是提供多一层的保护。如果防火墙或者用来保护redis的系统防御外部攻击失败的话,外部用户如果没有通过密码认证还是无法访问redis的。