PhpStorm 2023.3 Help

Connect to MySQL with unix sockets

Enable the Database Tools and SQL plugin

This functionality relies on the Database Tools and SQL plugin, which is bundled and enabled in PhpStorm by default. If the relevant features aren't available, make sure that you didn't disable the plugin.

  1. Press Ctrl+Alt+S to open the IDE settings and then select Plugins.

  2. Open the Installed tab, find the Database Tools and SQL plugin, and select the checkbox next to the plugin name.

On Unix, you can connect to the mysqld server by using two different ways: a Unix socket file (for example, /var/run/mysqld/mysqld.sock), or by using TCP/IP (for example, 127.0.0.1:3306). A connection that is created with a Unix socket file is faster than TCP/IP but can only be used when connecting to a server on the same computer. When you use a Unix socket file, you can skip a hostname and port in the connection string.

Prerequisites

Step 1. Locate a Unix socket file

  1. On the server host in the command line, run the following command:

    mysql -u root -p -h 127.0.0.1 -e "select @@socket"
  2. Type a password for your root user and press Enter.

    Locate a Unix socket file

Step 2. Check the Unix socket connection from the command line

  1. In the command line, run the following command: mysql -u root -p -S /var/run/mysqld/mysql.sock.

  2. Type a password for your root user and press Enter.

    Check the Unix socket connection from the command line

Step 3. Download third-party libraries

The Connector/J driver does not natively support connections to MySQL Servers with Unix domain sockets. To enable socket connection, you need to download third-party libraries. For more information about this limitation, refer to Connecting Using Unix Domain Sockets at dev.mysql.com.

  1. Download the latest release from the junixsocket repository at github.com (for example, junixsocket-dist-2.3.2-bin.tar.gz).

  2. Extract the downloaded archive. You need to add the following files from the lib directory to the MySQL driver in PhpStorm:

    • junixsocket-mysql-2.3.2.jar

    • junixsocket-native-common-2.3.2.jar, if you have a custom architecture try junixsocket-native-custom-2.3.2.jar

    • junixsocket-core-2.3.2.jar

    • junixsocket-common-2.3.2.jar

Step 4. Configure the MySQL driver in PhpStorm

  1. Open data source properties. You can open data source properties by using one of the following options:

    • In the Database tool window ( View | Tool Windows | Database) , click the Data Source Properties icon The Data Source Properties icon.

    • Press Shift+Enter.

  2. In the Drivers section, click the MySQL driver and click the Duplicate button the Duplicate button. Alternatively, press Ctrl+D.

  3. Change the name of the duplicated driver (for example, MySQL socket).

  4. On the General tab, click the Add button (the Add button) and select Custom JARs.

  5. In the file browser, navigate to a folder with third-party libraries (refer to Step 3). While pressing Ctrl, select the following files:

    • junixsocket-mysql-2.3.2.jar

    • junixsocket-native-common-2.3.2.jar, if you have a custom architecture try junixsocket-native-custom-2.3.2.jar

    • junixsocket-core-2.3.2.jar

    • junixsocket-common-2.3.2.jar

  6. Click OK.

  7. On the Advanced tab, find the socketFactory property, double-click the Value cell, and change the value to org.newsclub.net.mysql.AFUNIXDatabaseSocketFactory.

    Change the socketFactory property
  8. Scroll down to the end of the property list, double-click the <user defined> cell and type junixsocket.file. Double-click the Value cell and type the path to your socket file (refer to Step 1).

    Add the junixsocket.file property
  9. Click Apply.

Step 5. Create a connection to the MySQL server

  1. To connect to the database, create a data source that will store your connection details. You can do this using one of the following ways:

    • In the main menu, go to File | New | Data Source and select MySQL.

    • In the Database tool window ( View | Tool Windows | Database) , click the New icon (the New icon) in the toolbar. Navigate to Data Source and select MySQL.

    Create a new data source
  2. Check if there is a Download missing driver files link at the bottom of the connection settings area. Click this link to download drivers that are required to interact with a database. For a direct download link, refer to the JetBrains JDBC drivers page.

    Location for the downloaded JDBC drivers is the PhpStorm configuration directory.

    The Download missing driver files link

    You can also use your drivers for the database instead of the provided ones. For more information about connecting to a database with your driver, refer to Add a user driver to an existing connection.

    If there is no Download missing driver files link, then you already have the required drivers.

  3. On the Advanced tab, find the serverTimezone parameter in the list of options. Double-click the Value cell and type your server timezone (for example, UTC).

    Set the serverTimezone parameter
  4. Click the General tab.

  5. From the Driver list, select the driver that you created earlier (refer to Step 4).

  6. In User and Password fields, specify your user credentials.

  7. Ensure that the connection to the database can be established using the provided details. To do that, click the Test Connection link at the bottom of the connection details area.

    Test Connection link

    In case of any connection issues, refer to the Cannot connect to a database page.

  8. (Optional) By default, only the default schema is introspected and available to work with. If you also want to work with other schemas, in the Schemas tab, select them for the introspection.

    Schemas tab of the Data Sources and Drivers dialog
  9. Click OK to create the data source.

  10. Find your new data source in the Database tool window (⌘ 1) .

    • For more information about the Database tool window, see the corresponding reference topic.

    • For more information about working with database objects in PhpStorm, refer to Database objects.

    • To write and run queries, open the default query console by clicking the data source and pressing F4.

    • To view and edit data of a database object, open Data editor and viewer by double-clicking the object.

Test connection
Last modified: 25 March 2024