51

I just installed mysql in terminal through homebrew.

Now when I try to connect to mysql it fails, but after I run mysqld it works..

So what I need to do now is run mysqld when I boot my Mac.

I've searched Google for mysqld autoload at startup etc. but couldn't find the right answer.

Hope someone can point me in the right direction.. Thanks!

boatcoder
  • 17,525
  • 18
  • 114
  • 178
Tim Baas
  • 6,035
  • 5
  • 45
  • 72

7 Answers7

85

brew info mysql gives you the instructions for loading MySQL at startup, but here's all you need to do:

mkdir -p ~/Library/LaunchAgents
cp `brew --prefix mysql`/*mysql*.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/*mysql*.plist
Morgan Christiansson
  • 29,280
  • 1
  • 19
  • 13
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • I believe this is only true if you've installed mysql via brew, but it's great if you have! – joanwolk May 04 '12 at 17:00
  • 9
    In my latest installation, the plist file is named: homebrew.mxcl.mysql.plist – mtkopone Oct 05 '12 at 11:19
  • And if you have Mariadb installed, file name is : homebrew.mxcl.maraidb.plist – chhantyal Sep 29 '14 at 08:44
  • While this answer is historically accurate, the more recent `brew services start` command is the modern solution, easier to work with than the arcane launchctl – RonLugge Dec 18 '19 at 21:56
  • Should this go in `/Library/LaunchAgents` or `/Library/LaunchDaemons`? I think the later. – gone Jul 16 '20 at 03:12
35

Nowadays, brew info mysql gives you this statement:

To have launchd start mysql now and restart at login: brew services start mysql

Or, if you don't want/need a background service you can just run: mysql.server start

So running brew services start mysql will ensure it will run as a service and re-start on every login.

If you run that command with sudo:

sudo brew services start mysql

will make the service run on boot and not after logging in, which is more useful for servers.

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30
  • 2
    This is the ONLY answer that actually answers the question, and launches the server at BOOT and not LOGIN. It creates the right .plist in the right location. This should really be marked as the right answer... even though it's not actually working for me. But I think that is a separate issues. – l008com Mar 17 '18 at 07:21
  • Won't allow to do sudo because "Running Homebrew as root is extremely dangerous and no longer supported". How do I do it on boot now? – Oleksii Nezhyborets Aug 11 '21 at 10:13
  • Looks like there's no way unless `root` owns the `brew` executable. You'll probably need to go the `LaunchDaemons` way from other answers. IMHO this is the worst decision people from Homebrew has made. We know what we're doing, there should at least be a toggle do disable that stupid enforcement. – Alejandro Iván Aug 13 '21 at 14:53
4

Mipadi's answer failed for me with MySQL 5.6.13 installed with Homebrew on the date of this post. But I checked brew info mysql as he suggested and it gave me a working solution:

> ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Rubinous
  • 464
  • 6
  • 12
2
mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/mysql/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Bruno
  • 6,211
  • 16
  • 69
  • 104
2

You don't need to setup a plist anymore. The latest version of brew (1.3.4) as of Oct 2017, you can only run:

brew services start <service_name>

and it will auto start every time you restart your computer. The command brew info gives you this

To have launchd start mysql now and restart at login: brew services start mysql Or, if you don't want/need a background service you can just run: mysql.server start

2

Followup to Alejandro that couldn't fit in a comment.

Check which services are user (login) and root (boot) with this:

$ brew services list
Name       Status  User   Plist
chronograf started jacob  /Users/jacob/Library/LaunchAgents/homebrew.mxcl.chronograf.plist
grafana    started jacob  /Users/jacob/Library/LaunchAgents/homebrew.mxcl.grafana.plist
influxdb   started root   /Library/LaunchDaemons/homebrew.mxcl.influxdb.plist
kapacitor  stopped        
mariadb    started jacob  /Users/jacob/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
memcached  started jacob  /Users/jacob/Library/LaunchAgents/homebrew.mxcl.memcached.plist
telegraf   started jacob  /Users/jacob/Library/LaunchAgents/homebrew.mxcl.telegraf.plist
JP Duffy
  • 1,307
  • 1
  • 11
  • 18
1

I have brew install of MYSQL.

type

brew info mysql

and follow the instructions given:

To have launchd start mysql at login:

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
mikoop
  • 1,981
  • 1
  • 18
  • 18