1. 程式人生 > 實用技巧 >【Redis】02 Redis 搭建與操作

【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