python3.5 django1.10 连接mysql时遇到的问题以及解决方法

首先是关于启动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.png

6.进入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。。

后续遇到问题还会更新。(ಥ_ಥ)
多想能够不更新就好了呢。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,951评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,606评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,601评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,478评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,565评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,587评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,590评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,337评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,785评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,096评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,273评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,935评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,578评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,199评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,440评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,163评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,133评论 2 352

推荐阅读更多精彩内容