Redis介绍、安装部署、操作

学习连接:http://www.runoob.com/redis/redis-tutorial.html

一、Redis介绍

  Redis是NoSql的一种。

  NoSql,全名:Not Only Sql,是一种非关系型数据库,它不能替代关系弄数据库,只是关系型数据库的一个补充,是可以解决高并发、高可用、高扩展、大数据存储等一系列问题而产生的数据库解决方案。

NoSql有以下4种分类:键值(Key-Value)存储数据库、列存储数据库、文档型数据库、图形(Graph)数据库。而Redis属于第一种:键值(Key-Value)存储数据库。

  Redis是使用c语言开发的一个高性能键值数据库,即通过一些键值类型来存储数据。Redis支持的键值类型有:String字符类型、map散列类型、list列表类型、set集合类型、sortedset有序集合类型。

redis的应用场景如下:
  缓存(数据查询、短连接、新闻内容、商品内容等等)、分布式集群架构中的session分离、聊天室的在线好友列表、任务队列。   (秒杀、抢购、12306等等)、应用排行榜、网站访问统计、数据过期处理(可以精确到毫秒)。   作为缓存的应用场景是最多的。
   
Redis优点:

  异常快速 : Redis是非常快的,每秒可以执行大约110000设置操作,81000个/每秒的读取操作。
  支持丰富的数据类型 : Redis支持最大多数开发人员已经知道如列表,集合,可排序集合,哈希等数据类型。
  这使得在应用中很容易解决的各种问题,因为我们知道哪些问题处理使用哪种数据类型更好解决。
  操作都是原子的 : 所有 Redis 的操作都是原子,从而确保当两个客户同时访问 Redis 服务器得到的是更新后的值(最新值)。
  MultiUtility工具:Redis是一个多功能实用工具,可以在很多如:缓存,消息传递队列中使用(Redis原生支持发布/订阅),在应用程序中,如:Web应用程序会话,网站页面点击数等任何短暂的数据;
二、Redis基本知识
、Redis的数据类型:
  字符串、列表(lists)、集合(sets)、有序集合(sorts sets)、哈希表(hashs)
、Redis和memcache相比的独特之处:
  ()redis可以用来做存储(storge)、而memcache是来做缓存(cache)。这个特点主要是因为其有“持久化”功能
  ()存储的数据有“结构”,对于memcache来说,存储的数据,只有一种类型——“字符串”,而redis则可以存储字符串、链表、集合、有序集合、哈序结构
、持久化的两种方式:
  Redis将数据存储于内存中,或被配置为使用虚拟内存。
  实现数据持久化的两种方式:()使用截图的方式,将内存中的数据不断写入磁盘(性能高,但可能会引起一定程度的数据丢失)
               ()使用类似mysql的方式,记录每次更新的日志
、Redis的主从同步:对提高读取性能非常有益
、Redis服务端的默认端口是6379

  

三、redis安装部署

一般redis安装于linux服务器,我这里是Linux下的安装,也支持window或mac,请自行百度安装方法。

我这里使用的Linux系统是CentOS release 6.5。

1、Redis下载

  可以到redis的官网找到各个Redis版本的下载地址,如:http://download.redis.io/releases      https://redis.io/download

  我这里用的是:redis-5.0.2.tar.gz

  上传、解压过程略

  我的路经:/opt/redis/redis-5.0.2

2、编译安装Redis

  进入redis源码:
  cd  /opt/redis/redis-5.0.2  

[root@centos04 redis-5.0.]# ll
total
-rw-rw-r--. root root Nov : -RELEASENOTES
-rw-rw-r--. root root Nov : BUGS
-rw-rw-r--. root root Nov : CONTRIBUTING
-rw-rw-r--. root root Nov : COPYING
drwxrwxr-x. root root Nov : deps
-rw-rw-r--. root root Nov : INSTALL
-rw-rw-r--. root root Nov : Makefile
-rw-rw-r--. root root Nov : MANIFESTO
-rw-rw-r--. root root Nov : README.md
-rw-rw-r--. root root Nov : redis.conf
-rwxrwxr-x. root root Nov : runtest
-rwxrwxr-x. root root Nov : runtest-cluster
-rwxrwxr-x. root root Nov : runtest-sentinel
-rw-rw-r--. root root Nov : sentinel.conf
drwxrwxr-x. root root Nov : src
drwxrwxr-x. root root Nov : tests
drwxrwxr-x. root root Nov : utils
[root@centos04 redis-5.0.]#

  使用make命令编译Redis需要c语言环境,CentOS自带c语言环境,若是使用其他Linux系统中没有c语言环境,则需要安装。

[root@centos04 redis-5.0.]# make
cd src && make all
make[]: Entering directory `/opt/redis/redis-5.0./src'
CC Makefile.dep
make[]: Leaving directory `/opt/redis/redis-5.0./src'
make[]: Entering directory `/opt/redis/redis-5.0./src'
rm -rf redis-server redis-sentinel redis-cli redis-benchmark redis-check-rdb redis-check-aof *.o *.gcda *.gcno *.gcov redis.info lcov-html Makefile.dep dict-benchmark
(cd ../deps && make distclean)
make[]: Entering directory `/opt/redis/redis-5.0./deps'
(cd hiredis && make clean) > /dev/null || true
(cd linenoise && make clean) > /dev/null || true
(cd lua && make clean) > /dev/null || true
(cd jemalloc && [ -f Makefile ] && make distclean) > /dev/null || true
(rm -f .make-*)
make[]: Leaving directory `/opt/redis/redis-5.0./deps'
(rm -f .make-*)
......(略)
...... (略)
Hint: It's a good idea to run 'make test' ;) make[]: Leaving directory `/opt/redis/redis-5.0./src'
[root@centos04 redis-5.0.]#

  编译过后,就是安装了,安装Redis的命令如下:
  make install PREFIX=/opt/redis  

[root@centos04 redis-5.0.]# make install PREFIX=/opt/redis
cd src && make install
make[]: Entering directory `/opt/redis/redis-5.0./src' Hint: It's a good idea to run 'make test' ;) INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[]: Leaving directory `/opt/redis/redis-5.0./src'
[root@centos04 redis-5.0.]#
该命令中,前面的”make install PREFIX=”是固定的,而”/opt/redis”是Redis的安装目录,一般就这么写,如若需要安装在其他地方,只需将此路径更换即可。

  查看Redis是否安装成功

[root@centos04 redis-5.0.]# cd /opt/redis/
[root@centos04 redis]# ll
total
drwxr-xr-x. root root Nov : bin

四、Redis启动与停止
  Redis有两种启动,分别是:前端启动、后端启动。要启动Redis,就需要到Redis的bin目录下执行启动命令,先看看bin目录结构:

[root@centos04 redis]# cd bin/
[root@centos04 bin]# ll
total
-rwxr-xr-x. root root Nov : redis-benchmark
-rwxr-xr-x. root root Nov : redis-check-aof
-rwxr-xr-x. root root Nov : redis-check-rdb
-rwxr-xr-x. root root Nov : redis-cli --->客户端
lrwxrwxrwx. root root Nov : redis-sentinel -> redis-server
-rwxr-xr-x. root root Nov : redis-server --->服务端
[root@centos04 bin]#
redis/bin目录下的几个文件说明:
  redis-benchmark:redis性能测试工具
  redis-check-aof:检查aof日志的工具
  redis-check-dump:检查rdb日志的工具
  redis-cli:连接用的客户端
  redis-server:redis服务进程

1、前端启动与停止

  1)前端启动的命令:

[root@centos04 bin]# ./redis-server 

  启动后可以看到Redis的启动端口为6379(默认),进程id是26765,同时,前端启动Redis后,终端将进入Redis控制台,没办法继续别的Linux命令,即这个终端窗口就”废了”,只能输入Redis自己的命令。下图:Port: 6379     PID: 26765

[root@centos04 bin]# ./redis-server
:C Nov ::31.066 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
:C Nov ::31.066 # Redis version=5.0., bits=, commit=, modified=, pid=, just started
:C Nov ::31.066 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
:M Nov ::31.067 * Increased maximum number of open files to (it was originally set to ).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0. (/) bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port:
| `-._ `._ / _.-' | PID: 26765
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-' :M Nov ::31.104 # WARNING: The TCP backlog setting of cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of .
:M Nov ::31.104 # Server initialized
:M Nov ::31.104 # WARNING overcommit_memory is set to ! 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.
:M Nov ::31.105 # 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.
:M Nov ::31.105 * Ready to accept connections

  2)前端启动的关闭命令:

强制关闭:Ctrl+c

正常关闭:[root@localhost bin]# ./redis-cli shutdown

  对这两个命令进行对比:
    强制关闭只需在Redis控制台直接执行即可(redis可能会丢失部分数据)。
    正常关闭需要另开一个终端窗口才可执行(redis不会丢失数据,推荐使用)。

[root@centos04 bin]# ./redis-cli shutdown
:M  Nov  ::43.151 # User requested shutdown...
:M Nov ::43.151 * Saving the final RDB snapshot before exiting.
:M Nov ::43.161 * DB saved on disk
:M Nov ::43.161 # Redis is now ready to exit, bye bye...
[root@centos04 bin]#
需要注意一点,一旦前端启动的关闭命令执行,则redis控制台关闭,redis服务也会停掉。

2、后端启动与停止

  后端启动是我们开发中绝对会用到的方式,但在使用后端启动命令之后,需要做如下几步配置:

  1)后端启动的配置:

  第一步,需要把redis源码目录下的redis.conf文件复制到redis安装目录的bin目录下。  

[root@centos04 bin]# cp /opt/redis/redis-5.0./redis.conf /opt/redis/bin/
[root@centos04 bin]# ll
total
-rw-r--r--. root root Nov : dump.rdb
-rwxr-xr-x. root root Nov : redis-benchmark
-rwxr-xr-x. root root Nov : redis-check-aof
-rwxr-xr-x. root root Nov : redis-check-rdb
-rwxr-xr-x. root root Nov : redis-cli
-rw-r--r--. root root Nov : redis.conf
lrwxrwxrwx. root root Nov : redis-sentinel -> redis-server
-rwxr-xr-x. root root Nov : redis-server
[root@centos04 bin]#

  第二步,修改redis.conf文件,将daemonize的值改为yes后保存。

[root@centos04 bin]# vim redis.conf 

Redis的配置
  daemonize:如需要在后台运行,把该项的值改为yes
  pdifile:把pid文件放在/var/run/redis.pid,可以配置到其他地址
  bind:指定redis只接收来自该IP的请求,如果不设置,那么将处理所有请求,在生产环节中最好设置该项
  port:监听端口,默认为6379
  timeout:设置客户端连接时的超时时间,单位为秒
  loglevel:等级分为4级,debug,revbose,notice和warning。生产环境下一般开启notice
  logfile:配置log文件地址,默认使用标准输出,即打印在命令行终端的端口上
  database:设置数据库的个数,默认使用的数据库是0
  save:设置redis进行数据库镜像的频率
  rdbcompression:在进行镜像备份时,是否进行压缩
  dbfilename:镜像备份文件的文件名
  dir:数据库镜像备份的文件放置的路径
  slaveof:设置该数据库为其他数据库的从数据库
  masterauth:当主数据库连接需要密码验证时,在这里设定
  requirepass:设置客户端连接后进行任何其他指定前需要使用的密码
  maxclients:限制同时连接的客户端数量
  maxmemory:设置redis能够使用的最大内存
  appendonly:开启appendonly模式后,redis会把每一次所接收到的写操作都追加到appendonly.aof文件中,当redis重新启动时,会从该文件恢复出之前的状态
  appendfsync:设置appendonly.aof文件进行同步的频率
  vm_enabled:是否开启虚拟内存支持
  vm_swap_file:设置虚拟内存的交换文件的路径
  vm_max_momery:设置开启虚拟内存后,redis将使用的最大物理内存的大小,默认为0
  vm_page_size:设置虚拟内存页的大小
  vm_pages:设置交换文件的总的page数量
  vm_max_thrrads:设置vm IO同时使用的线程数量
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no ---> # By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
如果发现内容太多,可以通过命令来查找要修改的内容:
  /demonize no --->向下查找
  ?demonize no --->向上查找

    经过上面配置后,以后就无需再配置,下面就可以通过命令让redis后台启动了。

  2)后端启动的命令:

[root@centos04 bin]# ./redis-server redis.conf
[root@centos04 bin]# ./redis-server redis.conf
:C Nov ::41.117 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
:C Nov ::41.117 # Redis version=5.0., bits=, commit=, modified=, pid=, just started
:C Nov ::41.117 # Configuration loaded
[root@centos04 bin]#

    后端启动命令加了” redis.conf”参数,让redis根据这个配置文件的配置运行。

    在启动完redis后台,终端不会进入redis控制台,这就是将redis运行后台

    我们可以查查看系统现在是不是有redis的进程:

[root@centos04 bin]# ps -aux | grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2./FAQ
root 0.1 0.4 ? Ssl : : ./redis-server 127.0.0.1:
root 0.0 0.0 pts/ S+ : : grep redis
[root@centos04 bin]#

  3)后端启动的关闭命令:

强制关闭:[root@localhost bin]# kill - 进程id

正常关闭:[root@localhost bin]# ./redis-cli shutdown
项目中,建议使用正常关闭。
因为redis作为缓存来使用的话,将数据存储到内存中,如果使用正常关闭,则会将内存数据持久化到本地之后,再关闭。如果强制关闭,则不会进行持久化操作,可能会造成部分数据丢失。

五、Redis客户端

1、redis自带客户端

  在前面介绍redis安装目录下bin目录的结构时,就已经标记出了redis的客户端,它就是redis-cli。

  这个客户端有两个常用的功能:

  1. 用来正常关闭redis服务。
  2. 让终端进入redis控制台(后台运行redis的场景下用到)。

  1)启动启动客户端命令:

[root@centos04 bin]# ./redis-cli -h 127.0.0.1 -p 
-h:指定访问的redis服务器的ip地址
-p:指定访问的redis服务器的port端口

  如果使用的ip地址与端口都是默认的,则:

[root@centos04 bin]# ./redis-cli
使用默认配置:默认的ip【127.0.0.1】,默认的port【】

  进入客户端:

[root@centos04 bin]# ./redis-cli -h 127.0.0.1 -p
127.0.0.1:>

  2)关闭/退出客户端

ctrl+c

127.0.0.1:> quit

    要关闭该客户端,使用上述两个命令中任意一个均可,都不会影响redis数据的保存。  

2、图形界面客户端

  有一个redis的图形界面客户端软件,名为redis-destop-manager。

  支持Windows、Mac OS X、Linux,请根据自己的电脑系统选择下载,这里以windows为例,简单说下这软件的使用,安装很简单,一路下一步即可,安装后打开该应用。

  下载 https://redisdesktop.com/download,百度网盘:https://pan.baidu.com/s/1iM1kIwahhs9vV-mAQLV41A

   redis-desktop-manager-0.8.8.384.exe   傻瓜式安装,点击下一步就行。

  Redis介绍、安装部署、操作

  Redis介绍、安装部署、操作

  1)打开redis服务器连接配置

  Redis介绍、安装部署、操作

  2)添加redis服务器连接并测试是否成功连接

  Redis介绍、安装部署、操作

  配置IP、端口测试,可以添加auth配置信息,该信息在redis.conf中配置,上文有说明。

  Redis介绍、安装部署、操作

  连接成功,Redis默认为15个库,这个库的个数也是可以配置的,在redis.conf配置文件中databases的值为改变。

  Redis介绍、安装部署、操作

注:

在这里配置IP的时候有一个问题,redis-desktop-manager配置后连接不上服务端,并且打开的redis-desktop-manager客户端会崩溃。

  在防火墙关闭、redis的redis.conf配置文件中bind 127.0.0.1也注释掉的情况下还是不行。

       然后就将redis的redis.conf配置文件中改为bind 192.168.48.104---我安装redis虚拟机的IP。如下:

# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1
bind 192.168.48.104 修改的配置信息。

      redis-desktop-manager客户端可以连接了,但是linux命令行无法通过默认的方法连接。

默认和合端口
[root@centos04 bin]# ./redis-cli
Could not connect to Redis at 127.0.0.1:: Connection refused
not connected> 指定IP和端口(虚拟机本地IP)
[root@centos04 bin]# ./redis-cli -h 192.168.48.104 -p
192.168.48.104:> select
OK
192.168.48.104:[]>

查看了redis配置文件redis.conf,发现可以配置多个绑定IP。配置后重启Redis,发现没有问题。如下:

# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1
bind 192.168.48.104 127.0.0.1 多个IP中间用空格隔开。

3、代码控制客户端

  redis不仅可以使用命令来操作。

  目前基本上主流的语言都有客户端支持,比如:java、Python、C、C#、C++、php、Node.js、Go等。  

  在官方网站里有一些Java的客户端,如:Jedis、Redisson、Jredis、JDBC-Redis等,其中官方推荐使用Jedis和Redisson。

  在企业中用的最多的就是Jedis。 https://github.com/xetorthio/jedis

  - - - - - - - - - - 后续更新 - - - - - - - - - -

六、Redis 数据库操作

  转载: http://www.cnblogs.com/alex3714/articles/6217453.html

  

  

上一篇:SQL SERVER ->> BCP导出数据到平面文件


下一篇:Redis介绍、安装、配置