Andromedae93 Andromedae93 - 10 days ago 6
MySQL Question

Django unable to connect to MySQL server

I have a Django version (1.10) which is installed on my MacBookPro (El Capitan OS X) and I would like to connect the API to my MySQL Database which is located on a distant server (Ubuntu - same network as my job network).

I get this error when I'm running the Django server :

django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '172.30.10.58' (61)")


This is my
settings.py
file from my project :

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Etat_Civil',
'USER': 'root',
'PASSWORD': 'password',
'HOST': '172.30.10.58',
'PORT': '3306',
}
}


On my Mac I installed
mysqlclient
/
mysql connector/python


I don't work with a virtual environment because I just have one project in my laptop. So it's not necessary to isolate Python.

I think that's maybe a permission problem but I don't really know How configure that. I found some tutorials but I get problems each time.

Do you have some ideas ?

Thank you so much ;)

Answer

Firstly, you need to allow remote connections by editing my.cnf file and commenting the line that starts with bind-address as follows (You may also just change 127.0.0.1by your machine ip address):

#bind-address                   = 127.0.0.1

Then restart mysql service:

sudo service mysql restart

Grant privileges to your user as follows:

mysql> CREATE USER 'root'@'172.30.10.%' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.30.10.%'

To grant privileges only on Etat_Civil, you can try:

mysql> GRANT ALL PRIVILEGES ON Etat_Civil.* TO 'root'@'172.30.10.%'
Comments