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...

Daniel
  • 2,744
  • 1
  • 31
  • 41

1 Answers1

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 ;-)

ZF007
  • 3,708
  • 8
  • 29
  • 48
  • i did it, now is showing this error : Traceback (most recent call last): File "test.py", line 2, in 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")') – Freddy Alex Saavedra Jan 29 '18 at 22:46
  • 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")') `. – ZF007 Jan 29 '18 at 22:49
  • 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! – ZF007 Jan 29 '18 at 22:52
  • 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. – Freddy Alex Saavedra Jan 29 '18 at 22:56
  • euhm... `db = 'freddy'` vs. `db = 'maria'`... makes sense ;-) – ZF007 Jan 29 '18 at 22:58
  • Check also `MySQLdb` db-settings if the are set as explained [here](https://stackoverflow.com/a/1420862/8928024). Don't forget to reboot afterwards. – ZF007 Jan 29 '18 at 23:02
  • i tried both... db = Maria and db = MariaDB, not working either, btw when i want to install "sudo apt-get install python-mysql" showing this: E: Unable to locate package python-mysql – Freddy Alex Saavedra Jan 29 '18 at 23:04
  • try at the command-line `pip install ` but do first `pip search mysql` to get a list with packages and select correct packagename. – ZF007 Jan 29 '18 at 23:06
  • i tried pip install python-mysql and this is showing me Could not find a version that satisfies the requirement python-mysql (from ver sions: ) No matching distribution found for python-mysql – Freddy Alex Saavedra Jan 29 '18 at 23:11
  • its $sudo apt-get install python-mysqldb for python 2.7. For python3 $sudo apt-get install python3-mysqldb. Or pip equivalents but search with the word `mysqldb`. My typo to miss-out on the 'db' letters. – ZF007 Jan 29 '18 at 23:20
  • you can also try `apt-cache search mysql|grep python` to find the correct wording this way.. – ZF007 Jan 29 '18 at 23:25
  • keep showing me the error 111 like before, i dont know what more to do. i've installed all you said and nothing mate... – Freddy Alex Saavedra Jan 29 '18 at 23:35
  • did you check the settings of the db in MySQL database and if you made changes... did you reboot? – ZF007 Jan 29 '18 at 23:37
  • uhmm i dont know what i can change in the db settings to be honest, also i dont know how to acces there. – Freddy Alex Saavedra Jan 29 '18 at 23:40
  • Did you read Pascal Martin his answer about the MySql server? Find my.cnf file like [here](https://stackoverflow.com/a/2485758/8928024) is described and modify it accordingly. – ZF007 Jan 29 '18 at 23:41
  • Ranaivo shows [here](https://stackoverflow.com/a/22529285/8928024) where you have to write the code. – ZF007 Jan 29 '18 at 23:47
  • yes i saw it, everything its commented exept by those 2 lines: !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mariadb.conf.d/ – Freddy Alex Saavedra Jan 29 '18 at 23:48
  • Raaivo post, his my.cnf lines are really differents than mine i got less than his. – Freddy Alex Saavedra Jan 29 '18 at 23:51
  • No problem if you have less. Different setup... ;-) Use '#' to disable `!includedir /etc/mysql/conf.d/` and `!includedir /etc/mysql/mariadb.conf.d/`. Then it should work... – ZF007 Jan 29 '18 at 23:51
  • still the same error dear friend.. everything its commeted in my "my.cnf" documented... – Freddy Alex Saavedra Jan 29 '18 at 23:54
  • can i give you my teamviewer id? so you can check it really quickly if u dont mind... dear friend – Freddy Alex Saavedra Jan 29 '18 at 23:55
  • its better to post it as a separate question on SO for the experts. I stretched my knowledge here on what I ever tried to get a mysqldb inlog working again. Got an arduino around but no berry atm. Otherwise I could go through the process with you... Last shot... have you tried login as admin or root? This could reset some privilages on the server-side as I recall. – ZF007 Jan 30 '18 at 00:00
  • dammmmm man...... its working now!!!! you really helped me man!!! thanks so much for your time!!!! its been hard this for me. but now works .... thanks, i follow Ranaivo post. – Freddy Alex Saavedra Jan 30 '18 at 00:04
  • Wohooo... great its working... don't forget to feed the fish "usefull answer" ;-) – ZF007 Jan 30 '18 at 00:05