Skip to main content

Setting program_name in mysql session_connect_attrs allows you to see the name of the program connected

I am using mysql for quite some time now.

I have noticed that looking at client connections using mysql workbench so an interesting column called "Program". I can see that the program column indicate "MySQL Workbench":

Client Connections view inside mysql workbench showin the Program column

 This column can be very useful and help to filter different mysql database connections.

It took me a while to understand how to set up this column data.

So first thing I needed to do is understand where is this column name is actually coming from. I inspected the "SHOW FULL PROCESSLIST" command by mysql - but I did not get any answer there.

I then looked at the query the mysql workbench itself running to get this data "Program" column:

Using the "show details" button in the "client connections" view, show me this:

Showing the details query for full process list within client connections to mysql workbench

Copying the query, it looks like that:

SELECT t.PROCESSLIST_ID,IF (NAME = 'thread/sql/event_scheduler','event_scheduler',t.PROCESSLIST_USER) PROCESSLIST_USER,
t.PROCESSLIST_HOST,
t.PROCESSLIST_DB,
t.PROCESSLIST_COMMAND,
t.PROCESSLIST_TIME,
t.PROCESSLIST_STATE
,t.THREAD_ID,
t.TYPE,
t.NAME,
t.PARENT_THREAD_ID,
t.INSTRUMENTED,
t.PROCESSLIST_INFO,
a.ATTR_VALUE
FROM performance_schema.threads t  LEFT OUTER JOIN performance_schema.session_connect_attrs a ON t.processlist_id = a.processlist_id AND (a.attr_name IS NULL OR a.attr_name = 'program_name') WHERE t.TYPE <> 'BACKGROUND' 

It seems that mysql saves some extra data on the connection within the table performance_schema.session_connect_attrs and specifically the  attr_name column.

The performance_schema.session_connect_attrs table, which allows you to store custom key-value pairs associated with a client connection. This is invaluable for identifying and monitoring connections from different applications or parts of your system. 

MySQL provides a mechanism for client applications to send a set of key-value pairs (connection attributes) to the server during the connection handshake. These attributes are then exposed in the performance_schema.session_connect_attrs table (and session_account_connect_attrs for the current user's connections).

Commonly used attributes include _client_name, _client_version, _os, _pid, _platform, and program_name. You can also define your own custom attributes. Attribute names starting with an underscore (_) are reserved for internal MySQL use.

Here is how you can us Go (Golang mysql drive) to set this key-value pairs and most important the program_name field which later on views as "Program" inside MySQL Workbench.

 

user := "your_user"
    password := "your_password"
    host := "localhost"
    port := "3306"
    database := "your_database"

    // Define your custom connection attributes
    connectAttrs := "program_name:MyGoApp,environment:development,module:data_access"

    // Construct the DSN string
    // The `connectionAttributes` parameter is added to the query string part of the DSN.
    dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?connectionAttributes=%s",
        user, password, host, port, database, connectAttrs)


 


Comments

Popular posts from this blog

Accessing Windows Share (Samba) From Linux (XFCE) using Thunar

In order to access samba (windows share) from Linux XFCE using Thunar file browser you need to: 1. Install samba client sudo apt install smbclient 2. Run: sudo modprobe cifs 3. Open thunar and access the share: smb://IPADDRESS/sharename e.g.: smb://192.168.1.1/photos 4. Type in the username / password in the prompt window Some helpful information: https://forum.manjaro.org/t/thunar-how-do-i-access-folders-shared-on-windows-computers/116649/9

Using phpword to merge two Mircrosoft Office Word .docx documents

How to combine or embed and insert another .docx file (Microsoft office docx word document) into another one using PHPWord Joining two .docx document using php ( phpword library ) $mainTemplateProcessor = new \PhpOffice\PhpWord\TemplateProcessor("file1"); //$mainTemplateProcessor ->setValue('var_name', $value); $innerTemplateProcessor = new \PhpOffice\PhpWord\TemplateProcessor("file2"); //$innerTemplateProcessor->setValue('var2_name', $value2); // extract internal xml from template that will be merged inside main template $innerXml = $innerTemplateProcessor->gettempDocumentMainPart(); $innerXml = preg_replace('/^[\s\S]*<w:body>(.*)<\/w:body>.*/', '$1', $innerXml); // remove tag containing header, footer, images $innerXml = preg_replace('/<w:sectPr>.*<\/w:sectPr>/', '', $innerXml); // inject internal xml inside main template $mainXml = $mainTemplateProcessor->gettempDocumentMainPart(...

Bypassing the error by "go get" "tls: failed to verify certificate: x509: certificate signed by unknown authority"

When I was trying to download dependencies for my go project in an old Ubuntu machine I was getting this error all the time: "go: gopkg.in/alexcesaro/quotedprintable.v3@v3.0.0-20150716171945-2caba252f4dc: Get "https://proxy.golang.org/gopkg.in/alexcesaro/quotedprintable.v3/@v/v3.0.0-20150716171945-2caba252f4dc.mod": tls: failed to verify certificate: x509: certificate signed by unknown authority" Which the main part of it was go get failing to authenticate: " tls: failed to verify certificate: x509: certificate signed by unknown authority " I tried many things but couldn't make it work until I found the way: export GOINSECURE="proxy.golang.go" This will tell go get to ignore certification validity. Then export GOPROXY=direct This will tell go get to by pass proxy Then git config --global http.sslverify false And only after those I could run again: go get And it worked