Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

python - Can I use MySQL on Django(dev 1.6.x) with Python3.x?

I use Django dev(1.6.x) from git repo and I want to use MySQL , But on the settings.py file can not setup MySQL because MySQL is not supported in python3 and Django, So I used pymysql package on python3.x without any problem but in Django can not setup that on settings.py too.

Can I use mysql(or pymysql or ?) on django with python3 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I also struggled with making MySQL work with Django 1.6 and Python 3.3; the only thing that worked was to switch to PyMySQL. See my post on that here

Adding the answer below

My environment: OSX 10.9, Python 3.3.3, Django 1.6.1, PyMySQL 0.6.1, MySQL Server 5.5 on Windows

How to make it work:

  1. Install PyMySQL version 0.6.1 (https://github.com/PyMySQL/PyMySQL/): you can install it either by using pip, i.e. : pip install PyMySQL or by manually downloading the package; there is a good documentation on their website on how to do that.

  2. Open your Django App __init__.py and paste the following lines:

    import pymysql
    pymysql.install_as_MySQLdb() 
    
  3. Now, open settings.py and make sure your DATABASE property looks like this:

    DATABASES = {
       'default': {
           'ENGINE': 'django.db.backends.mysql',
           'NAME': 'mydb',
           'USER': 'dbuser',
           'PASSWORD': 'dbpassword',
           'HOST': 'dbhost',
           'PORT': '3306'
        }
    }
    
  4. That's it, you should be able to execute python manage.py syncdb to init your MySQL DB; see the sample output below:

    Creating tables ...
    Creating table django_admin_log
    Creating table auth_permission
    Creating table auth_group_permissions
    ...
    ...
    Creating table socialaccount_socialtoken
    
    You just installed Django's auth system, which means you don't have any superusers defined.
    ...
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...