Kai Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 六角鼠年鐵人賽 Week 20 - Spring Boot - Redis 安裝與設定配置介紹 ==大家好,我是 "為了拿到金角獎盃而努力著" 的文毅青年 - Kai== ## Michael Jordan :::info Always turn a negative situation into a positive situation. ::: ## 主題 上週簡單介紹過了什麼是 **Redis** 後,接下來幾篇開始來做一些實務面的介紹。 在介紹之前,不免得要來分享一下關於 Redis 的安裝與配置等流程。 避免一開始接觸的人手忙腳亂還建錯,雖不致於造成什麼問題,但知道正確的一些環節還是很重要的。 ## 安裝環境 [Redis Download Page](https://Redis.io/download) ![](https://i.imgur.com/HbzvK42.png) Redis 官網目前提供最新穩定版本 6.0 的檔案,若是需要搭配 Docker 的話,在一旁也有支援的載點,也可以直接透過 Docker Hub 取得。 因為目前 Redis 需要 Linux 環境才能作業,所以如果原先系統是 Windows 的系統,則需要自己建立 ubuntu 或是 CentOS 的 VM 環境。 Kai 這邊會利用之前學到的 Docker 方式,用 Docker Hub 提供的 image 直接建一個環境,最快最省事 ### Docker Env ```shell= // 使用下列語法進行 Redis image 的查詢 docker search redis // 使用下列語法進行 Redis 最新版本 image 的下載 docker pull redis:latest ``` ### Run Container 當載好後,就可以開始執行建立 Redis 環境 ```shell= docker run --name kai-test-redis -p 6379:6379 -d redis redis-server --appendonly yes ``` > appendonly [yes/no] 的設定是為了讓 Redis 開啟 persistent storage 模式,這模式可以讓數據一直保持著,不因關閉而受影響 ![](https://i.imgur.com/9bCayPj.png) ### Check Inside 執行後,我們可以進入到 Container 的環境中察看目前的 Redis 是否真的成功運行了。 透過輸入以下指令去檢查 ```shell= // 進入 Container 環境 docker exec -it 7da723853c51 bash // 開啟 Redis-client-tool redis-cli // 最簡單的檢查語法,會回傳 pong ping // 簡單的設定一個字串進入 Redis set testString "hello world" // 嘗試取得這個字串內容 get testString ``` 其結果如下圖,基本上這幾個功能都可以 work 的話,就代表 Redis 環境已經準備好了。 ![](https://i.imgur.com/utwV30i.png) ### Linux Env 上述是針對 Windows 的部分需要使用 Docker 的方式,若是一般 Linux 的環境就只需要下載 tar 包後安裝即可。 Linux 的指令流程如下: ```shell= wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make ``` ### Linux Run 比較差異的地方是,Docker 在執行 image 建立 Container 的時候,會自動執行 Redis-server 的指令去啟用 Redis 伺服器,但在 Linux 的部分就需要手動處理這件事情了。 ```shell= // 啟動 Redis 伺服器 redis-server & ``` > 後方加一個 & 符號是為了讓執行的指令可以在背景執行,方便開發人員繼續進行後續的指令操作 Linux 環境用 (如果 Kai 沒記錯的話...) ### Linux Check 在 Linux 環境上要確認 Redis 是否 work,比起 docker 環境可以啟動 Redis-client-tool 並常駐使用,需要在每一次的指令前都加上 redis-cli 的指令。 ```shell= redis-cli ping redis-cli set testString "hello world" redis-cli get testString // 可以透過指令 redis-cli 啟動 Redis-client-tool,就不需要每一行都打 redis-cli 的指令了 ``` > 因為 Kai 電腦沒有建 ubuntu 的環境,就不展示測試的結果了。 > 按照過往經驗應該是可以這樣操作的XD 有誤還請指正 ## 配置 Redis 本身提供了直接修正 Config 的操作,去幫助開發者調整符合系統需求的設定。 ```shell= // 取得 Config 的指令 config get [Config Name] // 取得所有 Config 的指令 config get * // 設置 Config 的指令,特別注意新值要用雙引號括起來 config set [Config Name] "[New Value]" ``` ![](https://i.imgur.com/uGOkLt6.png) 那接下就來看看,有哪些參數是 Redis 可以調整的,並且這些調整能讓系統達到什麼樣的變化。 |參數名稱| 定義 | 值限制 | | ---- | ---- | ---- | | daemonize [yes/no]| 將 Redis 設為一個程序並給予一個 PID,讓 Redis 可以保持運行 | yes/no;預設為 no;Windows 不支援此功能| | pidfile [/var/run/redis.pid] | 當 daemonize 為 yes 時,默認寫入 PID 到 redis.pid 文件,可以通過設置修改寫入文件位置 | 實際存在的文件路徑即可 | | port [6379]| | 綁定 Redis 監聽的埠號 | 預設 6379;可根據需求變動 | | bind [127.0.0.1] | 綁定 Redis Server 的主機位置 | 預設 127.0.0.1 本機;可根據需求變動 | | timeout [300] | 當 Client 端閒置多長秒數後關閉連接 | 單位秒;預設300;若為 0 則表示關閉此功能 | | loglevel [notice] | 指定 Log 級別 | 預設 notice;Redis 共支援 debug, verbose, notice, warning 四個級別 | | logfile [stdout] | Log 記錄方式 |預設為 stdout 標準輸出;若 daemonize 為 yes,logfile 又是 stdout,則 Log 將會發送給 /dev/null | | databases [0] | 設置資料庫數量 | 預設為0;可以使用 SELECT [id] 指定要使用的 DB | | save [seconds] [changes] | 指定多長時間內,有多少次更新,就將資料同步到資料文件內 | 單位為秒與次數;預設有三種: 900 1, 300 10, 60 10000 | | rdbcompression [yes/no] | 將資料儲存至DB時,是否進行壓縮 | 預設為 yes;採 LZF 壓縮,若要省 CPU 時間可以設置為 no,但資料會變得十分巨大 | | dbfilename [dump.rdb] | 指定 DB 的文件名稱 | 預設為 dump.rdb | | dir [./] | 指定 DB 的文件存放位置 | 預設為 ./ 底下 | | slaveof [master-ip] [master-port] | 設置 Slave 的 Redis 服務時,指定 Master 的 IP 與 Port,在該 Slave 啟動時會自動與 Master 同步資料 | 沒有預設值 | | masterauth [master-password] | 設置 Slave 連接 Master 需要的密碼 | 沒有預設值 | | requirepass [password] | 設置 Master 的密碼,其他 Slave 必須有密碼才能連接 | 沒有預設值 | | maxclients [0] | 設置同一時間的最大 Client 連線數 | 預設為0;若為 0 則表示不做限制 | | maxmemory [0] | Redis 最大記憶體限制,在啟動時會將資料載入到記憶體中。當達到最大上限時,會優先清除已到期或即將到期的 Key。達到上限時,也將無法進行寫入,但仍然可以讀取。| 單位為 bytes;沒有預設 | | appendonly [yes/no] | 設定是否在每次更新後進行 Log 紀錄。Redis 在預設會透過異步的方式將資料寫入磁碟中,若不開啟功能,則在斷電一段時間後會遺失資料。| 預設 no | | appendfilename [appendonly.aof] | 設定更新 Log 紀錄的文件名稱 | 預設為 appendonly.aof | | appendfsync [everysec] | 設定更新 Log 紀錄的條件 | 預設 everysec;另有 no, always 可選;no 為會與系統操作進行資料緩存同步到磁碟;always 表示手動下達 fsync() 指令將資料寫入磁碟;everysec 表示每秒同步一次| | vm-enabled [yes/no] | 設定是否啟用虛擬記憶體機制 | 預設 no;VM 機制會將資料分業存放,Redis 會將使用次數較少的資料 Swap 到磁碟上,使用次數較多的資料則會從磁碟 Swap 到記憶體中| | vm-swap-file [/tmp/redis.swap] | 虛擬記憶體文件路徑 | 預設為 /tmp/redis.swap;不可與多個 Redis 實體共用 | | vm-max-memory [0] | 設置 VM 最大記憶體上限,高於上限的資料將會存入 VM 記憶體中 | 預設為 0;若為 0 則表示所有 Value 都存在磁碟中 | | vm-page-size [32/64] | Redis Swap 文件分成很多 page,每一個 page 設定的儲存容量大小| 單位為 bytes;預設為 32;另有 64| |vm-pages [134217728] | 設置 swap 文件中 page 的數量,由於頁表是放在記憶體中,在磁碟中每 8 個 pages 將消耗 1 bytes 的記憶體空間| 沒有預設 | | vm-max-thread [4] | 訪問 swap 文件的 thread 數量上限 | 預設為 4;建議不超過硬體的 core 數 | | glueoutputbuf [yes/no] | 設置在 Client 端,設定是否把較小的封包一起合併發送 | 預設為 yes | | hash-max-zipmap-entries [64] | 設定在超過一定數量的元素時,採用特殊的 Hash 算法 | 沒有預設 | | hash-max-zipmap-value [512] | 設定在超過一定值的元素時,採用特殊的 Hash 算法 | | activerehashing [yes/no] | 設定是否重啟時會重製 Hash 值 | 預設為 yes| | include [/path/to/local.conf] | 指定配置文件路徑 | 可以在同一個主機、多個 Redis 實體間共用同一份配置文件 | | protected-mode [yes/no] | 是否開啟保護模式 | | ## 結語 :::danger 關於安裝與設定配置的學習就分享到這裡,原先還想加上資料結構的操作指令,但不同的資料結構 Redis 提供了不同的操作指令,若同時分享會造成本篇幅過長,讓讀者閱讀起來勞心勞力的。 就不在同一篇內提及,Kai 在下一篇與大家分享這些資料結構的操作部分~ [六角鼠年鐵人賽 Week 21 - Spring Boot - Redis 資料結構介紹](/w8Y42FmJTpyY_cx7fUz6qg) ::: 首頁 [Kai 個人技術 Hackmd](/2G-RoB0QTrKzkftH2uLueA) ###### tags: `Redis`,`w3HexSchool`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully