在Mac上我們使用[homebrew]包管理工具(http://brew.sh/index_zh-cn.html)來安裝和管理開發(fā)工具包背传,例如:mysql汗侵、php忿峻、redis掀亥。只需要一個命令
brew install mysql
它會將所有的包安裝到/usr/local/Cellar/
目錄下昆箕,并將文件軟連接到/usr/local/
安裝完成后你需要到/usr/local/Cellar/mysql/5.6.26/bin
下找到mysql來啟動鸦列。但是如果關(guān)掉終端租冠,mysql服務(wù)也會隨之關(guān)閉,這樣就始終占據(jù)了一個終端窗口薯嗤。
Mac OS 的開機啟動方式
launchd
是 Mac OS 下用于初始化系統(tǒng)環(huán)境的關(guān)鍵進(jìn)程顽爹,它是內(nèi)核裝載成功之后在OS環(huán)境下啟動的第一個進(jìn)程。采用這種方式來配置自啟動項很簡單骆姐,只需要一個plist文件镜粤,該plist文件存在的目錄有:
- LaunchDaemons
~/Library/LaunchDaemons
用戶登陸前運行 plist(程序) - LaunchAgents
~/Library/LaunchAgents
用戶登錄后運行相應(yīng)的 plist(程序)
你需要.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安裝的時候已經(jīng)為你寫好.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
按照提示,執(zhí)行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
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
啟動之后就可以關(guān)閉終端了,mysql會在后臺運行。