gpt4 book ai didi

python - 无法使用 django 框架连接 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 17:17:58 25 4
gpt4 key购买 nike

我正在尝试使用 django 框架连接 mysql 数据库,但它没有连接。

设置:安装xampp、安装django、安装mysqlclient

使用 phpmyadmin 创建数据库,数据库名称为 polls

设置.py:

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

运行 python manage.py migrate 时出错:

Traceback (most recent call last):
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
return Database.connect(**conn_params)
File "/home/akshay/.local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect
return Connection(*args, **kwargs)
File "/home/akshay/.local/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/akshay/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/akshay/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/akshay/.local/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/akshay/.local/lib/python3.6/site-packages/django/core/management/base.py", line 332, in execute
self.check()
File "/home/akshay/.local/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/home/akshay/.local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 57, in _run_checks
issues = run_checks(tags=[Tags.database])
File "/home/akshay/.local/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/akshay/.local/lib/python3.6/site-packages/django/core/checks/database.py", line 10, in check_database_backends
issues.extend(conn.validation.check(**kwargs))
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 9, in check
issues.extend(self._check_sql_mode(**kwargs))
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
with self.connection.cursor() as cursor:
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
return self._cursor()
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
self.ensure_connection()
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/akshay/.local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection
return Database.connect(**conn_params)
File "/home/akshay/.local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect
return Connection(*args, **kwargs)
File "/home/akshay/.local/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

最佳答案

卸载 XAMPP。安装了 LAMP 堆栈。

sudo apt-get install apache2
sudo apt-get install php"version"
sudo apt-get install mysql-server
sudo apt-get install phpmyadmin

它开始工作了。

关于python - 无法使用 django 框架连接 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51437447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com