python3.5 django1.10 连接mysql时遇到的
2016-10-22 本文已影响4327人
yayaQAQ
首先是关于启动mysql:
1.在mac下可以用pkg文件直接安装。
2.然后可以在系统偏好设置里面启动。但是你会发现你除了启动mysql其余什么都不能够做。
3.因为你会发现:
屏幕快照 2016-10-22 下午1.10.19.png
4.这意味我们需要进入到mysql的目录中去。(和windows中命令的运行相类似)
/usr/local/mysql/
5.ls:
屏幕快照 2016-10-22 下午1.16.00.png6.进入bin文件夹中,bin中存放的是可以执行的文件。
7.执行
./mysql -u root -p password
可以在输入原来的密码之后更换密码。8.执行
./mysql -u root -p
可以在输入密码后进入数据库中,这些的前提都是你在系统偏好设置里面开启了mysql。9.如果没有开启数据库可以使用
sudo /usr/local/mysql/support-files/mysql.server start
来启动。10.同时关闭数据库用的命令是
sudo /usr/local/mysql/support-files/mysql.server stop
----------------------分割线?喵喵喵。--------------
OK,现在可以在命令行中正确使用mysql了,那么我们需要在我们django框架下的python3项目中正确的使用mysql。
1.mysql和python之间的连接需要一个中间的部分。
2.python3.2开始就不对mysqlDB支持了,但是django的默认连接是mysqlDB,这样我们只能通过其他的方式来连接数据库了。
3.python3.2之后使用PyMySQL连接数据库,当你使用:
pip install PyMySQL
来安装pymysql的时候你会发现,他会告诉你已经安装好了。但我的情况是这个没用。
所以我又重新安装了一遍,怎么安装呢?
下载zip包,命令行cd到解压后的目录,然后输入:
python setup.py install
我是重新安装一遍数据库就可以正常的连接了。。。(:з」∠)
4.然后需要在项目的init.py文件中的插入:
import pymysql
pymysql.install_as_MySQLdb()
不加的话可能会报错,像这样:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10f198378>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 25, in <module>
import MySQLdb as Database
ImportError: No module named 'MySQLdb'
屏幕快照 2016-10-22 下午7.56.47.png
5.当然在settings.py文件也还要插入mysql的配置信息:
屏幕快照 2016-10-22 下午7.41.39.png这个是老生常谈的一个东西了。
然后我们跑一遍:
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x102a6b6a8>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 263, in get_new_connection
conn = Database.connect(**conn_params)
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/connections.py", line 688, in __init__
self.connect()
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/connections.py", line 906, in connect
self._request_authentication()
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/connections.py", line 1114, in _request_authentication
auth_packet = self._read_packet()
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/connections.py", line 981, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1049, "Unknown database 'choosesubject'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python3.5/site-packages/django/core/checks/model_checks.py", line 30, in check_all_models
errors.extend(model.check(**kwargs))
File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 1266, in check
errors.extend(cls._check_fields(**kwargs))
File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 1337, in _check_fields
errors.extend(field.check(**kwargs))
File "/usr/local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 893, in check
errors = super(AutoField, self).check(**kwargs)
File "/usr/local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 208, in check
errors.extend(self._check_backend_specific_checks(**kwargs))
File "/usr/local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 311, in _check_backend_specific_checks
return connections[db].validation.check_field(self, **kwargs)
File "/usr/local/lib/python3.5/site-packages/django/db/backends/mysql/validation.py", line 41, in check_field
field_type = field.db_type(connection)
File "/usr/local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 629, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/usr/local/lib/python3.5/site-packages/django/db/__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/local/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 183, in data_types
if self.features.supports_microsecond_precision:
File "/usr/local/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.5/site-packages/django/db/backends/mysql/features.py", line 54, in supports_microsecond_precision
return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
File "/usr/local/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 367, in mysql_version
with self.temporary_connection() as cursor:
File "/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/contextlib.py", line 59, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 564, in temporary_connection
cursor = self.cursor()
File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.5/site-packages/django/db/backends/mysql/base.py", line 263, in get_new_connection
conn = Database.connect(**conn_params)
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/connections.py", line 688, in __init__
self.connect()
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/connections.py", line 906, in connect
self._request_authentication()
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/connections.py", line 1114, in _request_authentication
auth_packet = self._read_packet()
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/connections.py", line 981, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.5/site-packages/PyMySQL-0.7.9-py3.5.egg/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
django.db.utils.InternalError: (1049, "Unknown database 'choosesubject'")
解决方法来自这里。因为没有创建SCS数据表。。我以为NAME关键字只是用来标识用的,爽快的打出了gg。。
后续遇到问题还会更新。(ಥ_ಥ)
多想能够不更新就好了呢。。。