Redis学习01——介绍与搭建环境

Redis学习01——介绍与搭建环境

一、简介

Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。

Redis 与其他 key - value 缓存产品有以下三个特点:

Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。

Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。

Redis支持数据的备份,即master-slave模式的数据备份。

可以查看:

Redis 简介

NoSql

我们可以在 windows 或者linux 下安装 Redis。

Window 下,去这个地址下载安装就行了 https://github.com/MicrosoftArchive/redis/releases

Linux下安装我们需要一个linux。我使用的是 Redhat7。

我们需要去 Redis 的官网下载最新的 redis。 https://redis.io/download

安装步骤主要分为以下几大步:

  1. 下载Redis(可以一边下载,一边进行后面的步骤)

  2. 使用yum安装编译器gcc

  3. 把下载好的 Redis上传到linux中

  4. 解压redis

  5. 编译redis

  6. 安装redis

  7. 配置redis

  8. 启动测试


二、yum配置(已经配置了的,可以跳过)

我使用的是 Redhat 7。如果你已经配置了 yum ,就跳过这步。

第1步:把光盘设备中的系统镜像挂载到/media/cdrom目录:

[root@wiming ~]# mkdir -p /media/cdrom
[root@wiming ~]# mount /dev/cdrom /media/cdrom
mount: /dev/sr0 is write-protected, mounting read-only

第2步:使用Vim文本编辑器创建Yum仓库的配置文件

name=rhel7 yum仓库的名称描述,易于识别仓库用处。。

baseurl=file:///media/cdrom 提供方式包括FTP(ftp://..)、HTTP(http://..)、本地(file:///..)

enabled=1 设置此源是否可用,1为可用,0为禁用。

gpgcheck=1 设置此源是否校验文件,1为校验,0为不校验。

[root@wiming ~]# vim /etc/yum.repos.d/rhel7.repo
[rhel7]
name=rhel7
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0

三、安装gcc

[root@wiming ~]# yum install gcc-c++
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel7                                                                                                                 | 4.1 kB  00:00:00     
(1/2): rhel7/group_gz                                                                                                 | 134 kB  00:00:00     
(2/2): rhel7/primary_db                                                                                               | 3.4 MB  00:00:01     
....省略了输出....  
Total download size: 34 M
Installed size: 83 M
Is this ok [y/d/N]: y(这里选择y)
Downloading packages:
-------------------------------------------

....省略了输出....                                                                                                                      

Dependency Installed:
  cpp.x86_64 0:4.8.2-16.el7                 gcc.x86_64 0:4.8.2-16.el7      glibc-devel.x86_64 0:2.17-55.el7         glibc-headers.x86_64 0:2.17-55.el7   
  kernel-headers.x86_64 0:3.10.0-123.el7    libmpc.x86_64 0:1.0.1-3.el7    libstdc++-devel.x86_64 0:4.8.2-16.el7    mpfr.x86_64 0:3.1.1-4.el7            

Complete!(完成)


四、安装redis

我下载的版本是 redis-4.0.2

4.1 上传Redis到linux中

这一个步骤就是把下载好的redis上传到linux中。我使用的是Xshell,可以看着文章操作

linux学习(八) XShell上传、下载本地文件到linux服务器

4.2 解压到/usr/local下

[root@wiming ~]# tar -xvf redis-4.0.2.tar.gz -C /usr/local
redis-4.0.2/
redis-4.0.2/.gitignore
redis-4.0.2/00-RELEASENOTES
redis-4.0.2/BUGS
redis-4.0.2/CONTRIBUTING
redis-4.0.2/COPYING
redis-4.0.2/INSTALL
......

4.3 编译redis

进入 redis-4.0.2 目录 使用 make 命令编译redis

[root@wiming ~]# cd /usr/local/
[root@wiming local]# ls
bin  etc  games  include  lib  lib64  libexec  redis-4.0.2  sbin  share  src
[root@wiming local]# cd redis-4.0.2/
[root@wiming redis-4.0.2]# make
cd src && make all
make[1]: Entering directory `/usr/local/redis-4.0.2/src'
...........省略输出........

4.4 安装redis

在 redis-4.0.2 目录中 使用 make PREFIX=/usr/local/redis install 命令安装 redis到/usr/local/redis 中

[root@wiming redis-4.0.2]# make PREFIX=/usr/local/redis install
cd src && make install
make[1]: Entering directory `/usr/local/redis-4.0.2/src'
    CC Makefile.dep
make[1]: Leaving directory `/usr/local/redis-4.0.2/src'
make[1]: Entering directory `/usr/local/redis-4.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 `/usr/local/redis-4.0.2/src'

4.5 拷贝配置文件到redis目录下

拷贝 redis-4.0.2 中的 redis.conf 到安装目录 redis\bin

[root@wiming bin]# cp /usr/local/redis-4.0.2/redis.conf /usr/local/redis/bin/redis.conf

4.6 修改配置文件

使用vim打开 redis.conf,修改136行 daemonize为yes
这个配置文件是 bin 下的配置文件

....
 132 ################################# GENERAL #####################################
 133 
 134 # By default Redis does not run as a daemon. Use 'yes' if you need it.
 135 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
 136 daemonize yes

....

五、启动redis

5.1 bin下主要命令说明

redis-benchmark 性能测试工具

redis-check-aof 检查修复aof文件

redis-cli 命令行客户端

redis.conf Redis配置文件

redis-server redis服务启动命令

5.2 前端启动

直接使用 /redis-server 就会前端启动,缺点就是其中完成后,不能再进行操作(除非重新打开一个终端),停止使用 ctrl+c 。

[root@wiming bin]# ./redis-server
7341:C 24 Sep 05:01:19.756 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7341:C 24 Sep 05:01:19.757 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=7341, just started
7341:C 24 Sep 05:01:19.757 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
7341:M 24 Sep 05:01:19.759 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 7341
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

7341:M 24 Sep 05:01:19.764 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7341:M 24 Sep 05:01:19.764 # Server initialized
7341:M 24 Sep 05:01:19.765 # 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.
7341:M 24 Sep 05:01:19.765 # 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.
7341:M 24 Sep 05:01:19.765 * DB loaded from disk: 0.000 seconds
7341:M 24 Sep 05:01:19.765 * Ready to accept connections

5.3 后端启动

后端启动就是启动的同时,带上配置文件,使用的命令是

./redis-server redis.conf

例如:

[root@wiming bin]# ./redis-server redis.conf 
7374:C 24 Sep 05:04:13.424 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7374:C 24 Sep 05:04:13.424 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=7374, just started
7374:C 24 Sep 05:04:13.424 # Configuration loaded
[root@wiming bin]# 

关闭服务器使用

[root@wiming bin]# ./redis-cli shutdown

六、测试

测试之前,必须打开redis服务。

测试使用 ./redis-cli 来打开一个客户端

然后使用 set 和 get 来存入和取出数据

退出客户端使用 exit

[root@wiming bin]# ./redis-server redis.conf
7422:C 24 Sep 05:09:07.642 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7422:C 24 Sep 05:09:07.642 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=7422, just started
7422:C 24 Sep 05:09:07.642 # Configuration loaded
[root@wiming bin]# ./redis-cli
127.0.0.1:6379> set username wiming
OK
127.0.0.1:6379> get username
"wiming"
127.0.0.1:6379> exit
[root@wiming bin]# ./redis-cli shutdown
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值