Install MySQL on Mac with Brew and Keep It Off the Network

Updated

install mysql mac brew

Short answer: install with brew install mysql, but skip the “allow external connections” step this page used to teach. It told readers to find bind-address and set it to 0.0.0.0, with no firewall step, no non-root user, and no requirement to run mysql_secure_installation first. Homebrew’s MySQL installs with no root password at all — its own installer says so — so that combination publishes an unauthenticated database to anything that can reach the port. On a laptop on a café Wi-Fi network, “anything” includes every other device on the network.

What this machine’s server actually reports

Captured from a live MySQL 8.4.10 instance, unedited:

mysql> SHOW VARIABLES LIKE 'bind_address';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| bind_address  | 127.0.0.1 |
+---------------+-----------+

mysql> SELECT user, host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+

Localhost-only, as shipped. And from Homebrew’s own installer output for mysql@8.4, also pasted unedited: “We’ve installed your MySQL database without a root password.” A default install has no password on the one account bound to loopback. Set bind-address to 0.0.0.0 before fixing that, and you’ve moved the passwordless account from “reachable only from this machine” to “reachable from the network” — the whole distance between safe and not. Moving the server off 3306 does not buy back any of that distance either: the obscurity claim was tested and a scan finds the new port anyway.

What to do instead

Almost nobody who wants to “connect from another machine” actually needs MySQL listening on every interface. Two better options, in order of how often they’re the right call:

  1. Keep bind-address at 127.0.0.1 and tunnel. Run ssh -L 3306:127.0.0.1:3306 user@dbhost from the client machine, then connect to 127.0.0.1:3306 locally. The traffic is encrypted, and MySQL never listens anywhere but loopback. This is standard OpenSSH port forwarding, not specific to MySQL — cited here, not demonstrated against a remote host, since this machine has no second host to tunnel to.
  2. If you genuinely need network access, create a dedicated non-root user scoped to a host — CREATE USER 'app'@'10.0.1.%' IDENTIFIED BY '...' — grant it only the privileges it needs, run mysql_secure_installation first, and open the port on your firewall for that source range only. bind-address 0.0.0.0 plus a firewall rule is a real, defensible combination; 0.0.0.0 with root still passwordless is not.

The page’s own troubleshooting section already gets the firewall right, once: “ensure that port 3306 (default MySQL port) is open.” That is the only other place this page mentions a firewall at all — telling you to open it, never to restrict it. If you’re changing bind-address, that troubleshooting line and this section describe the same firewall; don’t configure it twice with different rules.

The other line that needed fixing: passwords on the command line

The old post-install step read mysqladmin -u root password 'your_new_password'. A password as a command-line argument lands in your shell history file and, on a multi-user machine, is visible to anyone running ps while the command executes. MySQL’s own client says so — captured here against a scratch account created and dropped in this session, not against root:

$ mysqladmin -u hw_w17_scratch -pInitPass1! password 'hw_w17_NewPass2!'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

Run mysqladmin -u root -p password without the value on the line, or better, use mysql_secure_installation, which prompts interactively and never puts a password on argv or in scrollback.

One more thing worth knowing before you install: brew install mysql today installs the unversioned mysql formula, currently 9.7.1 — a different major line than the 8.4.10 server this page’s other output was captured against. If you specifically want 8.4.x, install mysql@8.4. Either way, don’t trust mysql --version to tell you the server version — it reports the client’s. Ask the server: SELECT VERSION();.

Everything else on the original page — brew install mysql, brew services start mysql, and running mysql_secure_installation before you do anything else — is still the right sequence and is unchanged here. For tuning what you do with the server once it’s secured, see the buffer pool and memory tuning guide.

Check this yourself. Every command and every block of output on this page is reproduced by /verify/install-mysql-mac-brew.sh. Download it and run it: it creates its own scratch files, prints one line per claim, cleans up after itself, and exits non-zero if any claim here turns out to be wrong. If it disagrees with this page, the page is wrong.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.