1. Redis 簡介
Redis 是完全開源免費的,遵守BSD協議,是一個高性能的key-value數據庫。
Redis 與其他 key - value 緩存產品有以下三個特點:
- Redis支持數據的持久化,可以將內存中的數據保存在磁盤中,重啟的時候可以再次加載進行使用。
- Redis不僅僅支持簡單的key-value類型的數據,同時還提供list,set,zset,hash等數據結構的存儲。
- Redis支持數據的備份,即master-slave模式的數據備份。
2. Redis 優勢
- 性能極高 – Redis能讀的速度是110000次/s,寫的速度是81000次/s 。
- 豐富的數據類型 – Redis支持二進制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 數據類型操作。
- 原子 – Redis的所有操作都是原子性的,意思就是要么成功執行要么失敗完全不執行。單個操作是原子性的。多個操作也支持事務,即原子性,通過MULTI和EXEC指令包起來。
- 豐富的特性 – Redis還支持 publish/subscribe, 通知, key 過期等等特性。
3. Redis安裝及配置
//下載並解壓redis安裝包
[root@localhost ~]# wget http://download.redis.io/releases/redis-6.0.6.tar.gz?_ga=2.201306478.1396430506.1597239708-1651673018.1597239708
[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
//在編譯之前請安裝好gcc、gcc-c++,然后查看gcc的版本是否過低,我這里的版本就過低了,這時編譯的話,會直接報錯的!!如果你的gcc版本也和我這一樣,或者更低,就需要升級了!
[root@localhost ~]# yum -y install gcc gcc-c++ tcl
[root@localhost ~]# 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)
//升級gcc
[root@localhost redis-6.0.6]# yum -y install centos-release-scl
............
[root@localhost redis-6.0.6]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
............
[root@localhost redis-6.0.6]# scl enable devtoolset-9 bash
............
//再次查看gcc版本,此時為9.3了,現在就可以編譯了!
[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)
//編譯redis
[root@localhost redis-6.0.6]# make MALLOC=libc
............
Hint: It's a good idea to run 'make test' ;)
make[1]: 離開目錄“/root/redis-6.0.6/src”
[root@localhost src]# cp redis-cli redis-server /usr/bin/
[root@localhost ~]# which redis-cli
/usr/bin/redis-cli
[root@localhost ~]# redis-server
............
[root@localhost ~]# ss -anlt
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 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
//進入redis
[root@localhost ~]# redis-cli
127.0.0.1:6379> set a 10 //定義a的值為10
OK
127.0.0.1:6379> get a //查看a
"10"
//也可以在外面設置
[root@localhost ~]# redis-cli set b 20
OK
[root@localhost ~]# redis-cli get b
"20"
//設置登錄密碼
[root@localhost redis-6.0.6]# cp redis.conf /etc/
[root@localhost redis-6.0.6]# vim /etc/redis.conf
............
requirepass 123456@abc //修改密碼
//重啟服務
[root@localhost ~]# nohup redis-server /etc/redis.conf &
[1] 30509
[root@localhost ~]# nohup: 忽略輸入並把輸出追加到"nohup.out"
[root@localhost ~]# redis-cli
127.0.0.1:6379> set a 10
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456@abc
OK
127.0.0.1:6379> get a
"10"
命令大全
//添加多個元素到集合里
127.0.0.1:6379> SADD ab 10 20 30 40
(integer) 4
//獲取集合中元素的數量
127.0.0.1:6379> SCARD ab
(integer) 4
//獲取集合中所有的元素
127.0.0.1:6379> SMEMBERS ab
1) "10"
2) "20"
3) "30"
4) "40"