mysql, use password from my.cnf file

177 views
Skip to first unread message

Tom H

unread,
Sep 29, 2008, 5:48:18 PM9/29/08
to sqlalchemy
Depending on security preferences, it may be desirable to keep
database passwords out of code files.

One of the recommendations for MySQL is to keep the password in
a .my.cnf file accessible only to the user.
http://dev.mysql.com/doc/refman/5.0/en/password-security.html

To use the .my.cnf password in sqlalchemy, do something like the
following:

from sqlalchemy.engine.url import URL
myDB = URL(drivername='mysql', host='localhost',
database='my_database_name',
query={ 'read_default_file' : '/path/to/.my.cnf' } )
engine = create_engine(name_or_url=myDB)
# use the engine as usual, no password needed in your code file :)

This has been very useful to me, hopefully others find it helpful as
well.

Anon Define

unread,
May 16, 2014, 1:15:40 PM5/16/14
to sqlal...@googlegroups.com
excellent; thanks!

Balaji Pattewar

unread,
Aug 7, 2015, 8:26:50 PM8/7/15
to sqlalchemy
Hi ,

Iam trying to use


from sqlalchemy.engine.url import URL
myDB = URL(drivername='mysql', host='localhost',
database='my_database_name',
  query={ 'read_default_file' : '/path/to/.my.cnf' } )
engine = create_engine(name_or_url=
myDB)
but it is not working form me. The error I am getting is
sqlalchemy.exc.OperationalError: (OperationalError) (1045, "Access denied for user 'root'@'localhost' (using password: NO)") None None

This is occurring because SQLALCHEMY is not reading password from .my conf file.
It is working fine if I give username and password as URL argument. The my.conf file is
also correct which is working if pass it to mysql shell command.

Any comment on how to make SQLALCHEMY/MYSQL read password from file.

Thanks
Balaji
Reply all
Reply to author
Forward
0 new messages