Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Homebrew MySQL 8.0.18 on macOS 10.15 Catalina won't run as service

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
like image 419
Michael Fraase Avatar asked Oct 19 '19 17:10

Michael Fraase


People also ask

Why MySQL is not working in Mac?

The reason behind the MySQL Workbench client not working can be many. It can be because your macOS version is not compatible with the version of the Workbench that you downloaded, or maybe because the version of Python installed on your macOS is not compatible with the version of Workbench you have downloaded.

Is there a MySQL installer for Mac?

MySQL for macOS is available in a number of different forms: Native Package Installer, which uses the native macOS installer (DMG) to walk you through the installation of MySQL.


2 Answers

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.

like image 171
Simba Avatar answered Sep 20 '22 15:09

Simba


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.

like image 35
Babatunde Adeyemi Avatar answered Sep 20 '22 15:09

Babatunde Adeyemi