2021年8月18日更新
新版M1 macmini 用下面的方法总出错,不知道是版本问题,还是软件问题,故更新了一版安装程序,结果可以自动实现redis系统驻留,开机启动,感觉不错,记录下。
brew install redis
xudu@XudeMac-mini bin % brew install redis
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles/openssl%401.1-1.1
Already downloaded: /Users/xudu/Library/Caches/Homebrew/downloads/285271792fdc543e70405f9d9bbd2b01a8404289f984f453b1638bbf272c8d30--openssl@1.1-1.1.1k.arm64_big_sur.bottle.tar.gz
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/bottles/redis-6.2.5.arm64
==> Downloading from https://homebrew.bintray.com/bottles/redis-6.2.5.arm64_big_sur.bottle.tar.
#=#=-# #
curl: (22) The requested URL returned error: 403 Forbidden
Warning: Bottle missing, falling back to the default domain...
==> Downloading https://ghcr.io/v2/homebrew/core/redis/manifests/6.2.5
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/redis/blobs/sha256:080c05259d4b1d5b5a7fe9948b5
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:080c05259d
######################################################################## 100.0%
==> Installing dependencies for redis: openssl@1.1
==> Installing redis dependency: openssl@1.1
==> Pouring openssl@1.1-1.1.1k.arm64_big_sur.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
🍺 /opt/homebrew/Cellar/openssl@1.1/1.1.1k: 8,071 files, 18MB
==> Installing redis
==> Pouring redis--6.2.5.arm64_big_sur.bottle.tar.gz
==> Caveats
To start redis:
brew services start redis
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/redis/bin/redis-server /opt/homebrew/etc/redis.conf
==> Summary
🍺 /opt/homebrew/Cellar/redis/6.2.5: 14 files, 2MB
==> Caveats
==> redis
To start redis:
brew services start redis
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/redis/bin/redis-server /opt/homebrew/etc/redis.conf
xudu@XudeMac-mini bin % brew services start redis
==> Successfully started `redis` (label: homebrew.mxcl.redis)
xudu@XudeMac-mini bin %
安装直接完成,输入系统提示的命令 brew services start redis ,实现以进程方式启动,完美。
# ---------- 原始分割线 ----------
1、下载redis
reids下载地址 http://download.redis.io/releases ,当前稳定版为5.0.9
下载安装包的手段可以在上述网站点击下载,也可以用
wget http://download.redis.io/releases/redis-5.0.4.tar.gz这个命令下载
用系统自带的zip解压即可。
tar zxvf redis-5.0.4.tar.gz
cp文件 到 /usr/local/redis目录下即可
2、编译redis
sudo make
如果是第一次,系统会要求下载xcode,大概2分钟下载完成,继续安装
sudo make install
至此安装成功,这时候千万不要启动Redis,因为没有配置路径,会出错,需要修改redis.conf的配置
我的redis.conf 在 /usr/local/redis-5.0.4 目录下,不同版本或操作系统会稍有不同,大家可自行寻找。
每个版本的位置和配置都不太一样,大致为下图:因为路径在 /usr/local/bin 该路径没有写权限,因此会报持久化错误,无法退出redis-server,修改的办法是放在一个有写权限的目录,如 /Users/aaa/tmp/ ,即可。


3、自启动设置
为了让Redis在启动时自动启动,我使用的是launchd。
在/ Library / LaunchDaemons中创建一个简单的xml文档。
sudo vim /Library/LaunchDaemons/redis-server.plist
cp下面这段代码进 redis-server.plist文件
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>redis-server</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/redis-server</string>
<string>/usr/local/redis5.0.4/redis.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
其中redis-server的路径和redis.conf的路径需要根据安装路径修改。
现在将redis.server.plist加载到launchd中。
sudo launchctl load /Library/LaunchDaemons/redis-server.plist
只需要执行一次,现在您应该通过launchd控制您的redis服务器。使下面命令启动Redis。
sudo launchctl start redis-server
如果你做得正确,请启动redis-cli客户端并输入“ping”。你应该连接并接收“PONG”响应。
不正确多是路径错误,重新设定即可。
祝各位安装成功。
通过下列命令判断redis是否正常运行,如果正常运行会出现带端口号的进程。
ps -ef |grep redis
ps aux | grep redis
2022-11-08 补充
一、安装命令
使用mac的包管理工具brew一行命令搞定安装。若未安装brew,命令行先输入以下命令安装brew。
/bin/bash -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
安装redis
brew install redis@6.2 #后面@接版本号可指定版本
二、使用redis的常用命令
1.启动redis服务
brew services start redis
2.关闭redis服务
brew services stop redis
3.重启redis服务
brew services restart redis
4.打开图形化界面
redis-cli
三、一些常用的配置
1.开机启动redis命令
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
2.使用配置文件启动redis-server
redis-server /usr/local/etc/redis.conf
3.停止redis服务
redis-cli shutdown
4. redis配置文件位置
/usr/local/etc/redis.conf
5.卸载redis
brewuninstallredis rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

本文详细介绍在Mac上使用Homebrew安装Redis的过程,包括解决安装过程中遇到的问题,配置Redis服务开机自启,以及如何通过launchd进行管理。此外,还提供了启动、停止和重启Redis服务的常用命令。
526

被折叠的 条评论
为什么被折叠?



