11
[(pyEnv) Anants-MacBook-Pro:litibackend anantchandra$ brew postinstall mysql
==> Postinstalling mysql
==> /usr/local/Cellar/mysql/8.0.11/bin/mysqld --initialize-insecure --user=anantchandra --basedir=/usr/local/Cellar/mysql/8.0.11 --datadir=/usr/local/var/mysql --tmpdir=/tmp
Last 15 lines from /Users/anantchandra/Library/Logs/Homebrew/mysql/post_install.01.mysqld:
2018-06-15 04:41:04 -0700

/usr/local/Cellar/mysql/8.0.11/bin/mysqld
--initialize-insecure
--user=anantchandra
--basedir=/usr/local/Cellar/mysql/8.0.11
--datadir=/usr/local/var/mysql
--tmpdir=/tmp

2018-06-15T11:41:04.901191Z 0 [System] [MY-013169] [Server] /usr/local/Cellar/mysql/8.0.11/bin/mysqld (mysqld 8.0.11) initializing of server in progress as process 37841
2018-06-15T11:41:04.903504Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2018-06-15T11:41:04.903537Z 0 [ERROR] [MY-010119] [Server] Aborting
2018-06-15T11:41:04.903701Z 0 [System] [MY-010910] [Server] /usr/local/Cellar/mysql/8.0.11/bin/mysqld: Shutdown complete (mysqld 8.0.11)  Homebrew.

Warning: The post-install step did not complete successfully
You can try again using `brew postinstall mysql`
bfontaine
  • 18,169
  • 13
  • 73
  • 107
Anant Chandra
  • 113
  • 1
  • 1
  • 6

4 Answers4

11

I was able to go back to 5.7 for anyone who wants to:

brew uninstall mysql
brew install mysql@5.7
brew link --force mysql@5.7
mysql.server start
mysql_secure_installation

After all that, I'm back in 5.7 with all my databases intact. In my case, I knew the data in the databases wasn't crucial, so I didn't attempt to backup the data in advance. Worked fine for me. If you have irreplaceable data in your local databases, you might want to tread carefully. I didn't lose data, but I don't want anyone else to lose data on my advice either. ;)

Normally, I don't mind a MySQL upgrade, but 8.0 looks to have compatibility issues I'd like to vet before going forward, and in the meantime, I'd rather be back on a version that doesn't force me to deal with those issues.

Geoffrey Wiseman
  • 5,459
  • 3
  • 34
  • 52
5

The installation or re-installation, brew install mysql, created the default data directory, and the post installation does not handle it...

  1. Simply move the existing data directory (this moves it to a sibling directory, named with the process id of the shell):

    $ mv /usr/local/var/mysql /usr/local/var/mysql-$$
    

    or might need super user privileges...

    $ sudo mv /usr/local/var/mysql /usr/local/var/mysql-$$
    
  2. Then run:

    $ brew postinstall mysql
    
  • 1
    I receive the same message after a failed installation, then uninstallation and reinstallation...the above fixed worked for me. – Scott Robert Schreckengaust Jun 20 '18 at 23:47
  • This solution is incomplete. The question is how can I upgrade my databases to MySQL 8. Not how can I remove MySQL 5.7 databases completely. – Paweł Gościcki Jun 22 '18 at 09:29
  • @ScottRobertSchreckengaust could you explain why adding the process id `-$$` helps the postinstallation to handle it? Ty – ilam engl May 18 '22 at 12:41
  • The `$$` will substitute the PID (process identifier) of the executing shell making it a unique destination in case there has been a previous move. An alternative would be a datetime stamp yet that is not created the same for various shells – Scott Robert Schreckengaust May 20 '22 at 13:14
5

First, backup the content of your data directory: /usr/local/var/mysql by copying it to a safe place.

The error happens because the post-install script check if a file /usr/local/var/mysql/mysql/user.frm exists. For whatever reason you don't have this file. The postinstall script then tries to install a new MySQL 8 database by running mysqld with --initialize-insecure but as the directory already contains some data from MySQL 5.7 the script halts.

Here is the correspoding part of the script in mysql.rb:

  def post_install
    # Make sure the datadir exists
    datadir.mkpath
    unless (datadir/"mysql/user.frm").exist?
      ENV["TMPDIR"] = nil
      system bin/"mysqld", "--initialize-insecure", "--user=#{ENV["USER"]}",
        "--basedir=#{prefix}", "--datadir=#{datadir}", "--tmpdir=/tmp"
    end
  end

There is several possible solutions. If you can still run your MySQL 5.7 database, export everything with mysqldump then install a fresh MySQL 8 database by removing all content in /usr/local/var/mysql and then import everything back again. Another solution, is to use the mysql_upgrade tool.

P.S.: Personally, I use the formula mysql@5.7 and I will in the future switch to MariaDB.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • 1
    I guess it's mostly an issue with `brew`, that it cannot upgrade your databases locally during mysql upgrade and then you cannot easily fix it yourself. Thanks for the detailed explanation. – Paweł Gościcki Jun 24 '18 at 07:54
  • agree with @PawełGościcki, there are some issues with brew when installing MySQL 8. If you want to get MySQL 8 running, you have to download it from https://dev.mysql.com/downloads/mysql/. However, removing `/usr/local/var/mysql` directory can help install 5.7 – Fred Lai Sep 02 '18 at 11:02
  • @PawełGościcki I got the same issue after `brew uninstall mysql` followed by `brew cleanup`. I then reinstalled from scratch. So I'm confused. Why this is still happening especially because find \*mysql\* retuned none... – ilam engl May 18 '22 at 12:34
1

According to this link, below command saves me on macOS Mojave:

sudo chown -R $(whoami) /usr/local/*
Yongyi
  • 327
  • 2
  • 12
  • That's what helper after struggling for some time, thanks for bothering with writing this answer! – bolt May 18 '21 at 14:30