473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

connections.py

Does anyone know how to change the connections.py file in /usr/lib/python2.4/site-packages/MySQLdb/connections.py"
to a specific user?

The error I get is:

_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")

Attrched is the begiining of the file:
Expand|Select|Wrap|Line Numbers
  1. """
  2.  
  3. This module implements connections for MySQLdb. Presently there is
  4. only one class: Connection. Others are unlikely. However, you might
  5. want to make your own subclasses. In most cases, you will probably
  6. override Connection.default_cursor with a non-standard Cursor class.
  7.  
  8. """
  9. import cursors
  10. from _mysql_exceptions import Warning, Error, InterfaceError, DataError, \
  11.      DatabaseError, OperationalError, IntegrityError, InternalError, \
  12.      NotSupportedError, ProgrammingError
  13. import types, _mysql
  14.  
  15.  
  16. def defaulterrorhandler(connection, cursor, errorclass, errorvalue):
  17.     """
  18.  
  19.     If cursor is not None, (errorclass, errorvalue) is appended to
  20.     cursor.messages; otherwise it is appended to
  21.     connection.messages. Then errorclass is raised with errorvalue as
  22.     the value.
  23.  
  24.     You can override this with your own error handler by assigning it
  25.     to the instance.
  26.  
  27.     """
  28.     error = errorclass, errorvalue
  29.     if cursor:
  30.         cursor.messages.append(error)
  31.     else:
  32.         connection.messages.append(error)
  33.     del cursor
  34.     del connection
  35.     raise errorclass, errorvalue
  36.  
  37.  
  38. class Connection(_mysql.connection):
  39.  
  40.     """MySQL Database Connection Object"""
  41.  
  42.     default_cursor = cursors.Cursor
  43.  
  44.     def __init__(self, *args, **kwargs):
  45.         """
  46.  
  47.         Create a connection to the database. It is strongly recommended
  48.         that you only use keyword parameters. Consult the MySQL C API
  49.         documentation for more information.
  50.  
  51.         host
  52.           string, host to connect
  53.  
  54.         user
  55.           string, user to connect as
  56.  
  57.         passwd
  58.           string, password to use
Jan 21 '07 #1
8 9329
bartonc
6,596 Expert 4TB
Hello and welcome to the Python forum on TSDN.
The problem is not with /usr/lib/python2.4/site-packages/MySQLdb/connections.py
The error I get is:

_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")
The problem is that way you are calling it. Please post your code (not the library code). I'll dig up some examples that I have written which will help.
Jan 21 '07 #2
bartonc
6,596 Expert 4TB
Hello and welcome to the Python forum on TSDN.
The problem is not with /usr/lib/python2.4/site-packages/MySQLdb/connections.py"

The problem is that way you are calling it. Please post your code (not the library code). I'll dig up some examples that I have written which will help.
If this is a shared machine on "unix" you will most likely NOT have a root login and you will need the admin to create an account for you. If this is your own machine or you have an account already set up then you can get a connection like this:
Expand|Select|Wrap|Line Numbers
  1. from MySQLdb import *
  2. # replace servername, username and password with your account info
  3. dbconnect = connect(host=servername, user=username, passwd=password)
  4. dbcursor = dbconnect.cursor()
  5. # once you have a cursor, you can send SQL commands to the DB, like:
  6. dbcursor.execute('SET autocommit=1')
Here are some useful classes and helper functions that I wrote.
Jan 21 '07 #3
Hi and thanks for getting back to me here is the code you asked for...

and the error again

_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")

Expand|Select|Wrap|Line Numbers
  1. import MySQLdb
  2. import os
  3. from splicegraph_jan06 import *
  4.  
  5. spliceCalcs={'hg17_JAN06':
  6.              TableGroup(db='SPLICE_JAN06',suffix='hg17',clusters='cluster_hg17',
  7.                         exons='isoform_exon_form_hg17',splices='splice_verificat
  8. ion_hg17',
  9.                         genomic='genomic_cluster_hg17',mrna='isoform_mrna_seq_hg
  10. 17',
  11.                         protein='isoform_protein_seq_hg17'),
  12.              'mm7_JAN06':
  13.              TableGroup(db='SPLICE_JAN06',suffix='mm7',clusters='cluster_mm17',
  14.                         exons='isoform_exon_form_mm7',splices='splice_verificati
  15. on_mm7',
  16.                         genomic='genomic_cluster_mm7',mrna='isoform_mrna_seq_mm7
  17. ',
  18.                         protein='isoform_protein_seq_mm7')
  19.              }
  20.  
  21.  
  22. def getUserCursor(db):
  23.     'get a cursor as the current user'
  24.     db=MySQLdb.connect(db=db,read_default_file=os.environ['HOME']+'/.my.cnf',com
  25. press=True)
  26.     return db.cursor()
  27.  
  28. def getSpliceGraphFromDB(dbgroup,loadAll=False):
  29.     """load data from MySQL using the designated database table group.
  30.     If loadAll true, then load the entire splice graph into memory."""
  31.     cursor=getUserCursor(dbgroup.db)
  32.     import sys
  33.     print >>sys.stderr,'Reading database schema...'
  34.     idDict={}
  35.     tables=describeDBTables(dbgroup.db,cursor,idDict)
  36.     if hasattr(dbgroup,'suffix'):
  37.         tables=suffixSubset(tables,dbgroup.suffix) # SET OF TABLES ENDING IN JUN
  38. 03
  39.         idDict=indexIDs(tables) # CREATE AN INDEX OF THEIR PRIMARY KEYS
  40.     for t in dbgroup.values():
  41.         if t is not None and '.' in t and t not in tables: # THIS TABLE COMES FR
  42. OM ANOTHER DATABASE...
  43.             tables[t]=SQLTable(t,cursor) # SO GET IT FROM OTHER DATABASE
  44.  
  45.     # LOAD DATA & BUILD THE SPLICE GRAPH
  46.     return loadSpliceGraph(tables,dbgroup.clusters,dbgroup.exons,dbgroup.splices
  47. ,
  48.                            dbgroup.genomic,dbgroup.mrna,dbgroup.protein,loadAll)
  49.  
  50. def localCopy(localFile,cpCommand):
  51.     'if not already present on local file location, run cpCommand'
  52.     if not os.access(localFile,os.R_OK):
  53.         cmd=cpCommand % localFile
  54.         print 'copying data:',cmd
  55.         exit_code=os.system(cmd)
  56.         if exit_code!=0:
  57.             raise OSError((exit_code,'command failed: %s' % cmd))
  58.     return localFile
Jan 22 '07 #4
bartonc
6,596 Expert 4TB
Hi and thanks for getting back to me here is the code you asked for...

and the error again

_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: NO)")

Expand|Select|Wrap|Line Numbers
  1. import MySQLdb
  2. import os
  3. def getUserCursor(db):
  4.     'get a cursor as the current user'
  5.     db=MySQLdb.connect(db=db,read_default_file=os.environ['HOME']+'/.my.cnf',com
  6. press=True)
  7.     return db.cursor()
First I'd see if you can make this work
Expand|Select|Wrap|Line Numbers
  1. # replace servername, username and password with your account info
  2. dbconnect = connect(host=servername, user=username, passwd=password)
  3. dbcursor = dbconnect.cursor()
  4.  
Then, if that works, check to see if your
my.cnf
is set up correctly. I should look like this:
You can specify connection parameters in the [client] section of an option file. The relevant section of the file might look like this:
[client]
host=host_name
user=user_name
password=your_pass
Section 4.3.2, “Using Option Files”, discusses option files further.
according to MySQL docs.
It also looks like you can specify other that "client" group using
read_default_group
configuration group to use from the default file
but mysqld docs are not very clear on that.
Jan 22 '07 #5
Hi again,

I enter the following lines you recommend

dbconnect = connect(host=hostname, user=root, passwd=*******)
dbcursor = dbconnect.cursor()

and get the following error?

File "/home/pedro/pygr/tests/lldb_jan06.py", line 22, in ?
dbconnect = connect(host=hostname, user=root, passwd=*******)
NameError: name 'connect' is not defined


First I'd see if you can make this work
Expand|Select|Wrap|Line Numbers
  1. # replace servername, username and password with your account info
  2. dbconnect = connect(host=servername, user=username, passwd=password)
  3. dbcursor = dbconnect.cursor()
  4.  
Then, if that works, check to see if your
my.cnf
is set up correctly. I should look like this:
You can specify connection parameters in the [client] section of an option file. The relevant section of the file might look like this:
[client]
host=host_name
user=user_name
password=your_pass
Section 4.3.2, “Using Option Files”, discusses option files further.
according to MySQL docs.
It also looks like you can specify other that "client" group using
read_default_group
configuration group to use from the default file
but mysqld docs are not very clear on that.
Jan 22 '07 #6
bartonc
6,596 Expert 4TB
Hi again,

I enter the following lines you recommend

dbconnect = connect(host=hostname, user=root, passwd=*******)
dbcursor = dbconnect.cursor()

and get the following error?

File "/home/pedro/pygr/tests/lldb_jan06.py", line 22, in ?
dbconnect = connect(host=hostname, user=root, passwd=*******)
NameError: name 'connect' is not defined
Sorry, that was a snippet.. should be:
Expand|Select|Wrap|Line Numbers
  1. from MySQLdb import *
  2. dbconnect = connect(host=hostname, user=root, passwd=*******)
  3. dbcursor = dbconnect.cursor()
  4.  
as in my reply #3 above.
Jan 22 '07 #7
Ok that worked but now I get another error from a second py program...

File "/home/pedro/pygr/tests/lldb_jan06.py", line 34, in getSpliceGraphFromDB
tables=describeDBTables(dbgroup.db,cursor,idDict)
File "/usr/lib/python2.4/site-packages/pygr/sqlgraph.py", line 212, in describeDBTables
cursor.execute('use %s' % name)
AttributeError: 'NoneType' object has no attribute 'execute'

Here is the portion of the script:

Expand|Select|Wrap|Line Numbers
  1. def describeDBTables(name,cursor,idDict):
  2.     """
  3.     Get table info about database <name> via <cursor>, and store primary keys
  4.     in idDict, along with a list of the tables each key indexes.
  5.     """
  6.     cursor.execute('use %s' % name)
  7.     cursor.execute('show tables')
  8.     tables={}
  9.     l=[c[0] for c in cursor.fetchall()]
  10.     for t in l:
tname=name+'.'+t
o=SQLTable(tname,cursor)
[/code]

and the original change from the other py script


[code]
def getUserCursor(db):
'get a cursor as the current user'
#db=MySQLdb.connect(db=db,read_default_file=os.env iron['HOME']+'/.my.cnf',com
#press=True)
# return db.cursor()
from MySQLdb import *
dbconnect = connect(host='localhost', user='root', passwd='retep1er')
dbcursor = dbconnect.cursor()
[code]

Incidentally I changed my /etc/mysql/my.cnf file (my only my.cnf) file

[client]
[client]
user = 'root'
password = '********'
port = 3306
socket = /var/run/mysqld/mysqld.sock

with the original configuration of the py file

[code]
def getUserCursor(db):
'get a cursor as the current user'
db=MySQLdb.connect(db=db,read_default_file=os.envi ron['HOME']+'/.my.cnf',com
press=True)
return db.cursor()

[\CODE]

and this produced the original error?
Jan 22 '07 #8
bartonc
6,596 Expert 4TB
If this is a shared machine on "unix" you will most likely NOT have a root login and you will need the admin to create an account for you. If this is your own machine or you have an account already set up then you can get a connection like this:
Expand|Select|Wrap|Line Numbers
  1. from MySQLdb import *
  2. # replace servername, username and password with your account info
  3. dbconnect = connect(host=servername, user=username, passwd=password)
  4. dbcursor = dbconnect.cursor()
  5. # once you have a cursor, you can send SQL commands to the DB, like:
  6. dbcursor.execute('SET autocommit=1')
Here are some useful classes and helper functions that I wrote.
Here for error handling example.
Jan 22 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Randell D. | last post by:
Folks, I currently connect to my db with PHP code that uses non-persistent connections. I've read that persistent connections can help performance since a connection to the db will use an...
3
by: Mudge | last post by:
Hi, My hosting provider only allows me to use 50 connections to my MySQL database that my Web site will use. I don't know what this 50 connections means exactly. Does this mean that only 50...
4
by: Angelos | last post by:
I get this error mysql_pconnect Too many connections ... every now and then. Does anyone knows where it comes from ? There are a lot of sites running on the server and all of them use the...
1
by: C Sharp beginner | last post by:
I'm sorry about this verbose posting. This is a follow-up to my yesterday's posting. Thanks William for your reply. I understand it is a good practice to open connections as late as possible and...
2
by: Bob | last post by:
We have a production web site that's data intensive (save user input to DB and query for displaying) with the ASP.NET app part on one W2K server and SQL 2000 DB on another W2K server. I have set...
17
by: Peter Proost | last post by:
Hi Group, I've got an interesting problem, I don't know if this is the right group but I think so because everything I've read about it so far says it's a .net problem. Here's the problem, we're...
4
by: elyob | last post by:
Not really tried going two ways at once, but I have an include_once connection to a mysql_database, now I need to retrieve info from a second mysql_database .. My mysql_connects are getting...
1
by: marcfischman | last post by:
Please help. I have a website running on a linux/apache/mysql/php server. I receive about 8,000-10,000 visitors a day with about 200,000 to 300,000 page views. The server is a RedHat Linux...
13
by: PRP | last post by:
Hi, Our DBA has complained about the large number of connections from the aspnet_wp process. We have multiple web applications deployed in different virtual directories. I read that the way...
5
by: Usman Jamil | last post by:
Hi I've a class that creates a connection to a database, gets and loop on a dataset given a query and then close the connection. When I use netstat viewer to see if there is any connection open...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.