Question

I'm working on the front end of a webapp, and my co-developer is using Pyramid and SQAlchemy. We've just moved from SQLite to MySQL. I installed MySQL 5.6.15 (via Homebrew) on my OS X machine to get the Python MySQLdb install to work (via pip in a virtualenv).

Because in MySQL >= 5.6.5 secure_auth is now ON by default I can only connect to the remote database (pre 5.6.5) with the --skip-secure-auth flag, which works fine in a terminal.

However, in the Python Pyramid code, it only seems possible to add this flag as an argument to create_engine(), but I can't find create_engine() in my co-dev's code, only the connection string below in an initialisation config file. He's not available, this isn't my area of expertise, and we launch next week :(

sqlalchemy.url = mysql+mysqldb://gooddeeds:deeds808letme1now@146.227.24.38/gooddeeds_development?charset=utf8

I've tried appending various "secure auth" strings to the above with no success. Am I looking in the wrong place? Has MySQLdb set secure_auth to ON because I'm running MySQL 5.6.15? If so, how can I change that?

Was it helpful?

Solution

If you are forced to use the old passwords (bah!) when using MySQL 5.6, and using MySQLdb with SQLAlchemy, you'll have to add the --skip-secure-auth to an option file and use URL:

from sqlalchemy.engine.url import URL

..

dialect_options = {
  'read_default_file': '/path/to/your/mysql.cnf',
}

engine = create_engine(URL(
  'mysql',
  username='..', password='..',
  host='..', database='..',
  query=dialect_options
  ))

The mysql.cnf would contain:

[client]
skip-secure-auth

For Pyramid, you can do the following. Add a line in your configuration ini-file that holds the connection arguments:

sqlalchemy.url = mysql://scott:tiger@localhost/test
sqlalchemy.connect_args = { 'read_default_file': '/path/to/foo' }

Now you need to change a bit the way the settings are read and used. In the file that launches your Pyramic app, do the following:

def main(global_config, **settings):
    try:
        settings['sqlalchemy.connect_args'] = eval(settings['sqlalchemy.connect_args'])
    except KeyError:
        settings['sqlalchemy.connect_args'] = {}
    engine = engine_from_config(settings, 'sqlalchemy.')
    # rest of code..

The trick is to evaluate the string in the ini file which contains a dictionary with the extra options for the SQLAlchemy dialect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top