This repository has been archived by the owner. It is now read-only.

Can't autostart MySQL with LaunchAgent as _mysql #11710

Closed
kenips opened this Issue Apr 17, 2012 · 6 comments

Comments

Projects
None yet
3 participants
@kenips
Contributor

kenips commented Apr 17, 2012

I just installed MySQL 5.5.20 on a brand new lion installation (brew doctor is completely happy with the machine). This time, however, rather than running mysql as myself I've decided to make it run as _mysql. I've managed to get mysql running with mysql.server start, but it fails to autostart as LaunchAgent. Here's what I've done:

* brew install mysql
* unset TMPDIR
* sudo mysql_install_db --verbose --user=mysql --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
* sudo mysql.server start
-> SUCCESS
* sudo mysql.server stop
* mkdir -p ~/Library/LaunchAgents
* cp /usr/local/Cellar/mysql/5.5.20/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
* launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
* update UserName in ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist to '_mysql'

Here's the output from console:

12-04-17 2:53:57.847 PM com.apple.launchd.peruser.502: (homebrew.mxcl.mysql) Ignored this key: UserName
12-04-17 2:53:57.983 PM homebrew.mxcl.mysql: 120417 14:53:57 mysqld_safe Logging to '/usr/local/var/mysql/MacBook-Pro-2.local.err'.
12-04-17 2:53:58.040 PM homebrew.mxcl.mysql: 120417 14:53:58 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
12-04-17 2:53:58.040 PM homebrew.mxcl.mysql: /usr/local/bin/mysqld_safe: line 107: /usr/local/var/mysql/MacBook-Pro-2.local.err: Permission denied
12-04-17 2:53:58.047 PM homebrew.mxcl.mysql: /usr/local/bin/mysqld_safe: line 144: /usr/local/var/mysql/MacBook-Pro-2.local.err: Permission denied
12-04-17 2:53:58.051 PM homebrew.mxcl.mysql: 120417 14:53:58 mysqld_safe mysqld from pid file /usr/local/var/mysql/MacBook-Pro-2.local.pid ended
12-04-17 2:53:58.051 PM homebrew.mxcl.mysql: /usr/local/bin/mysqld_safe: line 107: /usr/local/var/mysql/MacBook-Pro-2.local.err: Permission denied
12-04-17 2:53:58.051 PM com.apple.launchd.peruser.502: (homebrew.mxcl.mysql[7863]) Exited with code: 1

While fixing this issue I've tried a bunch of things, including:

  • setting owner to mysql:wheel for everything in mysql/bin
  • setting owner to mysql:wheel for everything in /usr/local/var/mysql
  • moving the launchAgent to /Library/LaunchAgents

but none of these gets me further than what I've. Any suggestions?

@kenips

This comment has been minimized.

Show comment
Hide comment
@kenips

kenips Apr 17, 2012

Contributor

Okay, I've got it running now. Turns out that:

  • /Library/LaunchAgents will run for every user that logs in, AS that user.
  • /Library/LaunchDaemons run on boot.

So moving the homebrew.mxcl.mysql.plist from /Library/LaunchAgents to /Library/LaunchDaemons solves the problem.

So I'm going to leave this open for now. If this is the correct solution to running mysql as _mysql please update the formula. If not I'm happy to learn the correct way to doing this :). Thanks!

Contributor

kenips commented Apr 17, 2012

Okay, I've got it running now. Turns out that:

  • /Library/LaunchAgents will run for every user that logs in, AS that user.
  • /Library/LaunchDaemons run on boot.

So moving the homebrew.mxcl.mysql.plist from /Library/LaunchAgents to /Library/LaunchDaemons solves the problem.

So I'm going to leave this open for now. If this is the correct solution to running mysql as _mysql please update the formula. If not I'm happy to learn the correct way to doing this :). Thanks!

@jacknagel

This comment has been minimized.

Show comment
Hide comment
@jacknagel

jacknagel Apr 18, 2012

Contributor

Did you try changing the UserName field in the plist? I think the plist we ship defaults to the output of whoami.

Contributor

jacknagel commented Apr 18, 2012

Did you try changing the UserName field in the plist? I think the plist we ship defaults to the output of whoami.

@kenips

This comment has been minimized.

Show comment
Hide comment
@kenips

kenips Apr 18, 2012

Contributor

Thanks for the quick reply @jacknagel! Yes I did. See my last step of what I've done and first line of console output that I reported:

12-04-17 2:53:57.847 PM com.apple.launchd.peruser.502: (homebrew.mxcl.mysql) Ignored this key: UserName

Which I think is the problem. I'm not sure if UserName gets honored on other OS X, but seems like on Lion it doesn't? The only way that I can get it to start as root is placing the file in /Library/LaunchDaemons.

Contributor

kenips commented Apr 18, 2012

Thanks for the quick reply @jacknagel! Yes I did. See my last step of what I've done and first line of console output that I reported:

12-04-17 2:53:57.847 PM com.apple.launchd.peruser.502: (homebrew.mxcl.mysql) Ignored this key: UserName

Which I think is the problem. I'm not sure if UserName gets honored on other OS X, but seems like on Lion it doesn't? The only way that I can get it to start as root is placing the file in /Library/LaunchDaemons.

@jacknagel

This comment has been minimized.

Show comment
Hide comment
@jacknagel

jacknagel Apr 18, 2012

Contributor

I only use launchd/launchctl for a couple of non-essential things, so I never bothered to learn much about it, but I would imagine that in order for the process to setuid() to the mysql user, the plist probably has to be installed with sudo launchctl.

Contributor

jacknagel commented Apr 18, 2012

I only use launchd/launchctl for a couple of non-essential things, so I never bothered to learn much about it, but I would imagine that in order for the process to setuid() to the mysql user, the plist probably has to be installed with sudo launchctl.

@jacknagel

This comment has been minimized.

Show comment
Hide comment
@jacknagel

jacknagel Apr 23, 2012

Contributor

Suggestions or patches to clarify any muddy points in the caveats are welcome, but I don't think there is much else to do here.

Contributor

jacknagel commented Apr 23, 2012

Suggestions or patches to clarify any muddy points in the caveats are welcome, but I don't think there is much else to do here.

@jacknagel jacknagel closed this Apr 23, 2012

@zchrykng

This comment has been minimized.

Show comment
Hide comment
@zchrykng

zchrykng Nov 3, 2012

I believe that the pull request I just made will clarify the instructions. The issue is that launchd cannot start a process as another user unless it is running as root, which it will not do when starting a file in the user home folder.

Pull Request

zchrykng commented Nov 3, 2012

I believe that the pull request I just made will clarify the instructions. The issue is that launchd cannot start a process as another user unless it is running as root, which it will not do when starting a file in the user home folder.

Pull Request

@Homebrew Homebrew locked and limited conversation to collaborators Feb 16, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.