1

Hi I'm trying to connect from python to MySQL but I can't apparently due to below error. I tried it with this...

import MySQLdb
cnx = MySQLdb.connect(user     ='phpmyadmin',
                      passwd   ='raspberry', 
                      host     ='192.168.0.58',
                      database ='freddy')

print 'connected'
cnx.close()

and then i changed "Mysqldb" to "Mysql.connector" but doesnt work. This is the error showing :

Traceback (most recent call last): File "test.py", line 2, in cnx = MySQLdb.connect(user='phpmyadmin',passwd='raspberry',host='192.168.0.58',database='freddy') File "/usr/lib/python2.7/dist-packages/MySQLdb/init.py", line 81, in Connect return Connection(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 204, in init super(Connection, self).init(*args, **kwargs2) TypeError: 'database' is an invalid keyword argument for this function ....

what should i do? i know my password its fine, but must be somethig else...

3
  • Finish the tour and check in a bit how I reformatted your question to fit the requirements of SO. Commented Jan 29, 2018 at 22:32
  • im using rasbian. Commented Jan 29, 2018 at 22:37
  • If one of the provided answers worked for you please select it as best answer and upvote it. Commented Jan 29, 2018 at 22:45

1 Answer 1

0

The word database should be db and then you should be fine and connect.

Change your loginpart into below code:

cnx = MySQLdb.connect(user   ='phpmyadmin',
                      passwd ='raspberry', 
                      host   ='192.168.0.58',
                      db     ='freddy')

This gives you oversight of used keys and values. Enjoy ;-)

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

24 Comments

i did it, now is showing this error : Traceback (most recent call last): File "test.py", line 2, in <module> cnx = MySQLdb.connect(user='phpmyadmin',passwd='raspberry',host='192.168.0.58',db='freddy') File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect return Connection(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 204, in init super(Connection, self).__init__(*args, **kwargs2) _mysql_exceptions.OperationalError: (2003, 'Can\'t connect to MySQL server on \'192.168.0.58\' (111 "Connection refused")')
That's another question.. did you check if this is posted as a question on SO? The traceback in your comment should be posted as new question if nothing can be found. Or you could have posted it like: Traceback: OperationalError: (2003, 'Can\'t connect to MySQL server on \'192.168.0.58\' (111 "Connection refused")') .
Check if your user/pass credentials are captial sensitive server-side and if the db is named correct as you provided in your posted code. Also, turn on debugging, eventlog or errorlog server-side to see what's going on. This is error is server-side and not Py!
my user name and pass are fine, beacuse i cann acces by the browser , and also if i do mysql -u phpmyadmin -prasberry i can acces to maria db, but the error keep showing.
euhm... db = 'freddy' vs. db = 'maria'... makes sense ;-)
|

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.