Skip to content

Instantly share code, notes, and snippets.

@richaber
Created January 27, 2023 17:24
Show Gist options
  • Save richaber/77965920d302d9f32e0844d2f5486f5b to your computer and use it in GitHub Desktop.
Save richaber/77965920d302d9f32e0844d2f5486f5b to your computer and use it in GitHub Desktop.
Connect PhpStorm to Local “Lightning” MySQL database via TCP/IP

PhpStorm does not support connecting to MySQL via socket.

To connect via TCP/IP instead, you will need to allow the DB_USER to connect to MySQL over TCP/IP, for each site that you want to connect to.

You will need the DB_USER, DB_PASSWORD, and DB_NAME values from you WP-Config, for each site you want to connect to.

Right-click on a site you want to connect to in Local’s sidebar and choose “Open Site Shell.”

In the resulting terminal, run the following commands
Be sure to replace DB_USER and DB_PASSWORD in these commands with their values from WP-Config!:

# Allow your DB_USER to connect via 127.0.0.1.
mysql -e "CREATE USER 'DB_USER'@'127.0.0.1' IDENTIFIED BY 'DB_PASSWORD'; GRANT ALL ON *.* TO 'DB_USER'@'127.0.0.1';"

# Get the TCP/IP port that MySQL is running on.  
mysql -e "SHOW VARIABLES WHERE Variable_name = 'port';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 10003 |
+---------------+-------+

In PhpStorm, in the Database tool window (View → Tool Windows → Database), click the Data Source Properties icon.

On the Data Sources tab in the Data Sources and Drivers dialog, click the Add icon and select MySQL.

If necessary, at the bottom of the data source settings area, click the Download missing driver files link.

Specify the database connection details.
Enter the Host as 127.0.0.1
Enter the Port that was returned by the SHOW VARIABLES command
Set Authentication to User & Password
Enter the same User as WP-Config's DB_USER value
Enter the same Password as WP-Config's DB_PASSWORD value
Enter the same Database as WP-Config's DB_NAME value
To ensure that the connection to the data source was successful, click the Test Connection link.

A succesful connection will return something similar to the following...

DBMS: MySQL (ver. 5.7.28)
Case sensitivity: plain=mixed, delimited=mixed
Driver: MySQL Connector/J (ver. mysql-connector-java-8.0.25 (Revision: 08be9e9b4cba6aa115f9b27b215887af40b159e0), JDBC4.2)

Ping: 11 ms
SSL: yes

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment