此文章的前置条件是 “Mac系统已安装过Homebrew”,如果未安装,可访问 “Mac 安装 homebrew 详细教程” 一文,详细介绍Homebrew的用法。利用 “Homebrew” 对 “Redis” 进行安装管理,那是一个方便啊。
利用 homebrew 安装 Redis
Homebrew是一款 macOS 平台下缺失的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单到什么程度呢,仅仅通过一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。
Homebrew会将将这些工具统统安装到了 /usr/local/Cellar 目录中,并在 /usr/local/bin 中创建软链接。
PS:没安装 “Homebrew” 的同学,可以访问 “Mac 安装 homebrew 详细教程” 一文参考。
查看 Redis 服务
- 终端执行 Homebrew 的查询命令
brew search redis
安装 Redis 服务
- 终端执行 Homebrew 的安装命令
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
- 查询 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 。