78

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?

Justin
  • 26,443
  • 16
  • 111
  • 128
  • Can you manually start MySql still? – Parker Oct 20 '14 at 23:26
  • I think this question might be better suited for [Ask Different](http://apple.stackexchange.com/) – Barranka Oct 20 '14 at 23:52
  • 3
    This isn't a programming question per-se, but since Macs are primarily used as personal computers it stands to reason the vast majority of people trying to start MySQL on a Mac are developers. So this question is probably relevant for many users of SO. – par Oct 21 '14 at 02:38
  • This was answered here yesterday: [MySQL does not start in OSX Yosemite 10.10](http://stackoverflow.com/questions/25954230/mysql-does-not-start-in-osx-yosemite-10-10) – davidcondrey Oct 21 '14 at 03:13
  • 1
    @dcc That question/answer is how to start MySQL after restarting (as the GUI tool won't do it). My question is how to make MySQL load on start-up of OSX. – Justin Oct 21 '14 at 17:29
  • The answer in the linked question provides a solution for starting up MySQL when you login (when OSX starts-up) by use of launchd. I'm not sure what the difference is your referring to. – davidcondrey Oct 21 '14 at 21:47
  • Oh my bad, that was the wrong link actually. This is the link I meant to post: [Autostart MySQL in OSX Yosemite](http://stackoverflow.com/questions/26461173/autostart-mysql-server-on-mac-os-x-yosemite/26461357#26461357) – davidcondrey Oct 21 '14 at 21:47

5 Answers5

172

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
Justin
  • 26,443
  • 16
  • 111
  • 128
  • 10
    Works! Thanks! The reason you need this is because there StartupItems have been removed from Yosemite. There is [a note at the start of the page in Apple's docs explaining this](https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/StartupItems.html): ``Startup items are a deprecated technology. Launching of daemons through this process may be removed or eliminated in a future release of OS X.`` – GuySoft Oct 23 '14 at 11:45
  • Thank you. I thought I was going to have to use the PreferencePane to restart mysql everytime I rebooted. – sehummel Nov 18 '14 at 22:21
  • The above answer is correct, but alternatively the following launchd related MySQL on OS X documentation now exists: http://dev.mysql.com/doc/en/macosx-installation-launchd.html – Philip Olson Jan 06 '15 at 19:03
  • 14
    This worked for me as well, but on my system, the mysql user was actually `_mysql`. You can run `dscacheutil -q user | grep mysql` to see what the user should be on your system. – m14t Jan 28 '15 at 18:08
  • 2
    How are startup items a deprecated technology? What is the replacement for this feature? – Aaron Hill Feb 10 '15 at 17:00
  • 1
    Apple says in the depreciation notice, "use the launchd facility instead." – Justin Feb 10 '15 at 17:25
  • 1
    I had to change to `--user=my_user_name` to make it work. – Navidot Sep 06 '17 at 11:54
17

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

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Yeonho
  • 3,629
  • 4
  • 39
  • 61
2

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
lensovet
  • 5,490
  • 2
  • 23
  • 19
NobleUplift
  • 5,631
  • 8
  • 45
  • 87
1

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

Community
  • 1
  • 1
no_igor
  • 95
  • 1
  • 3
0

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.

Per Quested Aronsson
  • 11,380
  • 8
  • 54
  • 76
  • 1
    That's the way to fix it - just be sure to remove all existing mysql data before: https://community.jaspersoft.com/wiki/uninstall-mysql-mac-os-x – maaalex Apr 07 '15 at 08:58
  • 3
    Piping a shell script from an unknown source directly to bash is most definitely *not* a good way to do anything. – par Oct 03 '15 at 17:25
  • @par: Yes, piping a shell script from an *unknown* source directly to bash is a bad idea. In this case, however, the shell script is open source and available for inspection at Github... – Per Quested Aronsson Jan 09 '16 at 20:46
  • You're assuming the server that shows you the source code is the same one that serves it to your computer. On a large site such as github this is almost certainly not a valid assumption (i.e. a CDN is probably involved), so *YES*, you do want to treat the URL as an unknown source. Download the source, read it (better yet, calculate its SHA1 hash) and only after you've verified it install it. – par Jan 09 '16 at 21:38
  • Not to mention that you didn't even post a direct link. So many things can go wrong here. No thanks. – par Jan 09 '16 at 21:51
  • 1
    I have modified the original reply taking the security concerns into consideration. – Per Quested Aronsson Jan 11 '16 at 07:08