mac redis开机自启动

1,570 阅读2分钟

一、安装及启动关闭服务器

1、首先找到redis的安装路径

xxx@xxxMBP redis-6.0.5 % pwd
/Users/quzengpeng/Software/server/redis-6.0.5xxx@xxxMBP redis-6.0.5 % ls
00-RELEASENOTES		MANIFESTO		redis.conf		sentinel.confBUGS
Makefile		runtest			srcCONTRIBUTING		README.md
runtest-cluster		testsCOPYING			TLS.md			
runtest-moduleapi	utilsINSTALL			deps			
runtest-sentinel
xxx@xxxMBP redis-6.0.5 %

2、把配置文件拷贝一份到/usr/local/etc下

xxx@xxxMBP redis-6.0.5 % cp redis.conf /usr/local/etc/
xxx@xxxMBP redis-6.0.5 % ls /usr/local/etc
redis.conf

3、测试一下启动命令

xxx@xxxMBP redis-6.0.5 % cd /usr/local/bin
xxx@xxxMBP bin % ls 
brew				docker-credential-osxkeychain	redis-benchmarkcom.docker.cli			
hub-tool			redis-check-aofdocker		kubectl				
redis-check-rdbdocker-compose	kubectl.docker			redis-clidocker-compose-v1		
node				redis-sentineldocker-credential-desktop	npm				
xxx@xxxMBP bin % ./redis-server /usr/local/etc/redis.conf &[1] 5039xxx@xxxMBP bin % 5039:C 04 Dec 2021 01:37:33.344 # oO0OoO0OoO0Oo 
Redis is starting oO0OoO0OoO0Oo5039:C 04 Dec 2021 01:37:33.344 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=5039, just started
5039:C04 Dec 2021 01:37:33.344 # Configuration loaded
5039:M 04 Dec 2021 01:37:33.344 * Increased maximum number of open files to 10032 (it was originally set to 256).
5039:M 04 Dec 2021 01:37:33.344 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 5039
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               
5039:M 04 Dec 2021 01:37:33.345 # Server initialized5039:M 04 Dec 2021 01:37:33.345 * Loading RDB produced
 by version 6.2.55039:M 04 Dec 2021 01:37:33.345 * RDB age 21 
seconds5039:M 04 Dec 2021 01:37:33.345 * RDB memory usage when
 created 0.98 Mb5039:M 04 Dec 2021 01:37:33.345 * DB 
loaded from disk: 0.000 seconds5039:M 04 Dec 2021 01:37:33.345 * Ready to accept connections

4、连接redis客户端

xxx@xxxMBP bin % ./redis-cli
127.0.0.1:6379>  set k1 v1
ok
127.0.0.1:6379>  get k1 
v1
127.0.0.1:6379> keys * 
1) k1

5、关闭redis服务器

127.0.0.1:6379> SHUTDOWN
not connected> exit
xxx@xxxMBP bin % 

二、设置开机自启动- mac系统

1、创建.plist配置文件

为了让 Redis 在启动时自动启动,我使用的是 launchd。在 /Library/LaunchDaemons 中创建一个简单的xml文档。

xxx@xxxMBP bin %  sudo vim /Library/LaunchDaemons/redis-server.plist

注意⚠️:

查找redis-conf的位置、redis-server的位置;

xxx@xxxMBP bin %  which redis-server
/usr/local/bin/redis-server
xxx@xxxMBP bin %  sudo find / -name redis.conf
/usr/local/etc/redis.conf

这里在上面已经备份好了

然后正确填写以下代码:

<!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/etc/redis.conf</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

保持即可

2、将 redis.server.plist 加载到 launchd 中

xxx@xxxMBP bin %  sudo launchctl load /Library/LaunchDaemons/redis-server.plist

此时mac重启或开机会自动启动redis