? ? ? 此文章的前置條件是 “Mac系統已安裝過Homebrew”,如果未安裝,可訪問 “Mac 安裝 homebrew 詳細教程” 一文,詳細介紹Homebrew的用法。利用 “Homebrew” 對 “Redis” 進行安裝管理,那是一個方便啊。
利用 homebrew 安裝 Redis
Homebrew是一款 macOS 平臺下缺失的軟件包管理工具,擁有安裝、卸載、更新、查看、搜索等很多實用的功能。簡單到什么程度呢,僅僅通過一條指令,就可以實現包管理,而不用你關心各種依賴和文件路徑的情況,十分方便快捷。
Homebrew會將將這些工具統統安裝到了 /usr/local/Cellar 目錄中,并在 /usr/local/bin 中創建軟鏈接。
PS:沒安裝 “Homebrew” 的同學,可以訪問 “Mac 安裝 homebrew 詳細教程” 一文參考。
?
查看 Redis 服務
brew search redis
安裝 Redis 服務?
brew install redis# 默認安裝的事最新版本的 redis ---> 當前最新版本為 "7.0.2"
? brew install redis
Running `brew update --auto-update`...
==> Downloading https://ghcr.io/v2/homebrew/core/redis/manifests/7.0.2
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/redis/blobs/sha256:ae3e94acbf74e76afbf42a7627bc91864191f8530e0a04b
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:ae3e94acbf74e76afbf42a7627bc91
######################################################################## 100.0%
==> Pouring redis--7.0.2.monterey.bottle.tar.gz
==> Caveats
To restart redis after an upgrade:brew services restart redis
Or, if you don't want/need a background service you can just run:/usr/local/opt/redis/bin/redis-server /usr/local/etc/redis.conf
==> Summary
🍺 /usr/local/Cellar/redis/7.0.2: 14 files, 2.6MB
==> `brew cleanup` has not been run in the last 30 days, running now...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Removing: /Users/husky/Library/Caches/Homebrew/jenkins--2.354... (89.4MB)
Removing: /usr/local/Cellar/openssl@1.1/1.1.1o... (8,089 files, 18.5MB)
Removing: /Users/husky/Library/Caches/Homebrew/openssl@1.1--1.1.1o... (5.2MB)
Removing: /Users/husky/Library/Caches/Homebrew/python@3.10--3.10.4... (14.5MB)
Removing: /Users/husky/Library/Caches/Homebrew/Cask/iterm2--3.4.15.zip... (23MB)
Removing: /Users/husky/Library/Logs/Homebrew/autojump... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/gdbm... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/mpdecimal... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/libuv... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/openjdk@11... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/brotli... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/icu4c... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/neofetch... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/readline... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/carthage... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/screenresolution... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/sqlite... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/c-ares... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/xz... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/libnghttp2... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/ca-certificates... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/tcl-tk... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/openssl@1.1... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/python@3.10... (2 files, 2.5KB)
Removing: /Users/husky/Library/Logs/Homebrew/jenkins... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/node... (64B)
Removing: /Users/husky/Library/Logs/Homebrew/python@3.8... (3 files, 241.6KB)
Pruned 36 symbolic links and 4 directories from /usr/local
?
查看安裝及配置文件位置
- Homebrew安裝的軟件會默認在?/usr/local/Cellar/?路徑下
- redis的配置文件?redis.conf?存放在?/usr/local/etc?路徑下
安裝指定按本的 Redis 服務
- 上面我們默認安裝的事最新版本的 “Redis” ,除此之外,還可以安裝指定版本的的 “Redis” ,只需在安裝命令后面增加對應的版本號即可。
brew install redis@6.2# 指定安裝的版本號
配置 Redis 環境變量
說到配置環境變量這一步,不得不吐槽一句、完全沒必要。
當我們使用 "homebrew" 安裝好 "Redis" 的時候,會幫我們配置好環境變量(也就是軟連接 到 /usr/local/bin 目錄),所以我們可以在終端的任意目錄下運行命令 "redis-server" 命令來啟動 "redis"?
? which redis-server
/usr/local/bin/redis-server
? where redis-server
/usr/local/bin/redis-server
? where redis*
/usr/local/bin/redis-benchmark
/usr/local/bin/redis-check-aof
/usr/local/bin/redis-check-rdb
/usr/local/bin/redis-cli
/usr/local/bin/redis-sentinel
/usr/local/bin/redis-server
?啟動 redis 的兩種啟動方式、重啟與關閉查看本地運行的服務列表
啟動方式①:
通過 brew services 來啟動(后臺啟動) ;強烈推薦
brew services start redis
# 通過 brew service 命令啟動 redis
啟動方式②:
也可以通過上文提到的 redis-server 啟動;PS:需要注意的是,這樣的啟動方式,在關閉窗口后,redis 服務就停止了,不推薦!
redis-server
# redis 默認的啟動方式,但是在關閉終端窗口后,redis 服務就停止了,所以不推薦。
重啟
brew services restart redis
# 通過 brew service 命令重啟 redis 服務
關閉
brew services stop redis
# 通過 brew service 命令關閉 redis 服務
?
查看本地 redis 是否正常運行連接本地的Redis
brew services list | grep redis# brew services list 是查詢當前 brew 啟動的所有的服務
連接本地的Redis?
redis 默認端口號6379,默認auth(密碼)為空,輸入以下命令即可連接
redis-cli 是 redis 安裝的時候自帶的命令行客戶端,使用如下命令連接 redis
redis-cli -h 127.0.0.1 -p 6379
# -h : host - ip地址
# -p : port - 端口號
# -a : auth - 密碼;存在密碼的情況下使用,當前默認狀態沒有密碼,所以省略了。
連接成功后,使用 ping 命令與 redis服務端 做心跳測試,服務端正常會返回 pong 。
?
總結
以上是生活随笔為你收集整理的Python 数据库开发实战-Mac系统下通过homebrew安装Redis数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。