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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26461173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:08:13  来源:igfitidea点击:

Autostart MySQL Server on Mac OS X Yosemite/El Capitan

mysqlosx-yosemiteosx-elcapitan

提问by Xaver

I would like to auto start the MySQL server on startup. This was possible in Mavericks but seems to be not working on Yosemite.

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

edit: seems this works with El Capitan as well

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

enter image description here

在此处输入图片说明

回答by Xaver

@dcc was very close. This is how MySQL autostarts again on Yosemite:

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

The com.mysql.mysql.plistin /Library/LaunchDaemons:

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>

Additionally I've changed the permissions based on this answer

此外,我已根据此答案更改了权限

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

Finally I run this command

最后我运行这个命令

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

If you have any addition please share below!

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

回答by Yevgeny Simkin

I followed @revaxarts directionsand upon trying to execute the command

我按照@revaxarts 指示并尝试执行命令

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

was given the error:

给出了错误:

/Library/LaunchDaemons/com.mysql.mysql.plist: Invalid property list

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

After scratching my head for a minute I found that removing the DOCTYPE DTD declaration at the top made the error go away and upon restart mySQL server is, indeed, running.

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

So, my XML looks like this:

所以,我的 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>

回答by Kyle Chadha

If you installed mysql with homebrew, you can get instructions on how to autostart it by typing brew info mysql.

如果您使用自制软件安装了 mysql,您可以通过键入brew info mysql.

For example, the output on my machine is:

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

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

回答by davidcondrey

Create /Library/LaunchDaemons/com.mysql.mysql.plistand save it with the following plist:

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

Then load the newly created plist file

然后加载新创建的plist文件

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

回答by Sharvin

This article help me resolve the issue with invalid error. Corrected plist I used below.

这篇文章帮助我解决了无效错误的问题。更正了我在下面使用的 plist。

How to know which line of plist file is incorrect

如何知道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>

回答by NobleUplift

None of the other provided answers worked to auto-start my MySQL server. I followed the instructions from the MySQL 5.6 handbookand it finally auto-starts again! Create the file /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plistwith the following content:

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

And run the following commands after creating the file:

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

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

回答by Franklin F

When I used the plist suggested in earlier answers, I changed the user to _mysql for my system, but the "Stop MySQL Server" button in the MySQL preference pane no longer worked. The KeepAlivekey will cause the process to launch again immediately after the Stop button is pressed. I used the key RunAtLoadto get it to just start on reboot, but allow the button in the pane to continue working.

当我使用之前答案中建议的 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>

Then, as in the other answers, ran:

然后,与其他答案一样,运行:

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

Now, MySQL launches on restart, but the MySQL pane in System Preferences still works. I'm running El Capitan, 10.11.2

现在,MySQL 在重新启动时启动,但系统偏好设置中的 MySQL 窗格仍然有效。我正在运行 El Capitan,10.11.2