在Django中使用Workbench连接MySQL数据库
我有一个django项目,它连接到一个MySQL数据库,设置如下:
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_db',
'USER': 'username',
'PASSWORD': 'mypassword',
'HOST': '',
'PORT': '',
}
}
当我运行 python manage.py syncdb
时,它会创建数据库,然后在运行 runserver
时,应用程序运行得很好,我也能通过管理面板正确地输入数据到模型中。
但是,当我尝试使用MySQL Workbench连接到数据库时,它给我报了一个错误:Can't connect to MySQL server on '127.0.0.1' (111)
。
我创建数据库的方式如下:
Grant all on my_db.* to 'username'@'localhost' identified by 'mypassword';
flush privileges;
为什么Workbench会给我这个错误,尽管服务器运行得很好呢?提前谢谢!
编辑:我用了“pythong”这个词。
2 个回答
0
可能你的MySQL服务器只在本地监听。也就是说,它只接受来自自己电脑的连接。
在你的 my.cnf
文件里,把 bind-address
这一行前面加上#,这样就可以把它注释掉了。
#bind-address = 127.0.0.1
然后重启你的MySQL服务器,看看能不能用Workbench连接上。
1
我也遇到过类似的问题,但通过更新配置文件中的#port和#host解决了。
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.
}
}