如何在 OS X Yosemite / El Capitan 上启动时自动加载 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26476391/
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:22  来源:igfitidea点击:

How to auto-load MySQL on startup on OS X Yosemite / El Capitan

mysqlmacososx-yosemitestartuposx-elcapitan

提问by Justin

After upgrading OS X my install of MySQL stopped loading on startup.

升级 OS X 后,我的 MySQL 安装在启动时停止加载。

This walk-through on MySQLsays:

在MySQL穿行说:

"The Startup Item installation adds a variable MYSQLCOM=-YES- to the system configuration file /etc/hostconfig. If you want to disable the automatic startup of MySQL, change this variable to MYSQLCOM=-NO-."

“Startup Item安装在系统配置文件/etc/hostconfig中增加了一个变量MYSQLCOM=-YES-,如果要禁用MySQL的自动启动,把这个变量改为MYSQLCOM=-NO-。”

So, I opened that file and it says:

所以,我打开那个文件,它说:

# This file is going away 
AFPSERVER=-NO- 
AUTHSERVER=-NO-
TIMESYNC=-NO-
QTSSERVER=-NO-
MYSQLCOM=-YES-

I assume OSX dev's added the # This file is going awaybut I'm not certain.

我假设 OSX 开发人员添加了# This file is going away但我不确定。

If that is the case, what is the proper way to start MySQL on startup on OSX Yosemite?

如果是这种情况,在 OSX Yosemite 上启动时启动 MySQL 的正确方法是什么?

回答by Justin

This is what fixed it:

这是修复它的原因:

First, create a new file: /Library/LaunchDaemons/com.mysql.mysql.plist

首先,新建一个文件:/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>        
  </dict>
</plist>

Then update permissions and add it to launchctl:

然后更新权限并将其添加到launchctl

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

回答by Yeonho

If you installed mysql via homebrew, you can have launchdstart mysql at login by:

如果您通过homebrew安装了 mysql ,则可以launchd通过以下方式在登录时启动 mysql:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

回答by NobleUplift

The accepted answer did not work to auto-start my MySQL server (and in fact my Preferences Pane crashed System Preferences every time I tried to open it while it was active). 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 chown root:wheel com.oracle.oss.mysql.mysqld.plist 
sudo chmod o-w com.oracle.oss.mysql.mysqld.plist 
sudo launchctl load -F com.oracle.oss.mysql.mysqld.plist

回答by mulat

My Mac runs on El Capitan. MySQL installed via brew.

我的 Mac 在 El Capitan 上运行。MySQL 通过 brew 安装。

mysql.server status 

told me that I had some problems to solve:

告诉我我有一些问题需要解决:

ERROR! MySQL is not running, but PID file exists

Found homebrew.mxcl.mysql.plistfile in /usr/local/Cellar/mysql/x.x.x/directory and copied it to /Library/LaunchDaemons/

homebrew.mxcl.mysql.plist/usr/local/Cellar/mysql/x.x.x/目录中找到文件并将其复制到/Library/LaunchDaemons/

sudo cp homebrew.mxcl.mysql.plist /Library/LaunchDaemons/homebrew.mxcl.mysql.plist

Set all necessary permissions:

设置所有必要的权限:

sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.mysql.plist
sudo chmod 644 /Library/LaunchDaemons/homebrew.mxcl.mysql.plist
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.mysql.plist

Used part of advices, described by Justin

使用了部分建议,由Justin描述

回答by Per Quested Aronsson

There is a bash scriptby MacMiniVault, which will do this for you - and install MySQL as well. There is also an articlewhich describes the process.

有一个bash脚本MacMiniVault,它会为你做这一点-安装MySQL为好。还有一篇文章描述了这个过程。

It is suggested in the article to pipe the script straight into Terminal and run it, but as this has some serious security implications, it is a better idea to download and inspect the script before running it locally.

文章中建议将脚本直接通过管道传输到终端并运行它,但由于这具有一些严重的安全隐患,因此在本地运行之前下载并检查脚本是一个更好的主意。

Note: This reply has been reworked in response to comments about the security implications of following the instructions in the aforementioned article. It is generally a bad idea to pipe a shell script from an unknownsource directly to bash. Also, if you don't understand the script or trust the author, don't use it.

注意:为了回应有关遵循上述文章中的说明的安全影响的评论,此回复已被重新编写。将来自未知来源的 shell 脚本直接通过管道传输到 bash通常是一个坏主意。另外,如果您不理解脚本或信任作者,请不要使用它。