mysqld_exporter icon indicating copy to clipboard operation
mysqld_exporter copied to clipboard

Unable to connect using unix socket

Open cf-sewe opened this issue 2 years ago • 3 comments

With mysqld_exporter 0.15.0 I am unable to connect to the MariaDB server using the new --mysqld.address option. I do not want to use a my.cnf file.

I tried two variants, but both failed:

--mysqld.address="unix(/var/run/mariadb/mysql.sock)"

2023-07-05T16:16:30.833935+02:00 db mariadb_exporter[3181430]: ts=2023-07-05T14:16:30.832Z caller=mysqld_exporter.go:225 level=info msg="Error parsing host config" file=.my.cnf err="failed to parse address: address unix(/var/run/mariadb/mysql.sock): missing port in address"
--mysqld.address="unix:///var/run/mariadb/mysql.sock"

2023-07-05T16:17:31.077223+02:00 db mariadb_exporter[3181808]: ts=2023-07-05T14:17:31.075Z caller=config.go:146 level=error msg="failed to parse config" section=client err="set field \"port\": strconv.ParseInt: parsing \"///var/run/mariadb/mysql.sock\": invalid syntax"

How should I specify the unix socket?

cf-sewe avatar Jul 05 '23 14:07 cf-sewe

Same issue here. It looks like as the UNIX sockets are incorrectly parsed. The exporter always expects a port, although a UNIX socket doesn't have one.

It also looks like as the my.cnf file must exist, although it's optional and can be used as stated in the changelog:

ts=2023-08-05T14:14:59.993Z caller=mysqld_exporter.go:225 level=info msg="Error parsing host config" file=/home/prometheus/.my.cnf err="no configuration found"

To configure connections to MySQL you can either use a my.cnf style config file or command line arguments. - https://github.com/prometheus/mysqld_exporter/releases/tag/v0.15.0

--config.my-cnf should be an optional argument, which can be fully left out of the configuration in my opinion since you can use the other available arguments for configuring the database connection.

Sebbo94BY avatar Aug 05 '23 14:08 Sebbo94BY

I don't know if this was subsequently fixed but I looked at how the config was used https://github.com/prometheus/mysqld_exporter/blob/dd8afce2a46663a5112e53469f53ca56d50a63e2/config/config.go#L64 and it looks like it supports socket=/var/lib/mysql/mysql.sock in a my.cnf file, and it was able to connect, now it has some bogus error err="Error 1049 (42000): Unknown database '&lock_wait_timeout=2'" which is a new problem. ;-) edit: looks like the password handling is b0rken as it doesn't treat the password as an opaque blob and fails if there are certain special characters in the password, so I needed to regen the creds to avoid that https://github.com/prometheus/mysqld_exporter/issues/761

mtinberg avatar Jun 07 '24 04:06 mtinberg

I don't know if this was subsequently fixed but I looked at how the config was used

https://github.com/prometheus/mysqld_exporter/blob/dd8afce2a46663a5112e53469f53ca56d50a63e2/config/config.go#L64

and it looks like it supports socket=/var/lib/mysql/mysql.sock in a my.cnf file, and it was able to connect, ...

it looks like it supports socket=/var/lib/mysql/mysql.sock in a my.cnf file

Confirmed with mysqld_exporter v0.16.0 used at k8s deployment:

Changed the socket within the ConfigMap from

apiVersion: v1
kind: ConfigMap
name: mysql-idbc-metricsconf
namespace: mysql-idbc
data:
  metrics.cnf: |
    [client]
    user=mysqlmetrics
    socket=unix:///var/run/mysqld/mysql.sock

to

apiVersion: v1
kind: ConfigMap
name: mysql-idbc-metricsconf
namespace: mysql-idbc
data:
  metrics.cnf: |
    [client]
    user=mysqlmetrics
    socket=/var/run/mysqld/mysql.sock

and the container within the pod got up like expected without the issue. 🥳

CodeAdminDe avatar Nov 26 '24 06:11 CodeAdminDe

Hi, I can confirm the workaround and here is my setup:

  • Debian Trixie;
  • Prometheus-mysqld-exporter installed from debian packages (sudo apt install prometheus-mysqld-exporter).

Configure systemd service:

❯ cat /etc/.my.cnf 
[client]
user=prometheus
socket=/run/mysqld/mysqld.sock
❯ cat /etc/default/prometheus-mysqld-exporter
ARGS="--config.my-cnf=/etc/.my.cnf"

Configure prometheus user:

CREATE USER IF NOT EXISTS 'prometheus'@'localhost' IDENTIFIED WITH unix_socket;
GRANT PROCESS, REPLICATION CLIENT, SLAVE MONITOR, SELECT ON *.* TO 'prometheus'@'localhost';

Note 1/ the following was working on Debian 12 (bookworm) but no more on Debian 13:

❯ cat /etc/default/prometheus-mysqld-exporter
# no configuration file needed (/etc/.my.cnf): ARGS="--config.my-cnf=/etc/.my.cnf"
DATA_SOURCE_NAME="prometheus@unix(/run/mysqld/mysqld.sock)/"

2/ the SLAVE MONITOR grant is missing from the /etc/default/prometheus-mysqld-exporter suggestion comments.

I will raise this to the Debian Go Packaging Team [email protected].

fauust avatar Sep 08 '25 12:09 fauust