简体   繁体   English

Django无法连接到MySQL服务器

[英]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). 我在MacBookPro(El Capitan OS X)上安装了Django版本(1.10),我想将API连接到位于远程服务器(Ubuntu-与我的工作网络相同的网络)上的MySQL数据库。

I get this error when I'm running the Django server : 我在运行Django服务器时收到此错误:

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 : 这是我的项目中的settings.py文件:

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 在我的Mac上,我安装了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. 因此,没有必要隔离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 ;) 非常感谢 ;)

Looks like MySQL is configured to listen only on localhost. 看起来MySQL已配置为仅在localhost上侦听。

You can test this by running telnet 172.30.10.59 3306 from your client machine. 您可以通过从客户端计算机运行telnet 172.30.10.59 3306进行测试。

Please try to follow Configuration step described here : 请尝试按照此处描述的配置步骤进行操作:

Edit the /etc/mysql/my.cnf file to configure MySQL to listen for connections from network hosts, change the bind-address directive to the server's IP address. 编辑/etc/mysql/my.cnf文件,以配置MySQL侦听来自网络主机的连接,将bind-address指令更改为服务器的IP地址。 For example Replace 172.30.10.59 with the appropriate address. 例如,将172.30.10.59替换为适当的地址。 If there are no such entry - uncomment it or create new line. 如果没有这样的条目-取消注释或创建新行。

 bind-address = 172.30.10.59

Then restart mysqld with this command: 然后使用以下命令重启mysqld:

 sudo service mysql restart

Then test with telnet or by running your application once again. 然后使用telnet进行测试,或者再次运行您的应用程序。 Also netstat would have second entry for mysqld. netstat也将有mysqld的第二个条目。

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): 首先,您需要通过编辑my.cnf文件并注释以bind-address开头的行来允许远程连接,如下所示(您也可以通过计算机ip地址更改127.0.0.1 ):

#bind-address                   = 127.0.0.1

Then restart mysql service: 然后重启mysql服务:

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: 要仅在Etat_Civil上授予特权,您可以尝试:

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

The problem may be 'HOST': '172.30.10.58' You can change IP address to your hostname. 问题可能是“主机”:“ 172.30.10.58”您可以将IP地址更改为主机名。

  1. If you are using Ubuntu, check the name in sudo vi /etc/hostname and sudo vi /etc/hosts 如果使用的是Ubuntu,请在sudo vi / etc / hostname和sudo vi / etc / hosts中检查名称
  2. If you are using docker, go to docker 如果您使用的是docker,请转到docker

    docker exec -it CONTAINER_ID bash docker exec -it CONTAINER_ID bash

    sudo vi /etc/hostname and sudo vi /etc/hosts sudo vi / etc / hostname和sudo vi / etc / hosts

Hope this help ! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM