84

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

enter image description here

7 Answers 7

143

@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!

13
  • 1
    You should mark your Q answered so other people will find your solution. Oct 21, 2014 at 3:26
  • 26
    I dream about the day when OSX 10.11 will arrive and I won't have to spend 2 hours to fix apache, php and mysql. Maybe it's time to move to Vagrant..
    – dvk3
    Oct 24, 2014 at 12:05
  • 8
    It really works. BTW, the first two commands can be combined into one: sudo chown root:wheel /Library/LaunchDaemons/com.mysql.mysql.plist
    – Rockallite
    Oct 29, 2014 at 5:04
  • 1
    Very useful to have this to cut down on my two hours getting these working again! Thanks guys.
    – user2493235
    Oct 29, 2014 at 12:28
  • 1
    Worked for me - thanks. By the way, the chances are that if you're reading this, you will also have discovered that your copy of Apache doesn't work at startup either. If your Apache comes from Macports, the fix for this is similar: sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist. I found that the .plist file was still intact in the directory.
    – xgretsch
    Nov 14, 2014 at 12:23
13

I followed @Xavers 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>
0
10

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
2

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
8
  • doesn't work, it still says "stopped" after a restart :(
    – Xaver
    Oct 20, 2014 at 8:31
  • apachectl startstarts the server!? are they related?
    – Xaver
    Oct 20, 2014 at 8:36
  • The sudo launchctl load -w... throws an error Invalid property list
    – Xaver
    Oct 20, 2014 at 8:58
  • i'm out of possibilities, sorry Oct 20, 2014 at 9:00
  • 1
    ok, thank you anyway, I'll post the solution if I'm able to get one
    – Xaver
    Oct 20, 2014 at 9:47
2

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

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
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.