2

I need to pass in a custom argument to SQLAlchemy's create_engine function in order to SSL authorize my database connection with AWS RDS. here, here, and here it was recommended that I use SQLAlchemy's connect_args to pass in my custom keyword argument. I tried:

engine = create_engine(os.environ['DB_URI'], connect_args={'ssla':'amazon-rds-ca-cert.pem'})

However, I the following TypeError, which is the same error other posters said connect_args fixed for them:

TypeError: 'ssla' is an invalid keyword argument for this function

It seems like I'm doing this exactly right. I also tried passing the keyword arguments in the connection string:

mysql://name:password@localhost/test?sslca=amazon-rds-ca-cert.pem

And changing the names of the keyword arguments to exactly match the example in the sqlalchemy docs:

db = create_engine('mysql://name:password@localhost/test', connect_args = {'argument2':'bar'})

However, still no luck. It seems that the error is being thrown by MySQLdb in the error message, so perhaps it's an error there?

File "/Users/tobyweed/workspace/ftm/live-event-market/server/serverenv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 411, in connect
    return self.dbapi.connect(*cargs, **cparams)
File "/Users/tobyweed/workspace/ftm/live-event-market/server/serverenv/lib/python2.7/site-packages/MySQLdb/__init__.py", line 86, in Connect
    return Connection(*args, **kwargs)
File "/Users/tobyweed/workspace/ftm/live-event-market/server/serverenv/lib/python2.7/site-packages/MySQLdb/connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
TypeError: 'ssla' is an invalid keyword argument for this function

However, I don't have MySQLdb installed, so it must be SQLAlchemy which is using it. I have the latest version of SQLAlchemy (1.2.8), so that can't be it.

Please help!

1 Answer 1

3

The argument is being passed upstream, that is, to your mysql dbapi module - All sqlalchemy does is call the connect function from the chosen module.

As you can see here https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html sslca is not a valid parameter to the connect function - the correct parameter name would be ssl_ca I think?

In other words, you have to find out how to do what you want with python-mysql directly, without using sqlalchemy at all - sqlalchemy is not the culprit here

Also you say

However, I don't have MySQLdb installed, so it must be SQLAlchemy which is using it.

SQLAlchemy is not a database, it doesn't connect to anything. It is only a layer, you have to provide a database module for it to use to connect to databases.

Sign up to request clarification or add additional context in comments.

5 Comments

Smart, you're right. However, I tried ssl_ca, (and ssl_cert), which are supposedly valid parameters--no change to the error message besides the updated name.
Also I think SQLAlchemy must automatically handle the connection with MySQLdb (no providing by me necessary), because it all works swimmingly without any database module configuration by me (if I remove the ssl_ca param). Although I do have mysqlclient installed.
I assure you that SQLAlchemy CAN'T connect to any database by itself - it will always use some other module.
I suggest using the excellent oursql to connect to mysql databases - we use it in production here. The parameter there is named ssl and it works fine.
SQLAlchemy automatically uses MySQLdb. But anyways, it turns out the documentation for MySQL's connect params is slightly misleading--MySQLdb's connect function actually requires the SSL params to be formatted like: {'ssl':{'ca':'value'}} instead of {'sslca':'value'}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.