Redis介绍、安装部署、操作

一、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基本知识
1、Redis的数据类型:
  字符串、列表(lists)、集合(sets)、有序集合(sorts sets)、哈希表(hashs)
2、Redis和memcache相比的独特之处:
  (1)redis可以用来做存储(storge)、而memcache是来做缓存(cache)。这个特点主要是因为其有“持久化”功能
  (2)存储的数据有“结构”,对于memcache来说,存储的数据,只有一种类型——“字符串”,而redis则可以存储字符串、链表、集合、有序集合、哈序结构
3、持久化的两种方式:
  Redis将数据存储于内存中,或被配置为使用虚拟内存。
  实现数据持久化的两种方式:(1)使用截图的方式,将内存中的数据不断写入磁盘(性能高,但可能会引起一定程度的数据丢失)
               (2)使用类似mysql的方式,记录每次更新的日志
4、Redis的主从同步:对提高读取性能非常有益
5、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.2]# ll
total 248
-rw-rw-r--.  1 root root 85327 Nov 22 18:26 00-RELEASENOTES
-rw-rw-r--.  1 root root    53 Nov 22 18:26 BUGS
-rw-rw-r--.  1 root root  1894 Nov 22 18:26 CONTRIBUTING
-rw-rw-r--.  1 root root  1487 Nov 22 18:26 COPYING
drwxrwxr-x.  6 root root  4096 Nov 22 18:26 deps
-rw-rw-r--.  1 root root    11 Nov 22 18:26 INSTALL
-rw-rw-r--.  1 root root   151 Nov 22 18:26 Makefile
-rw-rw-r--.  1 root root  4223 Nov 22 18:26 MANIFESTO
-rw-rw-r--.  1 root root 20555 Nov 22 18:26 README.md
-rw-rw-r--.  1 root root 62155 Nov 22 18:26 redis.conf
-rwxrwxr-x.  1 root root   275 Nov 22 18:26 runtest
-rwxrwxr-x.  1 root root   280 Nov 22 18:26 runtest-cluster
-rwxrwxr-x.  1 root root   281 Nov 22 18:26 runtest-sentinel
-rw-rw-r--.  1 root root  9710 Nov 22 18:26 sentinel.conf
drwxrwxr-x.  3 root root  4096 Nov 22 18:26 src
drwxrwxr-x. 10 root root  4096 Nov 22 18:26 tests
drwxrwxr-x.  8 root root  4096 Nov 22 18:26 utils
[root@centos04 redis-5.0.2]# 

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

[root@centos04 redis-5.0.2]# make
cd src && make all
make[1]: Entering directory `/opt/redis/redis-5.0.2/src'
    CC Makefile.dep
make[1]: Leaving directory `/opt/redis/redis-5.0.2/src'
make[1]: Entering directory `/opt/redis/redis-5.0.2/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[2]: Entering directory `/opt/redis/redis-5.0.2/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[2]: Leaving directory `/opt/redis/redis-5.0.2/deps'
(rm -f .make-*)
......(略)
...... (略)
Hint: It's a good idea to run 'make test' ;)
 
make[1]: Leaving directory `/opt/redis/redis-5.0.2/src'
[root@centos04 redis-5.0.2]# 

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

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

  查看Redis是否安装成功

[root@centos04 redis-5.0.2]# cd /opt/redis/
[root@centos04 redis]# ll
total 1920
drwxr-xr-x. 2 root root    4096 Nov 22 20:38 bin

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

[root@centos04 redis]# cd bin/
[root@centos04 bin]# ll
total 54316
-rwxr-xr-x. 1 root root  9200155 Nov 22 20:38 redis-benchmark
-rwxr-xr-x. 1 root root 12251871 Nov 22 20:38 redis-check-aof
-rwxr-xr-x. 1 root root 12251871 Nov 22 20:38 redis-check-rdb
-rwxr-xr-x. 1 root root  9564766 Nov 22 20:38 redis-cli     --->客户端
lrwxrwxrwx. 1 root root       12 Nov 22 20:38 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 12251871 Nov 22 20:38 redis-server    --->服务端
[root@centos04 bin]# 

1、前端启动与停止

  1)前端启动的命令:

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

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

[root@centos04 bin]# ./redis-server 
26765:C 22 Nov 2018 20:46:31.066 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
26765:C 22 Nov 2018 20:46:31.066 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=26765, just started
26765:C 22 Nov 2018 20:46: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
26765:M 22 Nov 2018 20:46:31.067 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 26765
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               
 
26765:M 22 Nov 2018 20:46:31.104 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
26765:M 22 Nov 2018 20:46:31.104 # Server initialized
26765:M 22 Nov 2018 20:46:31.104 # 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.
26765:M 22 Nov 2018 20:46: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.
26765:M 22 Nov 2018 20:46:31.105 * Ready to accept connections

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

强制关闭:Ctrl+c

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

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

[root@centos04 bin]# ./redis-cli shutdown
26765:M 22 Nov 2018 20:52:43.151 # User requested shutdown...
26765:M 22 Nov 2018 20:52:43.151 * Saving the final RDB snapshot before exiting.
26765:M 22 Nov 2018 20:52:43.161 * DB saved on disk
26765:M 22 Nov 2018 20:52: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.2/redis.conf /opt/redis/bin/
[root@centos04 bin]# ll
total 54388
-rw-r--r--. 1 root root       92 Nov 22 20:52 dump.rdb
-rwxr-xr-x. 1 root root  9200155 Nov 22 20:38 redis-benchmark
-rwxr-xr-x. 1 root root 12251871 Nov 22 20:38 redis-check-aof
-rwxr-xr-x. 1 root root 12251871 Nov 22 20:38 redis-check-rdb
-rwxr-xr-x. 1 root root  9564766 Nov 22 20:38 redis-cli
-rw-r--r--. 1 root root    62155 Nov 22 20:57 redis.conf
lrwxrwxrwx. 1 root root       12 Nov 22 20:38 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 12251871 Nov 22 20:38 redis-server
[root@centos04 bin]# 

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

[root@centos04 bin]# vim redis.conf 
# 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
27131:C 22 Nov 2018 21:09:41.117 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
27131:C 22 Nov 2018 21:09:41.117 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=27131, just started
27131:C 22 Nov 2018 21:09: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.8/FAQ
root     27132  0.1  0.4 152416  7796 ?        Ssl  21:09   0:00 ./redis-server 127.0.0.1:6379
root     27153  0.0  0.0 103252   840 pts/1    S+   21:10   0:00 grep redis
[root@centos04 bin]# 

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

强制关闭:[root@localhost bin]# kill -9 进程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 6379
-h:指定访问的redis服务器的ip地址
-p:指定访问的redis服务器的port端口

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

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

  进入客户端:

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

  2)关闭/退出客户端

ctrl+c

127.0.0.1:6379> 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   傻瓜式安装,点击下一步就行。

  

  

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

  

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

  

  

猜你喜欢

转载自www.cnblogs.com/fameg/p/10013656.html
今日推荐