在 Mac OS X Yosemite/El Capitan 上自动启动 MySQL Server

我想在启动时自动启动 MySQL 服务器。这在小牛队是可能的,但在优胜美地似乎不起作用。

编辑 : 看起来这也适用于酋长岩

enter image description here

47520 次浏览

创建 /Library/LaunchDaemons/com.mysql.mysql.plist并用以下列表保存它:

<!--?xml version="1.0" encoding="UTF-8"?-->


<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true>
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<string>--user=mysql</string>
</array>
</true>
</dict>
</plist>

然后加载新创建的 plist 文件

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist

@ dcc 非常接近,这就是 MySQL 在 Yosemite 上的自动启动方式:

/Library/LaunchDaemons中的 com.mysql.mysql.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>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<string>--user=mysql</string>
</array>
</dict>
</plist>

此外,我已经更改了基于 这个答案的权限

sudo chown root:wheel /Library/LaunchDaemons/com.mysql.mysql.plist
sudo chmod 644 /Library/LaunchDaemons/com.mysql.mysql.plist

最后,我运行这个命令

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.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>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<string>--user=mysql</string>
</array>
</dict>
</plist>

我跟随 @ Xavers 方向并试图执行该命令

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist

给出了错误:

/Library/LaunchDaemons/com.mysql.mysql.plist: 无效属性列表

挠头一分钟后,我发现删除顶部的 DOCTYPE DTD 声明会消除错误,并且在重新启动 mySQL 服务器时,确实正在运行。

因此,我的 XML 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<string>--user=mysql</string>
</array>
</dict>
</plist>

当我使用早期答案中建议的 plist 时,我将系统的用户改为 _ MySQL,但 MySQL 首选项窗格中的“ Stop MySQL Server”按钮不再起作用。按下 Stop 按钮后,KeepAlive键将导致进程立即再次启动。我使用键 RunAtLoad让它重新启动,但是允许窗格中的按钮继续工作。

<?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>RunAtLoad</key>
<true/>
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<string>--user=_mysql</string>
</array>
</dict>
</plist>

然后,像其他答案一样,跑过去:

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist

现在,MySQL 在重新启动时启动,但是系统首选项中的 MySQL 窗格仍然可以工作。 我在查酋长岩10.11.2

如果你是 安装了自制的 mysql,你可以通过键入 brew info mysql来获得如何自动启动它的说明。

例如,我的机器上的输出是:

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

其他提供的答案都不能自动启动我的 MySQL 服务器。我按照指示从 MySQL 5.6手册和它终于自动启动再次!创建包含以下内容的文件 /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>
<string>--port=3306</string>
</array>
<key>WorkingDirectory</key>  <string>/usr/local/mysql</string>
</dict>
</plist>

并在创建文件后运行以下命令:

cd /Library/LaunchDaemons
sudo launchctl load -F com.oracle.oss.mysql.mysqld.plist