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

Xaver
  • 11,144
  • 13
  • 56
  • 91

7 Answers7

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!

Community
  • 1
  • 1
Xaver
  • 11,144
  • 13
  • 56
  • 91
  • 1
    You should mark your Q answered so other people will find your solution. – davidcondrey Oct 21 '14 at 03: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 '14 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 '14 at 05:04
  • 1
    Very useful to have this to cut down on my two hours getting these working again! Thanks guys. –  Oct 29 '14 at 12:28
  • Matches the solution posted earlier here: https://discussions.apple.com/thread/6604544 – arcseldon Nov 07 '14 at 03:24
  • 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 '14 at 12:23
  • @revaxarts Well, thanks to you it took me only 2 minutes... :) Thanks – yair Mar 14 '15 at 22:28
  • Y'all saved me 115 minutes! – Circle B Mar 28 '15 at 21:43
  • In my case the user running MySQL was: _mysql. You can check the current user with the command: dscacheutil -q user | grep mysql – Bjinse Jun 10 '15 at 08:07
  • Thanks first half the reboot solved, now to get Tomcat to start – Jim Geldermann Oct 25 '15 at 18:39
  • 1
    After upgrading from Yosemite to El Capitan, this was only part of my problems. This part was indeed solved within two hours: MySQl is started again. Thanks a lot. – Sigur Nov 25 '15 at 21:40
  • This code causes my System Preferences to crash on El Capitan when I click 'MySQL'. – NobleUplift Feb 08 '16 at 23:24
  • works! had this problem for months, finally found the solution. – RaptoX Sep 18 '17 at 11:01
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>
Xaver
  • 11,144
  • 13
  • 56
  • 91
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
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
Kyle Chadha
  • 3,741
  • 2
  • 33
  • 42
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
davidcondrey
  • 34,416
  • 17
  • 114
  • 136
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>
Community
  • 1
  • 1
Sharvin
  • 21
  • 2
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
NobleUplift
  • 5,631
  • 8
  • 45
  • 87
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