Local Community

How can I connect to MySQL using TCP/IP rather than a socket on macOS/Linux?

Depending on the application you’re using to connect to MySQL, you may be required to connect using TCP/IP rather than Unix sockets.

To connect via TCP/IP, you will need to create a new user on the IPv4 loopback IP (127.0.0.1) IP and grant it privileges.


Steps

  1. Right-click on the site you wish to connect to via TCP/IP in Local’s sidebar and go to “Open Site Shell.”

  2. Once your terminal opens, run the following commands:

    • Allow root to connect via 127.0.0.1.

      mysql -e "CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root'; GRANT ALL ON *.* TO 'root'@'127.0.0.1';"
      
    • Get the TCP/IP port that MySQL is currently running on. Note, this is not shown in Local’s UI as of writing if on macOS or Linux

      mysql -e "SHOW VARIABLES WHERE Variable_name = 'port';"
      

    Here’s an example of what you should see after running these two commands:


Example: Connecting via PhpStorm

Now that the user is created and you have the port, you can connect to it! Here’s what that looks like in PhpStorm.

Be sure that you use 127.0.0.1 as the “Host”, update “Port” to the port output in step #2 above, and set “Database” to local.

After Connecting in PhpStorm

1 Like