• 【Redis】02 Redis 搭建与操作


    Redis的安装及启动停止

    官网地址:

    https://redis.io/download

    使用wget命令下载redis

    wget 下载地址

    下载:

    [root@VM-0-7-centos ~]# wget http://download.redis.io/releases/redis-6.0.6.tar.gz?_ga=2.77545751.366490453.1596769060-1648792050.1596769060
    --2020-08-07 10:58:00--  http://download.redis.io/releases/redis-6.0.6.tar.gz?_ga=2.77545751.366490453.1596769060-1648792050.1596769060
    Resolving download.redis.io (download.redis.io)... 45.60.125.1
    Connecting to download.redis.io (download.redis.io)|45.60.125.1|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 2228781 (2.1M) [application/octet-stream]
    Saving to: ‘redis-6.0.6.tar.gz?_ga=2.77545751.366490453.1596769060-1648792050.1596769060’
    
    100%[===============================================================================================================>] 2,228,781   19.6KB/s   in 87s    
    
    2020-08-07 10:59:28 (25.0 KB/s) - ‘redis-6.0.6.tar.gz?_ga=2.77545751.366490453.1596769060-1648792050.1596769060’ saved [2228781/2228781]

    发现下载之后的文件url参数也不会去掉,重命名一下:

    mv redis-6.0.6.tar.gz?_ga=2.77545751.366490453.1596769060-1648792050.1596769060 redis-6.0.6.tar.gz

    安装编译器,下载的资源是二进制文件,不能直接使用

    yum install gcc-c++ 

    yum下载安装:

    [root@VM-0-7-centos ~]# yum install gcc-c++ 
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
    docker-ce-stable                                                                                                                  | 3.5 kB  00:00:00     
    epel                                                                                                                              | 4.7 kB  00:00:00     
    extras                                                                                                                            | 2.9 kB  00:00:00     
    os                                                                                                                                | 3.6 kB  00:00:00     
    updates                                                                                                                           | 2.9 kB  00:00:00     
    (1/2): epel/7/x86_64/updateinfo                                                                                                   | 1.0 MB  00:00:00     
    (2/2): epel/7/x86_64/primary_db                                                                                                   | 6.9 MB  00:00:01     
    Resolving Dependencies
    --> Running transaction check
    ---> Package gcc-c++.x86_64 0:4.8.5-39.el7 will be installed
    --> Processing Dependency: libstdc++-devel = 4.8.5-39.el7 for package: gcc-c++-4.8.5-39.el7.x86_64
    --> Running transaction check
    ---> Package libstdc++-devel.x86_64 0:4.8.5-39.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    =========================================================================================================================================================
     Package                                   Arch                             Version                                   Repository                    Size
    =========================================================================================================================================================
    Installing:
     gcc-c++                                   x86_64                           4.8.5-39.el7                              os                           7.2 M
    Installing for dependencies:
     libstdc++-devel                           x86_64                           4.8.5-39.el7                              os                           1.5 M
    
    Transaction Summary
    =========================================================================================================================================================
    Install  1 Package (+1 Dependent package)
    
    Total download size: 8.7 M
    Installed size: 25 M
    Is this ok [y/d/N]: y
    Downloading packages:
    (1/2): libstdc++-devel-4.8.5-39.el7.x86_64.rpm                                                                                    | 1.5 MB  00:00:00     
    (2/2): gcc-c++-4.8.5-39.el7.x86_64.rpm                                                                                            | 7.2 MB  00:00:01     
    ---------------------------------------------------------------------------------------------------------------------------------------------------------
    Total                                                                                                                    6.7 MB/s | 8.7 MB  00:00:01     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : libstdc++-devel-4.8.5-39.el7.x86_64                                                                                                   1/2 
      Installing : gcc-c++-4.8.5-39.el7.x86_64                                                                                                           2/2 
      Verifying  : gcc-c++-4.8.5-39.el7.x86_64                                                                                                           1/2 
      Verifying  : libstdc++-devel-4.8.5-39.el7.x86_64                                                                                                   2/2 
    
    Installed:
      gcc-c++.x86_64 0:4.8.5-39.el7                                                                                                                          
    
    Dependency Installed:
      libstdc++-devel.x86_64 0:4.8.5-39.el7                                                                                                                  
    
    Complete!

    解压Tar包:

    tar -zxvf redis-6.0.6.tar.gz

    查看目录结构:

    [root@VM-0-7-centos redis-6.0.6]# ll
    total 264
    -rw-rw-r--  1 root root 80561 Jul 21 02:08 00-RELEASENOTES
    -rw-rw-r--  1 root root    51 Jul 21 02:08 BUGS
    -rw-rw-r--  1 root root  2381 Jul 21 02:08 CONTRIBUTING
    -rw-rw-r--  1 root root  1487 Jul 21 02:08 COPYING
    drwxrwxr-x  6 root root  4096 Jul 21 02:08 deps
    -rw-rw-r--  1 root root    11 Jul 21 02:08 INSTALL
    -rw-rw-r--  1 root root   151 Jul 21 02:08 Makefile
    -rw-rw-r--  1 root root  6888 Jul 21 02:08 MANIFESTO
    -rw-rw-r--  1 root root 20806 Jul 21 02:08 README.md
    -rw-rw-r--  1 root root 83392 Jul 21 02:08 redis.conf
    -rwxrwxr-x  1 root root   275 Jul 21 02:08 runtest
    -rwxrwxr-x  1 root root   280 Jul 21 02:08 runtest-cluster
    -rwxrwxr-x  1 root root   679 Jul 21 02:08 runtest-moduleapi
    -rwxrwxr-x  1 root root   281 Jul 21 02:08 runtest-sentinel
    -rw-rw-r--  1 root root 10743 Jul 21 02:08 sentinel.conf
    drwxrwxr-x  3 root root  4096 Jul 21 02:08 src
    drwxrwxr-x 11 root root  4096 Jul 21 02:08 tests
    -rw-rw-r--  1 root root  3055 Jul 21 02:08 TLS.md
    drwxrwxr-x  9 root root  4096 Jul 21 02:08 utils

    发现编译失败,原因是因为Redis6版本需要5版本的GCC才能编译成功

    而当前这个服务器的GCC版本只有4

    索性升级GCC:

    https://blog.csdn.net/wfx15502104112/article/details/96508940

    再次编译终于成功:

    安装到指定位置:

    make install PREFIX=/usr/local/redis 

    查看目录有这些程序表示成功:

    [root@VM-0-7-centos bin]# ll
    total 36872
    -rwxr-xr-x 1 root root 4729200 Aug  7 11:53 redis-benchmark
    -rwxr-xr-x 1 root root 9331352 Aug  7 11:53 redis-check-aof
    -rwxr-xr-x 1 root root 9331352 Aug  7 11:53 redis-check-rdb
    -rwxr-xr-x 1 root root 5018336 Aug  7 11:53 redis-cli
    lrwxrwxrwx 1 root root      12 Aug  7 11:53 redis-sentinel -> redis-server
    -rwxr-xr-x 1 root root 9331352 Aug  7 11:53 redis-server

    文件说明:

    redis-benchmark:redis性能测试工具
    redis-check-aof:检查aof日志的工具
    redis-check-dump:检查rdb日志的工具
    redis-cli:连接用的客户端
    redis-server:redis服务进程

    复制源码包中的配置文件redis.conf 存放到redis的conf目录中

    conf目录自己创建

    启动Redis

    携带配置文件作为参数项启动

    /usr/local/redis/bin/redis-server ../conf/redis.conf

    配置信息说明:

     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同时使用的线程数量

    这样的启动关闭终端即销毁:

    [root@VM-0-7-centos bin]# /usr/local/redis/bin/redis-server ../conf/redis.conf
    31817:C 07 Aug 2020 12:05:25.341 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    31817:C 07 Aug 2020 12:05:25.341 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=31817, just started
    31817:C 07 Aug 2020 12:05:25.341 # Configuration loaded
                    _._                                                  
               _.-``__ ''-._                                             
          _.-``    `.  `_.  ''-._           Redis 6.0.6 (00000000/0) 64 bit
      .-`` .-```.  ```/    _.,_ ''-._                                   
     (    '      ,       .-`  | `,    )     Running in standalone mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
     |    `-._   `._    /     _.-'    |     PID: 31817
      `-._    `-._  `-./  _.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |           http://redis.io        
      `-._    `-._`-.__.-'_.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |                                  
      `-._    `-._`-.__.-'_.-'    _.-'                                   
          `-._    `-.__.-'    _.-'                                       
              `-._        _.-'                                           
                  `-.__.-'                                               
    
    31817:M 07 Aug 2020 12:05:25.342 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    31817:M 07 Aug 2020 12:05:25.342 # Server initialized
    31817:M 07 Aug 2020 12:05:25.342 # 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.
    31817:M 07 Aug 2020 12:05:25.342 # 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.
    31817:M 07 Aug 2020 12:05:25.343 * Ready to accept connections
    ^C31817:signal-handler (1596773129) Received SIGINT scheduling shutdown...
    31817:M 07 Aug 2020 12:05:29.751 # User requested shutdown...
    31817:M 07 Aug 2020 12:05:29.751 * Saving the final RDB snapshot before exiting.
    31817:M 07 Aug 2020 12:05:29.755 * DB saved on disk
    31817:M 07 Aug 2020 12:05:29.755 * Removing the pid file.
    31817:M 07 Aug 2020 12:05:29.755 # Redis is now ready to exit, bye bye...

    VIM打开redis.conf设置后台运行:

    更改为YES

    行号开启:

    :setnumber

    再次启动会以后台程序启动:

    [root@VM-0-7-centos conf]# /usr/local/redis/bin/redis-server ../conf/redis.conf
    32447:C 07 Aug 2020 12:10:18.741 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    32447:C 07 Aug 2020 12:10:18.741 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=32447, just started
    32447:C 07 Aug 2020 12:10:18.741 # Configuration loaded

    客户端访问:

    /usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379

    退出客户端:

    quit

    关闭Redis:

    正常关闭redis服务:

    /usr/local/redis/bin/redis-server shutdown

    以杀死进程方式关闭:

    pkill redis-server

    设置开机自启动Redis:

    打开启动加载项:

    vim /etc/rc.local

    空白处加入启动命令即可:

    /usr/local/redis/bin/redis-server ../conf/redis.conf
  • 相关阅读:
    C# 动态加载控件 (转)
    SQL 周月时间段
    js调用xml数据显示div层上下滚动的公告
    使用半透明的DIV实现禁用页面功能(转)
    学习jQuery第一天"Hello!How are you!"
    HTML生成类
    从无限畅想转来的
    SQL递归查询子类型
    js 图片缩显
    2020年统计用区划代码和城乡划分代码:安徽省乡镇编码
  • 原文地址:https://www.cnblogs.com/mindzone/p/13452056.html
Copyright © 2020-2023  润新知