目录
Redis
概念
Redis是NoSQL中比较常典型的一个非关系型数据库,在日常工作中也是最为常见的。Redis是一个由C语言编写的开源的、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API
这里我们引入一下 cookie 和 session ,session 一般是存在于服务器的,每次我们进行操作,都要跟这个 session 值进行校验,如果校验不上,就会重定向登录页。那么如果这个 session 放在普通的关系型数据库内,比如 Mysql ,那么首先读取的速度会相对较慢,而且会占用正常业务逻辑对于数据库的连接池,磁盘 io 等。在这种情况下,那么我们的 session 一般是存在缓存内。那么我们可以用 Redis 来存储这个 session。
安装&启动
1、下载并安装 Redis
我们可以使用wget下载,也可以将redis的包下载下来并且导入到linux中
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz $ yum install gcc tcl -y $ tar xzf redis-2.8.8.tar.gz $ cd redis-4.0.8 $ make $ mkdir /usr/local/redis $ make install PREFIX=/usr/local/redis
2、启动/停止 Redis
./redis-server
redis-cli shutdown
默认启动redis使用的是默认配置,端口号为 6379,密码为空。如果需要后台启动的话,可以使用nohup来启动。
nohup redis-server &
当然,如果需要指定redis的配置文件,那我们可以在启动命令中指定好配置文件就可以。
nohup redis-server /usr/local/redis/redis.conf &
3、redis.conf 配置
# Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. # # When protected mode is on and if: # # 1) The server is not binding explicitly to a set of addresses using the # "bind" directive. # 2) No password is configured. # # The server only accepts connections from clients connecting from the # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain # sockets. # # By default protected mode is enabled. You should disable it only if # you are sure you want clients from other hosts to connect to Redis # even if no authentication is configured, nor a specific set of interfaces # are explicitly listed using the "bind" directive. protected-mode yes ## 保护模式,保护 Redis 不受外网连接 # Accept connections on the specified port, default is 6379 (IANA #815344). # If port 0 is specified Redis will not listen on a TCP socket. port 6379 ## Redis 的端口,可自行设置 # Unix socket. # # Specify the path for the Unix socket that will be used to listen for # incoming connections. There is no default, so Redis will not listen # on a unix socket when not specified. # # unixsocket /tmp/redis.sock # unixsocketperm 700 # Close the connection after a client is idle for N seconds (0 to disable) timeout 0 # TCP keepalive. # # If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence # of communication. This is useful for two reasons: # # 1) Detect dead peers. # 2) Take the connection alive from the point of view of network # equipment in the middle. # # On Linux, the specified value (in seconds) is the period used to send ACKs. # Note that to close the connection the double of the time is needed. # On other kernels the period depends on the kernel configuration. # # A reasonable value for this option is 300 seconds, which is the new # Redis default starting with Redis 3.2.1. tcp-keepalive 300 ## tcp 连接数 # By default Redis does not run as a daemon. Use 'yes' if you need it. # Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. # 启用守护进程后,Redis会把pid写到一个pidfile中,在/var/run/redis.pid daemonize yes ## 后台启动 # Set the number of databases. The default database is DB 0, you can select # a different one on a per-connection basis using SELECT <dbid> where # dbid is a number between 0 and 'databases'-1 databases 16 ## Redis 到底起多少库 ################################ SNAPSHOTTING ################################ # # Save the DB on disk: # # save <seconds> <changes> # # Will save the DB if both the given number of seconds and the given # number of write operations against the DB occurred. # # In the example below the behaviour will be to save: # after 900 sec (15 min) if at least 1 key changed # after 300 sec (5 min) if at least 10 keys changed # after 60 sec if at least 10000 keys changed # # Note: you can disable saving completely by commenting out all "save" lines. # # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # like in the following example: # # save "" save 900 1 ## 900s 内 1 次数据更改操作,存一次数据库 save 300 10 ## 300s 内 10次数据更改操作,存一次数据库 save 60 10000 ## 60s内10000次数据更改操作,存一次数据库 # Compress string objects using LZF when dump .rdb databases? # For default that's set to 'yes' as it's almost always a win. # If you want to save some CPU in the saving child set it to 'no' but # the dataset will likely be bigger if you have compressible values or keys. rdbcompression yes # The filename where to dump the DB dbfilename dump.rdb # The working directory. # # The DB will be written inside this directory, with the filename specified # above using the 'dbfilename' configuration directive. # # The Append Only File will also be created inside this directory. # # Note that you must specify a directory here, not a file name. dir ./
这里,我们可以创建一个模板配置文件,命名为 redis6380.conf :
daemonize yes pidfile /usr/local/redis/redis.pid_6380 logfile /usr/local/redis/redis.log_6380 port 6380 maxmemory 128mb dir /usr/local/redis/redis6380 ## 该路径要自己创建 requirepass 123456 appendonly yes databases 8
然后,我们要启动该 Redis 的话,要指定该配置文件,否则会按照 redis.conf 文件启动:
./redis-server redis6380.conf
# ps -ef | grep redis ## 查看 redis 是否启动以及启动端口
root 13098 1 0 16:40 ? 00:00:00 ./redis-server *:6380
root 13105 13063 0 16:41 pts/1 00:00:00 grep redis
4、连接redis
远程连接redis,可以使用redis自带的工具redis-cli,具体使用方法如下:
redis-cli -h 127.0.0.1 -p 6379 -n 0 -h <hostname> Server hostname (default: 127.0.0.1). -p <port> Server port (default: 6379). -a <password> Password to use when connecting to the server. --help 显示帮助信息
切换数据库:
select 0 select 10
利用客户端连接: