How to Connect MySQL in macOS via 127.0.0.1

MySQL Database

Usually any application connecting to MySQL in macOS (macport) is via localhost or 127.0.0.1, and there might be error if the application is connect via 127.0.0.1.

This usually happened if MySQL service is installed via Macports, it’s due to Macports make it easier to run multiple MySQL version i.e MySQL 5.6, MySQL 5.7 or MariaDB, hence it only allow to connect via socket.

These are the errors you will see if you are connecting via localhost or 127.0.0.1

ERROR 2003 (HY000): Can’t connect to MySQL server on ‘127.0.0.1’ (61)

Here is how to enable and allow MySQL connection via 127.0.0.1 in macOS.

  1. Edit the file /opt/local/etc/mysql56/macports-default.cnf (or the version you are using)
  2. Uncomment the item skip-networking
  3. Restart MySQL via command
    1. sudo launchctl unload -w /Library/LaunchDaemons/org.macports.mysql56-server.plist
    2. sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql56-server.plist

Try connecting MySQL Server again with command

mysql -h 127.0.0.1 -u root -p

You should be able to connect via 127.0.0.1!