Christopher Waugh
Christopher Waugh

Reputation: 431

How do I connect to an AWS mysql database in python3?

I can connect to mysql using cli like this:

mysql -u cwaugh -p******** -h example.com mydb

However, when I try to use python3 with the same parameters, I get an error: mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'cwaugh'@'xxx.xxx.xxx.xxx' (with my ip where the x should be)

My code for python looks like this:

import mysql.connector
import datetime

thinknode = mysql.connector.connect(
    host = "example.com",
    user = "cwaugh",
    passwd = "********",
    db = "mydb")

The credentials also work in javascript, but only if I used ssl: "Amazon RDS" when I created the connection (the database is on AWS RDS).

What do I need to do to connect from python? I can connect from other applications on this same computer, so I can rule out AWS Security Groups and MySQL host limits. This seems to only happen with python. Does this have something to do with ssl like it did with javascript?

Upvotes: 1

Views: 1511

Answers (1)

Christopher Waugh
Christopher Waugh

Reputation: 431

You need to add the ssl_ca argument to the mysql.connector.connect function. Eg.

thinknode = mysql.connector.connect(
    host = "example.com",
    user = "cwaugh",
    passwd = "********",
    db = "mydb",
    ssl_ca = "./rds-combined-ca-bundle.pem")

I'm not sure why javascript wass able to use "Amazon RDS", and it would be a lot easier if python did too, but it doesn't.

Upvotes: 1

Related Questions