Autostart MySQL Server on Mac OS X Yosemite/El Cap

2019-01-10 00:53发布

问题:

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

edit: seems this works with El Capitan as well

回答1:

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

The com.mysql.mysql.plist in /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!



回答2:

I followed @revaxarts directions and upon trying to execute the command

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

was given the error:

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

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.

So, my XML looks like this:

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


回答3:

If you installed mysql with homebrew, you can get instructions on how to autostart it by typing 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


回答4:

Create /Library/LaunchDaemons/com.mysql.mysql.plist and save it with the following 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

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


回答5:

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

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>


回答6:

None of the other provided answers worked to auto-start my MySQL server. 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 launchctl load -F com.oracle.oss.mysql.mysqld.plist


回答7:

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 KeepAlive key will cause the process to launch again immediately after the Stop button is pressed. I used the key RunAtLoad to get it to just start on reboot, but allow the button in the pane to continue working.

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