Troubleshooting: Install MySQL with brew
Tags: mysql, macos, homebrew
Published: 2024-02-18 08:46:54

I failed to install MySQL with brew again and again. But it wasn't true. I just thought so.

## Way to my mistake

After installing MySQL with brew command:

`brew install mysql`

Then, my terminal said I finished to install MySQL and need to configure the password for my root user:

```bash
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -u root

To restart mysql after an upgrade:
  brew services restart mysql
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql/bin/mysqld_safe --datadir\=/opt/homebrew/var/mysql
==> Summary
🍺  /opt/homebrew/Cellar/mysql/8.3.0: 323 files, 309.4MB
==> Running `brew cleanup mysql`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
```

So I tried the following command:

```
╰─➤  mysql_secure_installation                                                                                                   

Securing the MySQL server deployment.

Enter password for user root: 
```

I thought the `mysql_secure_installation` requires the existing password, I thought. I had no idea about my "existing password" because I had not set up before (I have just installed as following the instruction). 

But this, the existence of my password, is wrong idea. What it requires here is the new password for the root user. So what I should do is just type your new password for the root user.

## What I did (but not necessary)

I did not notice the fact...

So I execute the following command:

```brew
brew uninstall mysql
sudo rm /opt/homebrew/etc/my.conf
sudo rm -r /opt/homebrew/var/mysql
brew install mysql
```

I tried to uninstall the mysql, delete configuration file, i.e.  '/opt/homebrew/etc/my.cnf', and caches from `/opt/homebrew/var/mysql/`, then reinstall mysql again.

Then run `mysql_secure_installation` again, then I have got the same "issue"...

## How to fix

I realised my mistake after researching and reading the following article:
https://dev.to/manikbajaj/how-to-install-mysql-8-on-macos-using-homebrew-399d

then I finally and successfully set up my password as follows.

```bash
╰─➤  mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
Please set the password for root here.

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
```

Phew!