How to auto-load MySQL on startup on OS X Yosemite / El Capitan
Asked Answered
S

5

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?

Sibyls answered 20/10, 2014 at 23:6 Comment(7)
Can you manually start MySql still?Widower
I think this question might be better suited for Ask DifferentDevanagari
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.Kalk
This was answered here yesterday: MySQL does not start in OSX Yosemite 10.10Amherst
@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.Sibyls
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.Amherst
Oh my bad, that was the wrong link actually. This is the link I meant to post: Autostart MySQL in OSX YosemiteAmherst
S
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
Sibyls answered 21/10, 2014 at 17:22 Comment(7)
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: Startup items are a deprecated technology. Launching of daemons through this process may be removed or eliminated in a future release of OS X.Lactobacillus
Thank you. I thought I was going to have to use the PreferencePane to restart mysql everytime I rebooted.Insanity
The above answer is correct, but alternatively the following launchd related MySQL on OS X documentation now exists: dev.mysql.com/doc/en/macosx-installation-launchd.htmlWoke
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.Brien
How are startup items a deprecated technology? What is the replacement for this feature?Scamp
Apple says in the depreciation notice, "use the launchd facility instead."Sibyls
I had to change to --user=my_user_name to make it work.Congest
A
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
Alialia answered 10/8, 2015 at 1:26 Comment(1)
This has not worked for me on either Yosemite or El Capitan. However, @Justin's answer worked.Tercentenary
P
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
Peta answered 16/2, 2016 at 23:24 Comment(0)
G
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

Grenadine answered 5/8, 2016 at 7:13 Comment(0)
S
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.

Smyth answered 11/3, 2015 at 21:28 Comment(6)
That's the way to fix it - just be sure to remove all existing mysql data before: community.jaspersoft.com/wiki/uninstall-mysql-mac-os-xFootman
Piping a shell script from an unknown source directly to bash is most definitely not a good way to do anything.Kalk
@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...Smyth
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.Kalk
Not to mention that you didn't even post a direct link. So many things can go wrong here. No thanks.Kalk
I have modified the original reply taking the security concerns into consideration.Smyth

© 2022 - 2024 — McMap. All rights reserved.