快取Redis

2020-08-12 22:27:47

Redis簡介

Redis(Remote Dictionary Server ),即遠端字典服務,是一個開源的使用ANSI C語言編寫、支援網路、可基於記憶體亦可持久化的日誌型、Key-Value數據庫,並提供多種語言的API。
快取:在記憶體中
作用:提升存取速率
環境說明

主機名 IP
localhost 192.168.175.150
下載Redis並解壓
[root@localhost ~]# ls
anaconda-ks.cfg  redis-6.0.6.tar.gz
[root@localhost ~]# tar xf redis-6.0.6.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  redis-6.0.6  redis-6.0.6.tar.gz

安裝編譯工具
[root@localhost ~]# yum -y install gcc gcc-c++

直接make編譯
[root@localhost redis-6.0.6]# make

此時會發現報錯
在这里插入图片描述
解決方法:升級編譯器的版本,因爲gcc版本一定要5.3以上

檢視gcc版本
[root@localhost redis-6.0.6]# gcc -v
使用內建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目標:x86_64-redhat-linux
設定爲:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
執行緒模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 

升級
[root@localhost redis-6.0.6]# yum -y install centos-release-scl && yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils && scl enable devtoolset-9 bash

再檢視gcc版本
[root@localhost redis-6.0.6]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC) 

刪除解壓包,重新編譯
[root@localhost ~]# rm -rf redis-6.0.6
[root@localhost ~]# tar xf redis-6.0.6.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  redis-6.0.6  redis-6.0.6.tar.gz
[root@localhost ~]# cd redis-6.0.6
[root@localhost redis-6.0.6]# make
[root@localhost redis-6.0.6]# cd /root/redis-6.0.6/src     進入到src,此時已經成功建立redis-cli、redis-server
[root@localhost src]# ls
acl.c              endianconv.o       notify.c           setcpuaffinity.o
acl.d              evict.c            notify.d           setproctitle.c
acl.o              evict.d            notify.o           setproctitle.d
adlist.c           evict.o            object.c           setproctitle.o
adlist.d           expire.c           object.d           sha1.c
adlist.h           expire.d           object.o           sha1.d
adlist.o           expire.o           pqsort.c           sha1.h
ae.c               fmacros.h          pqsort.d           sha1.o
ae.d               geo.c              pqsort.h           sha256.c
ae_epoll.c         geo.d              pqsort.o           sha256.d
ae_evport.c        geo.h              pubsub.c           sha256.h
ae.h               geohash.c          pubsub.d           sha256.o
ae_kqueue.c        geohash.d          pubsub.o           siphash.c
ae.o               geohash.h          quicklist.c        siphash.d
ae_select.c        geohash_helper.c   quicklist.d        siphash.o
anet.c             geohash_helper.d   quicklist.h        slowlog.c
anet.d             geohash_helper.h   quicklist.o        slowlog.d
anet.h             geohash_helper.o   rand.c             slowlog.h
anet.o             geohash.o          rand.d             slowlog.o
aof.c              geo.o              rand.h             solarisfixes.h
aof.d              gopher.c           rand.o             sort.c
aof.o              gopher.d           rax.c              sort.d
asciilogo.h        gopher.o           rax.d              sort.o
atomicvar.h        help.h             rax.h              sparkline.c
bio.c              hyperloglog.c      rax_malloc.h       sparkline.d
bio.d              hyperloglog.d      rax.o              sparkline.h
bio.h              hyperloglog.o      rdb.c              sparkline.o
bio.o              intset.c           rdb.d              stream.h
bitops.c           intset.d           rdb.h              syncio.c
bitops.d           intset.h           rdb.o              syncio.d
bitops.o           intset.o           redisassert.h      syncio.o
blocked.c          latency.c          redis-benchmark    testhelp.h
blocked.d          latency.d          redis-benchmark.c  t_hash.c
blocked.o          latency.h          redis-benchmark.d  t_hash.d
childinfo.c        latency.o          redis-benchmark.o  t_hash.o
childinfo.d        lazyfree.c         redis-check-aof    timeout.c
childinfo.o        lazyfree.d         redis-check-aof.c  timeout.d
cluster.c          lazyfree.o         redis-check-aof.d  timeout.o
cluster.d          listpack.c         redis-check-aof.o  t_list.c
cluster.h          listpack.d         redis-check-rdb    t_list.d
cluster.o          listpack.h         redis-check-rdb.c  t_list.o
config.c           listpack_malloc.h  redis-check-rdb.d  tls.c
config.d           listpack.o         redis-check-rdb.o  tls.d
config.h           localtime.c        redis-cli          tls.o
config.o           localtime.d        redis-cli.c        tracking.c
connection.c       localtime.o        redis-cli.d        tracking.d
connection.d       lolwut5.c          redis-cli.o        tracking.o
connection.h       lolwut5.d          redismodule.h      t_set.c
connection.o       lolwut5.o          redis-sentinel     t_set.d
connhelpers.h      lolwut6.c          redis-server       t_set.o
crc16.c            lolwut6.d          redis-trib.rb      t_stream.c
crc16.d            lolwut6.o          release.c          t_stream.d
crc16.o            lolwut.c           release.d          t_stream.o
crc16_slottable.h  lolwut.d           release.h          t_string.c
crc64.c            lolwut.h           release.o          t_string.d
crc64.d            lolwut.o           replication.c      t_string.o
crc64.h            lzf_c.c            replication.d      t_zset.c
crc64.o            lzf_c.d            replication.o      t_zset.d
crcspeed.c         lzf_c.o            rio.c              t_zset.o
crcspeed.d         lzf_d.c            rio.d              util.c
crcspeed.h         lzf_d.d            rio.h              util.d
crcspeed.o         lzf_d.o            rio.o              util.h
db.c               lzf.h              scripting.c        util.o
db.d               lzfP.h             scripting.d        valgrind.sup
db.o               Makefile           scripting.o        version.h
debug.c            memtest.c          sdsalloc.h         ziplist.c
debug.d            memtest.d          sds.c              ziplist.d
debugmacro.h       memtest.o          sds.d              ziplist.h
debug.o            mkreleasehdr.sh    sds.h              ziplist.o
defrag.c           module.c           sds.o              zipmap.c
defrag.d           module.d           sentinel.c         zipmap.d
defrag.o           module.o           sentinel.d         zipmap.h
dict.c             modules            sentinel.o         zipmap.o
dict.d             multi.c            server.c           zmalloc.c
dict.h             multi.d            server.d           zmalloc.d
dict.o             multi.o            server.h           zmalloc.h
endianconv.c       networking.c       server.o           zmalloc.o
endianconv.d       networking.d       setcpuaffinity.c
endianconv.h       networking.o       setcpuaffinity.d

把用戶端和伺服器端放到/usr/bin位置去
[root@localhost src]# cp redis-cli redis-server /usr/bin/
[root@localhost ~]# which redis-cli
/usr/bin/redis-cli
[root@localhost ~]# which redis-server
/usr/bin/redis-server

啓動redis-server
[root@localhost ~]# redis-server
86415:C 12 Aug 2020 21:40:47.005 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
86415:C 12 Aug 2020 21:40:47.005 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=86415, just started
86415:C 12 Aug 2020 21:40:47.005 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
86415:M 12 Aug 2020 21:40:47.006 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 86415
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

86415:M 12 Aug 2020 21:40:47.009 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
86415:M 12 Aug 2020 21:40:47.009 # Server initialized
86415:M 12 Aug 2020 21:40:47.009 # 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.
86415:M 12 Aug 2020 21:40:47.009 # 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.
86415:M 12 Aug 2020 21:40:47.010 * Ready to accept connections
[root@localhost ~]# ss -antl        //6379埠號已啓動
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128             *:6379                        *:*                  
LISTEN     0      128             *:22                          *:*                  
LISTEN     0      100     127.0.0.1:25                          *:*                  
LISTEN     0      128            :::6379                       :::*                  
LISTEN     0      128            :::22                         :::*                  
LISTEN     0      100           ::1:25                         :::*          

連線Redis
[root@localhost ~]# redis-cli

set用法
[root@localhost ~]# redis-cli
127.0.0.1:6379> set a 10    //設定a=10
OK

get用法
127.0.0.1:6379> get a    //獲取a的值
"10"
也可以在redis外面設定
[root@localhost ~]# redis-cli get a
"10"
[root@localhost ~]# redis-cli set b 20
OK
[root@localhost ~]# redis-cli get b
"20"

把組態檔放到該放的位置即/etc
[root@localhost ~]# cd redis-6.0.6
[root@localhost redis-6.0.6]# ls
00-RELEASENOTES  deps       README.md        runtest-moduleapi  tests
BUGS             INSTALL    redis.conf       runtest-sentinel   TLS.md
CONTRIBUTING     Makefile   runtest          sentinel.conf      utils
COPYING          MANIFESTO  runtest-cluster  src
[root@localhost redis-6.0.6]# cp redis.conf /etc/

修改組態檔去設定密碼
[root@localhost ~]# vim /etc/redis.conf
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatiblity
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
requirepass linux20200812                 //取消#號,並設定密碼

把redis放在後台執行,如果後臺是開啓的需要關閉之後再輸入下面 下麪命令
[root@localhost ~]# nohup redis-server /etc/redis.conf &
[1] 59743
[root@localhost ~]# nohup: 忽略輸入並把輸出追加到"nohup.out"
[root@localhost ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128     127.0.0.1:6379                        *:*                  
LISTEN     0      128             *:22                          *:*                  
LISTEN     0      100     127.0.0.1:25                          *:*                  
LISTEN     0      128            :::22                         :::*                  
LISTEN     0      100           ::1:25                         :::*             

登錄進去此時需要密碼
[root@localhost ~]# redis-cli
127.0.0.1:6379> set a 10
(error) NOAUTH Authentication required.  

輸入密碼登錄
127.0.0.1:6379> auth linux20200812
OK

redisx相關命令

APPEND追加一個值到key上
127.0.0.1:6379> APPEND mykey "hello"
(integer) 5
127.0.0.1:6379> APPEND mykey "world"
(integer) 10
127.0.0.1:6379> GET mykey
"helloworld"

AUTH接密碼進入redis
127.0.0.1:6379> auth linux20200812
OK

BITCOUNT 統計key位元組
127.0.0.1:6379> BITCOUNT mykey
(integer) 44

TIME返回當前伺服器時間
127.0.0.1:6379> TIME
1) "1597242301"
2) "424832"

KEYS檢視當前有多少key
127.0.0.1:6379> KEYS *
1) "mykey"
127.0.0.1:6379> quit
[root@localhost ~]#