redis配置和使用

前言

我們通過和數據庫的交互獲取數據,但是某些數據加載會嚴重影響性能,因爲查詢數據庫是一個比較耗時的事情。我們能不能提高查詢數據的效率?

這個時候我們就需要使用 redis了。(當然提升查詢效率不只有redis)

如果圖片鏈接失效可以直接在github上看,而且更新更及時,也更加有條理。在項目模塊有相關文章https://github.com/leosanqing/Java-Notes

目前已經有將近300個star

其他redis相關 (待更新)

  1. 設置主從複製
  2. Redis 爲啥會這麼快
  3. Redis常用的命令和數據結構

安裝和配置

下載

到官網下載redis https://redis.io/download

然後通過filezilla上傳至相應的虛擬機 我上傳至/opt文件夾下

安裝

  1. 使用 解壓縮命令解壓文件tar -zxvf <壓縮包名>
  2. 進入文件夾後 會發現有一個 Makefile文件,使用 make && make install
  3. 安裝的時候可能報錯,缺少gcc依賴。使用 yum install gcc-c++安裝一下就行

配置

  1. 進入redis目錄下,打開 redis.conf 文件 vim redis.conf

  2. 找到如下參數,並修改

    # The working directory.
    #
    # The DB will be written inside this directory, with the filename specified
    # above using the 'dbfilename' configuration directive.
    #
    # The Append Only File will also be created inside this directory.
    #
    # Note that you must specify a directory here, not a file name.
    #工作目錄,持久化 aof rdb都會存放在這個目錄下,如果修改了 需要創建相應的目錄
    dir /opt/redis5/working
    
    # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
    # JUST COMMENT THE FOLLOWING LINE.
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # 將127.0.0.1 改成 0.0.0.0 所有ip都能訪問
    bind 0.0.0.0
    
    # 保護模式關閉
    protected-mode no
    
    # By default Redis does not run as a daemon. Use 'yes' if you need it.
    # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
    # 是否後臺運行,設置成yes
    daemonize yes
    
    # 還有其他一下參數,比如 密碼什麼的,因爲是在虛擬機上運行,所以不需要設置
    

啓動

  1. 將redis目錄下的 /utils/ 的 redis_init_script文件複製到 /etc/init.d/下 。 cp redis_init_script /etc/init.d/
  2. 進入目錄cd /etc/init.d,修改相應文件 vim redis_init_script 將其配置文件路徑改成 我們的配置文件存放的路徑CONF="/opt/redis5/redis.conf"
  3. 賦予權限 chmod 777 redis_init_script
  4. 啓動 ./redis_init_script start

查看是否啓動成功

查看進程號 ps -ef|grep redis

設置自啓動

  1. vim /etc/init.d/redis_init_script 添加如下內容

    #chkconfig: 22345 10 90
    #description: Start and Stop redis
    

驗證

重啓虛擬機 reboot。再查看進程號是否存在

發佈了21 篇原創文章 · 獲贊 6 · 訪問量 4089
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章