albertodonato / query-exporter

Export Prometheus metrics from SQL queries
GNU General Public License v3.0
436 stars 101 forks source link

Driver error mssql+pyodbc Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) #159

Open Kiara0107 opened 1 year ago

Kiara0107 commented 1 year ago

Describe the bug Error when trying to connect the database, log shows:

2023-08-18 08:20:27,219 - ERROR - query-exporter - error from database "database_01": (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
(Background on this error at: http://sqlalche.me/e/13/dbapi)

Installation details

To Reproduce

If possible, please provide detailed steps to reproduce the behavior:

  1. Config file content (redacted of secrets if needed)

    databases:
    database_01:
    dsn: mssql+pyodbc://{{ user }}:{{ password }}@{{ host }}:1433/{{ db }}?driver=ODBC+Driver+17+for+SQL+Server&MARS_Connection=yes
    keep-connected: false
    autocommit: false
  2. Ran query-exporter with the following command line ...

    docker run -d  -it -p 9560:9560/tcp \
      -v /etc/query/query.yml:/config.yaml \
      --restart unless-stopped \
      --name query-exporter adonato/query-exporter:latest
  3. Got the error when ... As soon as the first connection attempt is made.

Query exporter metrics:

# HELP database_errors_total Number of database errors
# TYPE database_errors_total counter
database_errors_total{database="database_01""} 22756.0
# HELP database_errors_created Number of database errors
# TYPE database_errors_created gauge
database_errors_created{database="database_01"} 1.6921779978228438e+09
# HELP queries_total Number of database queries
# TYPE queries_total counter
# HELP query_latency Query execution latency
# TYPE query_latency histogram
# HELP exceptionlog_count Sum of log entries in ExceptionLog table
# TYPE exceptionlog_count gauge
albertodonato commented 1 year ago

This should be fixed in 2.9.0, which is also current latest docker image

Kiara0107 commented 1 year ago

Unfortunately not. To be sure I removed the docker image and pulled the latest image from the repository.

~$ docker images
REPOSITORY                        TAG       IMAGE ID       CREATED        SIZE
adonato/query-exporter            latest    98ec5e988c9c   2 days ago     515MB

Still the same error.

~$ docker logs query-exporter
2023-08-21 07:32:33,727 - ERROR - query-exporter - error from database "database_01": (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
albertodonato commented 1 year ago

Can you try using the dictionary format for the dsn, just to make sure it's not an issue with passing driver options?

Kiara0107 commented 1 year ago

What do you mean by 'dictionary format' for the dsn?

albertodonato commented 1 year ago
dsn:
  dialect: <dialect>[+driver]
  user: <username>
  password: <password>
  host: <host>
  port: <port>
  database: <database>
  options:
    <key1>: <value1>
    <key2>: <value2>

as shown in the readme

Kiara0107 commented 1 year ago

Ah, yes, tried that as well. Unfortunately the same result:

2023-08-23 13:07:14,425 - ERROR - query-exporter - error from database "database_01": (pyodbc.Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
(Background on this error at: http://sqlalche.me/e/13/dbapi)

In query.yml

databases:
  database_01:
    dsn:
      dialect: mssql+pyodbc
      user: {{user}}
      password: {{password}}
      host: {{host}}
      port: 1433
      database: database_01
      options:
        driver: 'ODBC Driver 17 for SQL Server'
        MARS_Connection: yes
    keep-connected: false
    autocommit: false
Kiara0107 commented 11 months ago

@albertodonato any updates or suggestions on this matter?

Kiara0107 commented 11 months ago

I found a sort of work-around. When I update line 49 to ACCEPT_EULA=Y apt-get install -y --no-install-recommends msodbcsql17 && \ it works. And then ofcourse build locally a new image

albertodonato commented 11 months ago

Would you mind proposing a PR with the change?

ilantnt commented 10 months ago

@albertodonato Hi, I have an local branch ready for this fix (as written above), Can you add me permissions to submit my branch?

albertodonato commented 10 months ago

What do you mean? You don't need permissions to make a PR with your changes

Drakonhawk commented 10 months ago

With the current 2.9.2 image you have to change the URL to use ODBC driver 18 and ignore the self signed SSL certificate.

from: mssql+pyodbc://user:password@host_ip:port/database?driver=ODBC+Driver+17+for+SQL+Server&MARS_Connection=Yes

to: mssql+pyodbc://user:password@host_ip:port/database?TrustServerCertificate=yes&driver=ODBC+Driver+18+for+SQL+Server&MARS_Connection=Yes