Mac 开机启动MySQL/MongoDB/Redis 等服务
Mac上我们使用 homebrew 包管理工具
brew 官方网站: https://brew.sh
brew 安装方法: 在Mac中打开Termal,输入命令:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安装 launchctl
brew install launchctl
brew install launchctl-completion
brew 安装 MySQL
brew install mysql
它会将所有的包安装到/usr/local/Cellar/目录下,并将文件软连接到/usr/local/
安装完成后你需要到 /usr/local/Cellar/mysql/5.6.26/bin/ 下找到mysql来启动。
但是如果关掉终端,mysql服务也会随之关闭,这样就始终占据了一个终端窗口。
Mac OS 的开机启动方式
launchd 是 Mac OS 下用于初始化系统环境的关键进程,它是内核装载成功之后在OS环境下启动的第一个进程。
采用这种方式来配置自启动项很简单,只需要一个plist文件,该plist文件存在的目录有:
用户登陆前运行 plist(程序) LaunchDaemons ~/Library/LaunchDaemons
用户登录后运行相应的 plist(程序) LaunchAgents ~/Library/LaunchAgents
你需要.plist文件来指定需要开机启动的程序。
以下是开机启动的.plist配置文件的示例:
<?xml version="1.0" encoding="UTF-8"?>
<!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>org.mongodb.mongod</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mongodb-2.0.3/bin/mongod</string>
<string>run</string>
<string>--config</string>
<string>/usr/local/mongodb-2.0.3/mongod.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>WorkingDirectory</key>
<string>/usr/local/mongodb-2.0.3</string>
<key>StandardErrorPath</key>
<string>/usr/local/mongodb-2.0.3/log/error.log</string>
<key>StandardOutPath</key>
<string>/usr/local/mongodb-2.0.3/log/mongo.log</string>
</dict>
</plist>
如何编写.plist文件
brew安装的时候已经为你写好.plist文件。你只需要运行 brew info mysql 来查看帮助信息。
brew info mysql
<script type="text/javascript" src="https://asciinema.org/a/44624.js" id="asciicast-44624" async></script>
此时终端会显示如下信息:
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
To connect run:
mysql -uroot
To have launchd start mysql at login:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
mysql.server start
按照提示,执行
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents/
将mysql加入到登陆启动列表
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
或者立即启动mysql
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
启动之后就可以关闭终端了,mysql会在后台运行。
Nginx 开机启动(LaunchDaemons)
1. 拷贝 brew 安装的 plist
sudo cp /usr/local/Cellar/nginx/1.12.0/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/
$ cat /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
<?xml version="1.0" encoding="UTF-8"?>
<!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>homebrew.mxcl.nginx</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/nginx</string>
<string>-g</string>
<string>daemon on;</string>
</array>
<key>WorkingDirectory</key>
<string>/usr/local</string>
</dict>
</plist>
2. plist 加载到后台进程
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
3. 重启 Nginx
nginx -s quit
nginx
php-fpm 开机启动(LaunchDaemons)
1. 拷贝 brew 安装的 plist
sudo vim /Library/LaunchDaemons/homebrew.mxcl.php-fpm.plist
$ cat /Library/LaunchDaemons/homebrew.mxcl.php-fpm.plist
<?xml version="1.0" encoding="UTF-8"?>
<!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>homebrew.mxcl.php-fpm</string>
<key>KeepAlive</key>
<true/>
<key>Program</key>
<string>/usr/sbin/php-fpm</string>
</dict>
</plist>
2. plist 加载到后台进程
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.php-fpm.plist
命令执行后 php-fpm 将会马上启动,下次也会随开机而启动。
MySQL 开机启动(LaunchDaemons)
1. 新建 plist 文件
vim /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
$ cat /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key> <string>com.oracle.oss.mysql.mysqld</string>
<key>ProcessType</key> <string>Interactive</string>
<key>Disabled</key> <false/>
<key>RunAtLoad</key> <true/>
<key>KeepAlive</key> <true/>
<key>SessionCreate</key> <true/>
<key>LaunchOnlyOnce</key> <false/>
<key>UserName</key> <string>_mysql</string>
<key>GroupName</key> <string>_mysql</string>
<key>ExitTimeOut</key> <integer>600</integer>
<key>Program</key> <string>/usr/local/mysql/bin/mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld</string>
<string>--user=_mysql</string>
<string>--basedir=/usr/local/mysql</string>
<string>--datadir=/usr/local/mysql/data</string>
<string>--plugin-dir=/usr/local/mysql/lib/plugin</string>
<string>--log-error=/usr/local/mysql/data/mysqld.local.err</string>
<string>--pid-file=/usr/local/mysql/data/mysqld.local.pid</string>
</array>
<key>WorkingDirectory</key> <string>/usr/local/mysql</string>
</dict>
</plist>
2. plist 加载到后台进程
sudo launchctl load -w /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
MySQL 修改端口号
1. 修改启动配置文件
sudo vim /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
添加一行 <string>--port=33806</string> ,完整配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key> <string>com.oracle.oss.mysql.mysqld</string>
<key>ProcessType</key> <string>Interactive</string>
<key>Disabled</key> <false/>
<key>RunAtLoad</key> <true/>
<key>KeepAlive</key> <true/>
<key>SessionCreate</key> <true/>
<key>LaunchOnlyOnce</key> <false/>
<key>UserName</key> <string>_mysql</string>
<key>GroupName</key> <string>_mysql</string>
<key>ExitTimeOut</key> <integer>600</integer>
<key>Program</key> <string>/usr/local/mysql/bin/mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld</string>
<string>--user=_mysql</string>
<string>--basedir=/usr/local/mysql</string>
<string>--datadir=/usr/local/mysql/data</string>
<string>--plugin-dir=/usr/local/mysql/lib/plugin</string>
<string>--log-error=/usr/local/mysql/data/mysqld.local.err</string>
<string>--pid-file=/usr/local/mysql/data/mysqld.local.pid</string>
<string>--port=33806</string>
</array>
<key>WorkingDirectory</key> <string>/usr/local/mysql</string>
</dict>
</plist>
2. 修改完端口号后,重新加载启动 MySQL
方式1 (推荐)
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start
sudo /usr/local/mysql/support-files/mysql.server restart
方式2
1) 拷贝 xxx.plist 从 /Library/LaunchDaemons/ 到 ~/Library/LaunchAgents/
cp /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist ~/Library/LaunchAgents/
2) 执行重启加载命令
launchctl load -w ~/Library/LaunchAgents/com.oracle.oss.mysql.mysqld.plist
Mac 安装配置 MySQL,以及MySQL修改密码,添加用户和密码
请参见米扑博客:Mac 安装配置 MySQL
Redis 开机启动(LaunchDaemons)
1. 新建 plist 文件
vim /Library/LaunchDaemons/io.redis.redis-server.plist
$ cat /Library/LaunchDaemons/io.redis.redis-server.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>io.redis.redis-server</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/redis-server</string>
<string>/etc/redis.conf</string>
</array>
<key>RunAtLoad</key><true/>
</dict>
</plist>
2. 使用 launchctl 设置开机自启动
sudo launchctl load /Library/LaunchDaemons/io.redis.redis-server.plist
launchctl 取消开机自启动
sudo launchctl unload /Library/LaunchDaemons/io.redis.redis-server.plist
launchctl 启动redis server
sudo launchctl start io.redis.redis-server
launchctl 停止redis server
sudo launchctl stop io.redis.redis-server
测试 redis server 是否启动
$ redis-cli -p 6379 ping
PONG
MongoDB 开机启动(LaunchDaemons)
0. 安装 mongo
brew install mongodb
$ cat /usr/local/etc/mongod.conf systemLog: destination: file path: /usr/local/var/log/mongodb/mongo.log logAppend: true storage: dbPath: /usr/local/var/mongodb net: bindIp: 127.0.0.1 port: 27017 processManagement: fork: true
1. 拷贝 brew 安装的 plist
sudo cp /usr/local/Cellar/mongodb/3.4.4/homebrew.mxcl.mongodb.plist /Library/LaunchDaemons/
$ cat /Library/LaunchDaemons/homebrew.mxcl.mongodb.plist
<?xml version="1.0" encoding="UTF-8"?>
<!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>homebrew.mxcl.mongodb</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/mongod</string>
<string>--config</string>
<string>/usr/local/etc/mongod.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
<key>WorkingDirectory</key>
<string>/usr/local</string>
<key>StandardErrorPath</key>
<string>/usr/local/var/log/mongodb/output.log</string>
<key>StandardOutPath</key>
<string>/usr/local/var/log/mongodb/output.log</string>
<key>HardResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>4096</integer>
</dict>
<key>SoftResourceLimits</key>
<dict>
<key>NumberOfFiles</key>
<integer>4096</integer>
</dict>
</dict>
</plist>
2. plist 加载到后台进程
后台设置,开机而启动
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.mongodb.plist
3. mongodb 马上启动
sudo launchctl start homebrew.mxcl.mongodb
参考推荐:
Linux 配置 nginx、mysql、php-fpm、redis 开机启动
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2017-12-11 22:00:55
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!