IntelliJ IDEA 2022.3 Help

Connect to MySQL with unix sockets

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, see 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 IntelliJ IDEA:

    • 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 IntelliJ IDEA

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

  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 (see 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 (see Step 1).

    Add the junixsocket.file property
  9. Click Apply.

Step 5. Create a connection to the MySQL server

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

  2. In the Data Sources and Drivers dialog, click the Add icon (The Add icon) and select MySQL.

  3. Check if there is a Download missing driver files link at the bottom of the data source settings area. As you click this link, IntelliJ IDEA downloads drivers that are required to interact with a database. The IDE does not include bundled drivers in order to have a smaller size of the installation package and to keep driver versions up-to-date for each IDE version.

    You can specify your drivers for the data source if you do not want to download the provided drivers. For more information about creating a database connection with your driver, see Add a user driver to an existing connection.

  4. 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
  5. Click the General tab.

  6. From the Driver list, select the driver that you created earlier (see Step 4).

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

  8. To ensure that the connection to the data source is successful, click the Test Connection link.

Test connection
Last modified: 05 December 2022