Create a MySQL data source using 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 PyCharm by default. If the relevant features aren't available, make sure that you didn't disable the plugin.
Press Ctrl+Alt+S to open settings and then select
.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
Linux only
Junixsocket JAR files: the junixsocket repository at github.com
PyCharm must be on the host where the server is running (MySQL 8.0 Reference Manual at dev.mysql.com)
Step 1. Locate a Unix socket file
On the server host in the command line, run the following command:
mysql -u root -p -h 127.0.0.1 -e "select @@socket"Type a password for your
root
user and press Enter.
Step 2. Check the Unix socket connection from the command line
In the command line, run the following command:
mysql -u root -p -S /var/run/mysqld/mysql.sock
.Type a password for your
root
user and press Enter.
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.
Download the latest release from the junixsocket repository at github.com (for example, junixsocket-dist-2.3.2-bin.tar.gz).
Extract the downloaded archive. You need to add the following files from the lib directory to the MySQL driver in PyCharm:
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 PyCharm
Open data source properties. You can open data source properties by using one of the following options:
In the Database tool window ( ) , click the Data Source Properties icon .
Press Shift+Enter.
In the Drivers section, click the MySQL driver and click the Duplicate button . Alternatively, press Ctrl+D.
Change the name of the duplicated driver (for example, MySQL socket).
On the General tab, click the Add button () and select Custom JARs.
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
Click OK.
On the Advanced tab, find the socketFactory property, double-click the Value cell, and change the value to
org.newsclub.net.mysql.AFUNIXDatabaseSocketFactory
.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).Click Apply.
Step 5. Create a connection to the MySQL server
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 MySQL.
and selectIn the Database tool window ( ) , click the New icon () in the toolbar. Navigate to Data Source and select MySQL.
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 PyCharm configuration directory.
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.
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
).Click the General tab.
From the Driver list, select the driver that you created earlier (refer to Step 4).
In User and Password fields, specify your user credentials.
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.
In case of any connection issues, refer to the Cannot connect to a database page.
(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.
Click OK to create the data source.
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 PyCharm, 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.