Starting up MySQL on mac on boot

estorstenson

macrumors member
Original poster
Jan 30, 2013
38
3
I have a mac mini with MySQL installed. The database is owned by my account (yeah, that is dumb, I know now...;) and if the machine is ever rebooted either to install updates or maybe the machine loses power, I have to manually launch mySQL using launchctl.

I've tried adding entries to /Library/LaunchAgents based on the example provided by homebrew, but it always seems to fail. I did add in the key/value pair for UserName, but that doesn't seem to work.

Any suggestions what I have done wrong?
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
I have a mac mini with MySQL installed. The database is owned by my account (yeah, that is dumb, I know now...;) and if the machine is ever rebooted either to install updates or maybe the machine loses power, I have to manually launch mySQL using launchctl.

I've tried adding entries to /Library/LaunchAgents based on the example provided by homebrew, but it always seems to fail. I did add in the key/value pair for UserName, but that doesn't seem to work.

Any suggestions what I have done wrong?
If this is only necessary on reboot you could perhaps try start up items.

http://support.apple.com/kb/ht2602
 

estorstenson

macrumors member
Original poster
Jan 30, 2013
38
3
Yeah, this won't work for my purposes: "Once set up, a "login item" is one that opens automatically after you log into the computer. " I want to avoid having to log in for the service to start. It should happen after the computer has rebooted.

I have created an entry in /Library/LaunchAgents/homebrew.mxcl.mysql.plist, but it isn't working, either because it doesn't do what I want, or it can't run things as other users (even though you can associate a user account with it).
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Yeah, this won't work for my purposes: "Once set up, a "login item" is one that opens automatically after you log into the computer. " I want to avoid having to log in for the service to start. It should happen after the computer has rebooted.
Well, if you do not have a login, you will automatically be logged in. If you do have a login, your database will not start any way since you will be stuck at your login screen on reboot.

One benefit of using launchd though, is that you can set additional properties like KeepAlive, which will restart your database if it should quit unexpectedly for some other reason than a reboot.

I have created an entry in /Library/LaunchAgents/homebrew.mxcl.mysql.plist, but it isn't working, either because it doesn't do what I want, or it can't run things as other users (even though you can associate a user account with it).
Do you need to run it as another user? Either way it's hard to tell without seeing that plist. Have you looked at Lingon, it makes setting up launchd scripts easy, it creates the plist for you after you fill in a form. http://www.peterborgapps.com/lingon/

Also: http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html
 

estorstenson

macrumors member
Original poster
Jan 30, 2013
38
3
Well, if you do not have a login, you will automatically be logged in. If you do have a login, your database will not start any way since you will be stuck at your login screen on reboot.

One benefit of using launchd though, is that you can set additional properties like KeepAlive, which will restart your database if it should quit unexpectedly for some other reason than a reboot.
It has a login, and more than one user. I want the job to launch without anyone having to login.

Do you need to run it as another user? Either way it's hard to tell without seeing that plist. Have you looked at Lingon, it makes setting up launchd scripts easy, it creates the plist for you after you fill in a form. http://www.peterborgapps.com/lingon/
Haven't seen lingon. Will look at it. Here is the plist file in /Library/LaunchAgents

Code:
<?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>homebrew.mxcl.mysql</string>
  <key>ProgramArguments</key>
  <array>
        <string>/usr/local/bin/mysqld_safe</string>
        <string>--user account-name</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>UserName</key>
  <string>account-name</string>
  <key>WorkingDirectory</key>
  <string>/usr/local/var</string>
</dict>
</plist>
account-name is the username that owns the database. I've tried it with both account-name entries present and not in all combinations. What I want is for this to work like it does in linux, where you have a command line job launched at the start. I'm sure there is a way to do it, since all of this is being done by the system, presumably as root, which can launch jobs as any user on the system.

Will look through these docs, but I suspect I've already been through them already.
 

estorstenson

macrumors member
Original poster
Jan 30, 2013
38
3
Ah, I see where I was mistaken. LaunchAgents are specific to a user login and a daemon is system level. So, I was using the wrong type. Hopefully using a daemon will fix the issue.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Yeah if you do want it to be system wide and not run under any particular user. But perhaps you can also check the system log at the time you tried last to see if there are any clues what happened.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,496
4,507
Restivus
Code:
<?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>homebrew.mxcl.mysql</string>
  <key>ProgramArguments</key>
  <array>
        <string>[COLOR="Red"]/usr/local/bin/mysqld_safe[/COLOR]</string>
        <string>--user account-name</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>UserName</key>
  <string>account-name</string>
  <key>WorkingDirectory</key>
  <string>/usr/local/var</string>
</dict>
</plist>
From what I can tell by googling, mysqld_safe is a shell script intended to start a daemonized version of mysql. Furtheremore, it appears to be designed for Linux and NetWare, neither of which use launchd.

The behavior of launchd with respect to launch daemons differs in many ways from how daemons are managed on other systems.

I recommend that you read more about exactly how launchd works, and then look at the shell code in mysqld_safe to understand exactly what it does. I suspect that at least one thing in the shell code is violating some rule of launchd. For example, the daemon(3) function in Linux is wrong to use with launchd.

https://developer.apple.com/library/mac/#technotes/tn2083/_index.html
http://dev.mysql.com/doc/refman/5.0/en/mysqld-safe.html

EDIT
http://hints.macworld.com/article.php?story=20080128103022907

Google search terms: launchd mysql
 
Last edited: