Connecting to MySQL database in django using workbench

依然范特西╮ 提交于 2021-02-08 04:36:31

问题


I've got a django project connected to a MySQL database as follows:

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my_db',
        'USER': 'username',
        'PASSWORD': 'mypassword',
        'HOST': '',
        'PORT': '',
    }
}

on running python manage.py syncdb, it creates the database and on runserver the application runs perfectly and I'm able to input data into the models using the admin panel correctly.

However, on trying to connect to the database using MySQL Workbench, it gives me Can't connect to MySQL server on '127.0.0.1' (111) error.

I had created the database as follows:

Grant all on my_db.* to 'username'@'localhost' identified by 'mypassword';
flush privileges;

Why would Workbench show me that error even though the server is running correctly? Thanks in advance!

Edit: Used the word pythong.


回答1:


I had similar issue but it got resolved by updating #port and #host into configuration file.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'vres',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '3306',                      # Set to empty string for default.
    }
}



回答2:


It could be that your MySQL server is only listening to localhost. In your my.cnf file, comment out bind-address.

#bind-address = 127.0.0.1

Restart your MySQL server and see if you can connect with Workbench.



来源:https://stackoverflow.com/questions/23482185/connecting-to-mysql-database-in-django-using-workbench

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!