7

Another macOS upgrade + another MySQL upgrade = another set of problems.

I can't seem to get MySQL 8.0.18 to run as a homebrew service on macOS 10.15 Catalina. Please show me the error of my ways.

Here's what I did:

  1. brew install mysql
  2. brew pin mysql
  3. touch /tmp/mysql.sock
  4. mysql.server start
  5. unset TMPDIR
  6. mysql_secure_installation
  7. mysql.server stop
  8. sudo brew services start mysql

Here's what I expected:

MySQL to run merrily along as a homebrew service as user root.

Here's what happened:

MySQL falls right over and dies, leaving a cryptic last statement in /usr/local/var/mysql/[host.domain.com].err:

"[ERROR] [MY-010123] [Server] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!"

Additional information:

MySQL runs fine through subsequent reboots so long as I launch it manually:

sudo reboot now
ssh [servername.domain.com]
touch /tmp/mysql.sock
mysql.server start

Here's the /usr/local/var/mysql/[servername.domain.com].err file contents:

2019-10-20T18:02:14.6NZ mysqld_safe Logging to '/usr/local/var/mysql/moriarty.farces.com.err'.
2019-10-20T18:02:14.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
2019-10-20T18:02:14.670494Z 0 [System] [MY-010116] [Server] /usr/local/Cellar/mysql/8.0.18/bin/mysqld (mysqld 8.0.18) starting as process 557
2019-10-20T18:02:14.685511Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/var/mysql/ is case insensitive
2019-10-20T18:02:15.617696Z 0 [System] [MY-010229] [Server] Starting crash recovery...
2019-10-20T18:02:15.626461Z 0 [System] [MY-010232] [Server] Crash recovery finished.
2019-10-20T18:02:15.795626Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-10-20T18:02:15.833541Z 0 [System] [MY-010931] [Server] /usr/local/Cellar/mysql/8.0.18/bin/mysqld: ready for connections. Version: '8.0.18'  socket: '/tmp/mysql.sock'  port: 3306  Homebrew.
2019-10-20T18:02:15.993739Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/tmp/mysqlx.sock' bind-address: '127.0.0.1' port: 33060

After a reboot, when I try to run MySQL as a Homebrew service:

sudo reboot now
ssh [servername.domain.com]
sudo brew services start mysql

MySQL fails with the following error recorded in the /usr/local/var/mysql/[servername.domain.com].err file:

2019-10-20T18:44:13.780394Z 0 [ERROR] [MY-010123] [Server] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
2019-10-20T18:44:13.780503Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-10-20T18:44:13.780727Z 0 [System] [MY-010910] [Server] /usr/local/opt/mysql/bin/mysqld: Shutdown complete (mysqld 8.0.18)  Homebrew.
2019-10-20T18:44:13.6NZ mysqld_safe mysqld from pid file /usr/local/var/mysql/moriarty.farces.com.pid ended
Simba
  • 23,537
  • 7
  • 64
  • 76
Michael Fraase
  • 621
  • 1
  • 7
  • 11

5 Answers5

17

Never use sudo with command brew. It will ruin the ownership of related files. Running brew as root is not supported. (I'm talking about brew, not mysqld)

Quote from Homebrew doc

tl;dr Sudo is dangerous, and you installed TextMate.app without sudo anyway.

Homebrew refuses to work using sudo.

Warnings from the source code of brew

check-run-command-as-root() {
  ...
  odie <<EOS
Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
EOS
}

Solutions

  1. Disable the service and remove the launchdaemon.

    # stop and unload the launchdaemon
    sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.mysql.plist
    # remove the lauchdaemon file
    sudo rm -f /Library/LaunchDaemons/homebrew.mxcl.mysql.plist
    sudo rm -f /tmp/mysql.sock /tmp/mysqlx.sock
    
  2. Fix ownership of homebrew related files

    # For x86 Mac
    sudo chown -R "$(whoami):admin" /usr/local/*
    # For M1 Mac
    sudo chown -R "$(whoami):admin" /opt/homebrew/*
    # it will take some time
    
  3. Re-enable the MySQL service.

    # DO NOT USE "sudo brew"
    brew services start mysql
    

Update:

Seems some of you are not familiar with Homebrew. I'll explain how Homebrew manages services here.

mysqld listens at port 3306 by default, which is not a privileged port. So there's no need to start mysqld with root. systemd starts mysqld with root on Linux, but macOS is not Linux.

Homebrew manages services with the help of launchd, which is kind of a systemd alternative on macOS. launchd starts a foreground process and manages it for you, just like what systemd does.

brew services start/stop mysql will suffice your need. It creates a launchd file ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist, starts mysqld with it (run by current user).

/usr/local/mysql/support-files/mysql.server is a shell script brought by MySQL to help you start mysqld. Using it directly is not recommended unless you build MySQL from source outside Homebrew. If you're using Homebrew, just stick with brew services. Or say it in another way, if you're using Homebrew, you should do things in the Homebrew way.

Simba
  • 23,537
  • 7
  • 64
  • 76
  • Thanks for your interest. I followed your suggested "recipe" because it sounded like it made sense. I've been running home-brew for years as root, using sudo, without any problems (well I've had problems with each OS and most MySQL upgrades). – Michael Fraase Oct 21 '19 at 23:59
  • I'll never use `sudo` with `brew` again. Thanks these steps helped me :D – Ibrahim Tayseer Dec 03 '20 at 18:09
  • There are a lot of tutorials all over the internet using brew with sudo, most people will copy and paste and believe blindly that those are from experts. – Gilberto Albino Jan 20 '21 at 12:29
  • This saved me. It worked for me using mysql@5.7 – Jimmy Ilenloa Jun 19 '21 at 13:33
  • Thanks so much. I had the problem before and I had to reinstall the MySQL. the same problem occurred and i found your solution. Thanks so much.. – stoneshaq Feb 15 '23 at 23:03
2

MySQL refuses to start on Catalina because elevated privileges are required to run it.

You need to locate your MySQL bin directory first:

    which mysqld

The result you get should be similar to /usr/local/mysql/bin/mysql.

The support-files directory contains the required scripts needed to start-up MySQL, and is located in the same directory where the bin directory is located. In the above example, the support files directory will be /usr/local/mysql/support-files/.

Start the MySQL service with administrative privileges as follows:

    sudo /usr/local/mysql/support-files/mysql.server start

N.B: In case the output from the first command you run is different from the one above, adjust the support-files directory accordingly as explained above.

Babatunde Adeyemi
  • 14,360
  • 4
  • 34
  • 26
  • Please take a look at the "Additional information" that I've added to my original question. Your pointing to Catalina requiring elevated privileges is perhaps helpful: All of the MySQL files live in /usr/local/cellar/mysql/8.0.18 with symlinks in /usr/local/bin – Michael Fraase Oct 20 '19 at 19:02
  • All I got when attempting this was: ERROR! The server quit without updating PID file – Johan Fredrik Varen Jun 15 '20 at 07:18
1

Oh geez, he says, shaking his head...

This was a case of RTFM, and I didn't. Here's the applicable page from the MySQL 8.0 Reference manual. And here's the magic juju:

Add user=root to the [mysqld] section of the /usr/local/etc/my.cnf file, like so:

# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
user=root
Michael Fraase
  • 621
  • 1
  • 7
  • 11
  • Glad you find a solution. But I'm afraid you're going farther on the wrong track. **`brew` (not the `mysqld` daemon) is not supported being run as root.**, which could be confirm from the [source code of `brew`](https://github.com/Homebrew/brew/blob/85e4013989e849b374ff40858969be1125d63370/Library/Homebrew/brew.sh#L381-L398) and the [Homebrew documentation](https://docs.brew.sh/FAQ#why-does-homebrew-say-sudo-is-bad). – Simba Oct 25 '19 at 06:28
-1

I just encountered the same issue after upgrading to Catalina. If I started MySQL server from system preferences it would just start and stop itself. The solution for me was:

sudo /usr/local/mysql-8.0.17-macos10.14-x86_64/support-files/mysql.server start

instead of: /usr/local/mysql/support-files/mysql.server start

I've encountered a lot of problems with Catalina. Now I can't start/stop mysql server from system preferences, only from terminal.

It's my first post so I hope this helps, I only registered to answer you since you're the only one I found with the same question.

Glad
  • 1
  • I'm sorry, I guess I wasn't clear. I'm having problems getting MySQL 8.0.18 to run as a homebrew service on macOS 10.15 Catalina ("sudo brew services start mysql"). It runs (seemingly) fine with "mysql.server start." – Michael Fraase Oct 20 '19 at 01:43
-1

I solved like this:

Remove MySQL completely Watching: https://gist.github.com/vitorbritto/0555879fe4414d18569d

Install MySQL with Download do MySQL community server

https://dev.mysql.com/downloads/mysql/

And Done! Successfully!

Anderson Braz
  • 371
  • 2
  • 4