diff --git a/README.md b/README.md index d1c331d..7d93507 100644 --- a/README.md +++ b/README.md @@ -126,12 +126,3 @@ Available at `http://localhost:9000/metrics`: - `msg_retry_total{queue_name, uuid, retry_count}`: Retry attempts - `msg_failed_total`: Failed deliveries - `queue_depth{queue_name}`: Current queue message counts - -## Troubleshooting - -### Connection Issues -- `aiomysql` uses `PyMySQL` which requires the `mysql_native_password` plugin to be used. Can be checked and changed via: -```sql -SELECT user, host, plugin FROM mysql.user WHERE user='backend-push-notifications'; -ALTER USER 'backend-push-notifications'@'%' IDENTIFIED WITH mysql_native_password BY 'db_password'; -``` diff --git a/src/db.py b/src/db.py index e003dce..89d92a0 100644 --- a/src/db.py +++ b/src/db.py @@ -1,6 +1,7 @@ import asyncio import os from contextlib import asynccontextmanager +import ssl import aiomysql import pymysql.err @@ -8,13 +9,18 @@ import pymysql.err from secret_manager import get_secret from simple_logger_handler import setup_logger -db_username = get_secret(os.getenv("DB_USERNAME",""),"/etc/secrets/db_username") -db_password = get_secret(os.getenv("DB_PASSWORD",""),"/etc/secrets/db_password") +db_username = get_secret("DB_USERNAME","/etc/secrets/db_username") +db_password = get_secret("DB_PASSWORD","/etc/secrets/db_password") db_host = os.getenv("DB_HOST","localhost") db_database = os.getenv("DB_DATABASE","app") logger = setup_logger(__name__) +#mysql_native is deprecated, caching_sha2_password needs a encrypted connection on first login to work +#aiomysql does not create that by default but mysql-connector-python does, so this is the workaround +_ssl_ctx = ssl.create_default_context() +_ssl_ctx.check_hostname = False +_ssl_ctx.verify_mode = ssl.CERT_NONE class DBManager: def __init__(self, host, user, password, db, port=3306, pool_size=5, health_interval=60): @@ -40,7 +46,8 @@ class DBManager: minsize=1, maxsize=self.pool_size, autocommit=True, - cursorclass=aiomysql.DictCursor + cursorclass=aiomysql.DictCursor, + ssl=_ssl_ctx, ) logger.info("[DB] Connection pool created") logger.debug(f"[DB] Pool object: {self._pool}")