5

I'm trying to access the database using python but it's just not working. When I access a remote database everything is fine. But local there is some kind of problem. What I'm doing wroing here? I've tryied some things that I found on intener do grant access but they did not work

Local users: enter image description here

Code used:

enter image description here

Message recieved

enter image description here

Thanks for the help

Xidh
  • 582
  • 1
  • 5
  • 19
  • Have you read the [related question](http://stackoverflow.com/questions/10299148/mysql-error-1045-28000-access-denied-for-user-billlocalhost-using-passw)? - The given answer seems to apply to your problem as well (If I'm interpreting your phpmyadmin Screenshot correctly) – Maurice Oct 24 '16 at 11:39
  • Sorry! Confused!! Is the database you are trying to access (local i.e. on the same machine as the code) or as you suggest (remote i.e. on another machine)???? – RiggsFolly Oct 24 '16 at 11:45
  • _Side Note_ Granting `All Privilages` to this user seems like OverKill. Grant only what the app requires and only on the databases that the app requires – RiggsFolly Oct 24 '16 at 11:48
  • I've tryied to edit the users.. I excluded the anonymous users and changed the host to '127.0.0.1', 'localhost', '%', '192.168.0.116'(my local ip) but it does't work.. Every thins is local here. I remove All Privileges and let only "data" commands. – Xidh Oct 24 '16 at 12:25
  • Also.. This is MAMP's MySQL – Xidh Oct 24 '16 at 12:45

4 Answers4

4

After hours of testing I finally found out what was the problem! After some alterations in the mysql users and no results everything work just fine when I added the PORT to mysql database!

db = MySQLdb.connect(host="127.0.0.1",user="userpython",passwd="root",db="bigdata",port=8886)
Xidh
  • 582
  • 1
  • 5
  • 19
1

Adding a port number did not work for me. When granted privileges to os user which runs the python script worked.

sudo mysql -u root -h localhost -p
grant all privileges on database_name.* to 'os_user'@'localhost' identified by 
'password' with grant option;
zKaj
  • 9
  • 4
0

The port number is missing in the MySQLdb.connect() function.

Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34
-1

Make sure you have added all the parameters correctly, confirm valid username and password. Example below:

db = MySQLdb.connect(
host="localhost",
user="username",
password="password",
database="database_name",
)