python django1.10.4 连接mysql (使用 pymysql)

  1. 1、安装pymysql,因为python3.5没有mysqldb
  2. pip install pymysql

    2、在settings.py中添加下面内容:
  3. import pymysql
    pymysql.install_as_MySQLdb()

  4. 3、将settings里面的DATABASES改成MySQL相关
  5. DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'OPTIONS':{'read_default_file': os.path.join(BASE_DIR, 'my.cnf'),}
        }
    }

  6. 4、在settings的上一级目录中添加 my.cnf 文件,内容如下:
  7. [client]
    database = 'miniblog'
    user = 'root'
    password = '123456'
    default-character-set = utf8

  8. 5进入mysql,查看是否有 miniblog数据库,没有的话需手动创建
  9. mysql -u root -p
    
    show databases;
    create database miniblog;

    6、接下来就可以正常的makemigrations、migrate等了。
  10. python manage.py makemigrations
    python manage.py migrate

    7、文件结构如下:
  11. [willie@localhost miniblog]$ find . -name *~  -exec rm -f {} \;
    [willie@localhost miniblog]$ tree
    .
    ├── account
    │   ├── admin.py
    │   ├── admin.pyc
    │   ├── apps.py
    │   ├── forms.py
    │   ├── forms.pyc
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── migrations
    │   │   ├── 0001_initial.py
    │   │   ├── 0001_initial.pyc
    │   │   ├── __init__.py
    │   │   └── __init__.pyc
    │   ├── models.py
    │   ├── models.pyc
    │   ├── templates
    │   │   ├── account
    │   │   │   ├── account_base.html
    │   │   │   └── register.html
    │   │   └── account_base.html
    │   ├── tests.py
    │   ├── urls.py
    │   ├── urls.pyc
    │   ├── views.py
    │   └── views.pyc
    ├── blog
    │   ├── admin.py
    │   ├── admin.pyc
    │   ├── apps.py
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── migrations
    │   │   ├── __init__.py
    │   │   └── __init__.pyc
    │   ├── models.py
    │   ├── models.pyc
    │   ├── tests.py
    │   ├── views.py
    │   └── views.pyc
    ├── manage.py
    ├── miniblog
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── settings.py
    │   ├── settings.pyc
    │   ├── urls.py
    │   ├── urls.pyc
    │   ├── wsgi.py
    │   └── wsgi.pyc
    ├── my.cnf
    └── templates
        └── index.html
    
    8 directories, 44 files
    


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值