Django无法连接到MySQL服务器 [英] Django unable to connect to MySQL server
问题描述
我在MacBookPro(El Capitan OS X)上安装了Django版本(1.10),我想将API连接到位于远程服务器(Ubuntu-与我的工作所在的网络相同)上的MySQL数据库网络).
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).
我在运行Django服务器时收到此错误:
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)")
这是我的项目中的settings.py
文件:
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',
}
}
在我的Mac上,我安装了mysqlclient
/mysql connector/python
On my Mac I installed mysqlclient
/ mysql connector/python
我不使用虚拟环境,因为我的笔记本电脑中只有一个项目.因此,没有必要隔离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.
您有什么想法吗?
非常感谢;)
推荐答案
首先,您需要通过编辑my.cnf
文件并注释以bind-address
开头的行来允许远程连接,如下所示(您也可以更改127.0.0.1
通过您的计算机的IP地址):
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.1
by your machine ip address):
#bind-address = 127.0.0.1
然后重新启动mysql服务:
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.%'
要仅在Etat_Civil
上授予特权,您可以尝试:
To grant privileges only on Etat_Civil
, you can try:
mysql> GRANT ALL PRIVILEGES ON Etat_Civil.* TO 'root'@'172.30.10.%'
这篇关于Django无法连接到MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!