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


84

我想在启动时自动启动MySQL服务器。在Mavericks中这是可能的,但似乎不适用于优胜美地。

编辑:似乎这也适用于El Capitan

在此处输入图片说明

Answers:


143

@dcc非常接近。这是MySQL在Yosemite上再次自动启动的方式:

com.mysql.mysql.plist/Library/LaunchDaemons

<?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

如果您有任何补充,请在下面分享!


1
您应将您的Q标记为已回答,以便其他人找到您的解决方案。
davidcondrey 2014年

26
我梦想着OSX 10.11到来的那一天,而我不必花2个小时来修复apache,php和mysql。也许是时候搬到流浪汉了
。– dvk3

10
只有两个小时!哈哈
Xaver

8
真的行。顺便说一句,前两个命令可以合并为一个:sudo chown root:wheel /Library/LaunchDaemons/com.mysql.mysql.plist
Rockallite 2014年

1
减少我的两个小时使它们重新工作非常有用!谢谢你们。
Nigel B. Peck

13

我遵循@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>

9

如果你用自制软件安装了mysql,你可以通过输入来获得如何自动启动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

2

/Library/LaunchDaemons/com.mysql.mysql.plist使用以下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

不起作用,它在重启后仍然说“停止”了:(
Xaver

apachectl start启动服务器!他们有关系吗?
Xaver 2014年

sudo launchctl load -w...引发一个错误Invalid property list
克萨韦尔

我无能为力,对不起
davidcondrey 2014年

1
好的,无论如何,谢谢,如果可以的话,我会发布解决方案
Xaver

2

本文可帮助我解决无效错误的问题。更正了我在下面使用的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>

1

其他提供的答案都无法自动启动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

0

当我使用先前答案中建议的plist时,我将系统的用户更改为_mysql,但是MySQL首选项窗格中的“停止MySQL服务器”按钮不再起作用。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的El Capitan

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.