How to auto-load MySQL on startup on OS X Yosemite

2019-01-10 01:35发布

问题:

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

This walk-through on MySQL says:

"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-."

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 away but I'm not certain.

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

回答1:

This is what fixed it:

First, create a new file: /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:

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


回答2:

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

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


回答3:

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 handbook and it finally auto-starts again! Create the file /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist with the following content:

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


回答4:

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

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 unknown source directly to bash. Also, if you don't understand the script or trust the author, don't use it.



回答5:

My Mac runs on El Capitan. MySQL installed via 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.plist file in /usr/local/Cellar/mysql/x.x.x/ directory and copied it to /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