在Mac OS X Yosemite / El Capitan上自动启动MySQL服务器

时间:2014-10-20 08:16:22

标签: mysql osx-yosemite osx-elcapitan

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

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

enter image description here

7 个答案:

答案 0 :(得分: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>

此外,我已根据this answer

更改了权限
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 :(得分:13)

我跟着@revaxarts directions并尝试执行命令

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>

答案 2 :(得分:8)

如果您使用自制软件安装了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

答案 3 :(得分: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

答案 4 :(得分:2)

本文帮助我解决无效错误的问题。纠正了我在下面使用的plist。

How to know which line of plist file is incorrect

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

答案 5 :(得分:1)

没有其他提供的答案能够自动启动我的MySQL服务器。我按照the MySQL 5.6 handbook的说明进行操作,最后再次自动启动!使用以下内容创建文件/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

答案 6 :(得分: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窗格仍然有效。 我正在运行El Capitan,10.11.2