Menu

working example using read_default_file?

Help
estrand
2005-01-22
2012-09-19
  • estrand

    estrand - 2005-01-22

    Hello,

    I am looking for a working example of a database connection using a my.cnf file to access a database. I can only access a database if I provide the username, password, and database in the MySQLdb.connection method. Below are my pared-down PSP page, my.cnf file (/var/lib/mysql/my.cnf), and error message to show how I am attempting access with a my.cnf file.

    Perhaps the my.cnf file is being looked for in a different location, or I don't have the correct group heading in the my.cnf file?

    I am running a Gentoo server with the following versions of software:

    apache 2.0.52
    mysql 4.0.22
    python 2.3.4
    mysql-python 0.9.2

    Thanks,
    Eric


    <%
    from mod_python import apache
    import MySQLdb

    this line will work

    db = MySQLdb.connect(user="dbtest",passwd="dbtest",db="dbtest")

    this line does not work

    db = MySQLdb.connect(user="dbtest",read_default_file="/var/lib/mysql/my.cnf")

    this line does not work

    db = MySQLdb.connect(user="dbtest",read_default_file="my.cnf")

    db.close()
    %>

    <html>
    <head>
    <title>DB Test</title>
    </head>
    <body>
    DB Test
    </body>
    </html>


    [client]
    password = dbtest
    database = dbtest


    Mod_python error: "PythonHandler mod_python.psp"

    Traceback (most recent call last):

    File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
    result = object(req)

    File "/usr/lib/python2.3/site-packages/mod_python/psp.py", line 297, in handler
    p.run()

    File "/usr/lib/python2.3/site-packages/mod_python/psp.py", line 208, in run
    exec code in global_scope

    File "/var/www/localhost/htdocs/home/dbtest.psp", line 7, in ?
    db = MySQLdb.connect(user="dbtest",read_default_file="my.cnf")

    File "/usr/lib/python2.3/site-packages/MySQLdb/init.py", line 63, in Connect
    return apply(Connection, args, kwargs)

    File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 115, in init
    self._make_connection(args, kwargs2)

    File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 41, in _make_connection
    apply(super(ConnectionBase, self).init, args, kwargs)

    OperationalError: (1045, "Access denied for user: 'dbtest@localhost' (Using password: NO)")

    <END OF MESSAGE>

     
    • Andy Dustman

      Andy Dustman - 2005-01-22

      read_default_file does support ~ expansion, so you can specify ~/.my.cnf. And by default, it reads from the [client] section, but you can override this with read_default_group.

      Try this in your option file, and specify an absolute path, if necessary, and make sure your application (in this case, apache) can actually read it:

      [client]
      user = dbtest
      database = dbtest
      password = dbtest

       
    • estrand

      estrand - 2005-01-22

      Thank you for your quick reply.

      Your suggestion that apache be able to read the my.cnf file did the trick. I copied my.cnf to /etc/apache2/ and specified an absolute path to it in my connection. Bingo! It's now working as I exepected. Not sure why a specific path to /var/lib/mysql/my.cnf does not work, but that is a mystery for another day.

      Thanks once again for your help!

      --Eric

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.