python - mariadb.OperationalError: Access denied for user... but credentials are correct - Stack Overflow

admin2025-04-16  3

I use this simple connection script:

import configparser
import mariadb

config = configparser.ConfigParser()
config.read('dbconfig.ini')
config_default = config['DEFAULT']

print(f' mariadb -h {config_default["server"]} -u {config_default["username"]} -p{config_default["password"]} -P {config_default["port"]} {config_default["database"]}')

conn = mariadb.connect(
    user=config_default['username'],
    password=config_default['password'],
    host=config_default['server'],
    port=int(config_default['port']),
    database=config_default['database']
)

When I run this, I get:

mariadb.OperationalError: Access denied for user 'myuser'@'myhost.local' (using password: YES)

But when I just call the output of the print statement, it connects just fine. So i guess the credentials and everything is correct. For testing I also chose a password without any special characters.

My setup:

  • Database: 11.4.4-MariaDB-log
  • Python: Python 3.13.1
  • Connector: mariadb==1.1.11
  • Database client: Ver 15.1 Distrib 10.11.6-MariaDB

I use this simple connection script:

import configparser
import mariadb

config = configparser.ConfigParser()
config.read('dbconfig.ini')
config_default = config['DEFAULT']

print(f' mariadb -h {config_default["server"]} -u {config_default["username"]} -p{config_default["password"]} -P {config_default["port"]} {config_default["database"]}')

conn = mariadb.connect(
    user=config_default['username'],
    password=config_default['password'],
    host=config_default['server'],
    port=int(config_default['port']),
    database=config_default['database']
)

When I run this, I get:

mariadb.OperationalError: Access denied for user 'myuser'@'myhost.local' (using password: YES)

But when I just call the output of the print statement, it connects just fine. So i guess the credentials and everything is correct. For testing I also chose a password without any special characters.

My setup:

  • Database: 11.4.4-MariaDB-log
  • Python: Python 3.13.1
  • Connector: mariadb==1.1.11
  • Database client: Ver 15.1 Distrib 10.11.6-MariaDB
Share asked Feb 1 at 23:15 Mr. ClearMr. Clear 6526 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It was clear that there is some embarrassing reason for this: I used parenthesis for the password in the config file. Python added them to the password, so the password was wrong. When testing on the shell, the shell removed the parenthesis, so the password was correct.

转载请注明原文地址:http://anycun.com/QandA/1744813587a87978.html