在centos7上部署 redis 和基本操作

一、爲什麼使用 Redis?

  • Redis 是開源的內存中的數據結構存儲系統,它可以用作數據庫、數據緩存和消息中間件。

  • 它支持多種類型的數據結構,如 字符串strings, 散列 hashes, 列表 lists, 集合 sets, 有序集合 sorted sets 與範圍查詢, bitmaps, hyperloglogs 和 地理空間(geospatial) 索引半徑查詢。

  • Redis 還內置了 複製(replication),LUA腳本(Lua scripting), LRU驅動事件(LRU eviction),事務(transactions) 和不同級別的磁盤持久化(persistence), 並通過 Redis哨兵(Sentinel)和自動 分區(Cluster)提供高可用性(high availability)

  • 支持數據的備份,即 master-slave 模式的數據備份。

  • 運行時數據和狀態都保存在內存中,支持數據的持久化。
    可以將內存中的數據保持在磁盤中,重啓的時候可以再次加載進行使用等等。

二、Redis在項目中的應用場景

  • 1、緩存數據
    最常用,對經常需要查詢且變動不是很頻繁的數據 常稱作熱點數據。
  • 2、消息隊列
    相當於消息訂閱系統,比如ActiveMQ、RocketMQ。如果對數據有較高一致性要求時,還是建議使用MQ。
  • 3、計數器
    比如統計點擊率、點贊率,Redis具有原子性,可以避免併發問題。
  • 4、電商網站信息
    大型電商平臺初始化頁面數據的緩存。比如去哪兒網購買機票的時候首頁的價格和你點進去的價格會有差異。
  • 5、熱點數據
    比如新聞網站實時熱點、微博熱搜等,需要頻繁更新。總數據量比較大的時候直接從數據庫查詢會影響性能。

三、redis部署

1、在 redis官網 下載安裝包到本地主機上,上傳到centos7虛擬機中。
官網鏈接:https://redis.io/download

//關閉防火牆
systemctl stop firewalld
setenforce 0

//安裝
yum install -y gcc gcc-c++ make

//解壓安裝包,make編譯
tar zxvf redis-5.0.7.tar.gz -C /opt/
cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis/ install

[root@promote redis-5.0.7]# cd utils/     #一直回車到最後只修改路徑
[root@localhost utils]# ./install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server
 
Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]     //定義主配置文件
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]     //定義日誌文件
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] //數據文件
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server //可執行文件路徑,需要自行定義
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

//查看服務狀態,服務已經開啓
[root@promote utils]# netstat -natp | grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      22999/redis-server  

//創建軟鏈接,優化服務
ln -s /usr/local/redis/bin/* /usr/local/bin/

//進入redis數據庫
vim /etc/redis/6379.conf 
#行尾模式輸入bind
bind 127.0.0.1 192.168.100.129 綁定本機IP地址重啓服務即可
//重啓服務
service redis_6379 restart     
Stopping ...
Redis stopped
Starting Redis server...

//登錄數據庫
[root@localhost ~]# redis-cli -h 192.168.100.129 -p 6379    //-h指定地址,-p指定端口
192.168.43.101:6379> 

在這裏插入圖片描述

四、redis數據庫的命令操作

1、遠程登錄redis數據庫命令

redis-cli -h 192.168.100.129 -p 6379    //-h指定地址,-p指定端口

2、鍵值key-value的管理

格式:
創建鍵:set 鍵名 值
刪除鍵:del 鍵名
獲取鍵對應的值:get 鍵名
獲取當前數據庫的所有鍵:keys 

在這裏插入圖片描述
在這裏插入圖片描述
3、數據庫的切換

格式:
切換到某庫:select 庫名
移動當前庫中的鍵值到另一個庫:move 鍵名 庫名  

在這裏插入圖片描述

五、數據庫的性能測試

示例:測試數據庫的 get 功能命令如下

redis-benchmark -h 192.168.100.129 -p 6379 -t get -c 50 -n 10000
-h 指服務器的地址  
-p 服務端口
-c 併發連接數
-n 請求總數

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章