Anaconda3 离线安装和配置 Django-3.2.7 使用 MySQL-5.7 数据库

Django文档 Settings / Core Settings / DATABASES 一节阐述了django与数据库交互配置的内容。

先在 MySQL 5.7 版本数据库中创建一个名为 learning_log_db 的数据库,和名为 myuser 的用户,并分配权限。

create databases learning_log_db;
create user 'myuser' identified by '1qaz@WSX';
grant all on learning_log_db.* to 'myuser'@'%' with grant option;

再在工程目录下settings.py文件里面有关于数据库的配置信息。django要求必须有一个名为 default 的默认数据库配置,这个默认配置可以是空的,可以配置多个数据库。

vim learning_log/settings.py
DATABASES = {
    'default': {                              # default可以空但不能没有
    },
    'learning_log': {                         # 数据配置的名字
        'ENGINE': 'django.db.backends.mysql', # 连接数据库引擎
        'NAME': 'learning_log_db',            # mysql数据库名
        'USER': 'myuser',                     # mysql数据库用户
        'PASSWORD': '1qaz@WSX',               # mysql数据库用户密码
        'HOST': '127.0.0.1',                  # mysql数据库IP地址
        'PORT': '3306',                       # mysql数据库端口
    },
}

为了避免 mysqlclient 模块在 python3 支持不好的情况,直接安装 pymysql 模块。并修改项目目录下 __init__.py 文件。

https://mirror.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/pymysql-1.0.2-py37h06a4308_1.tar.bz2https://mirror.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/pymysql-1.0.2-py37h06a4308_1.tar.bz2

vim learning_log/__init__.py
import pymysql
pymysql.install_as_MySQLdb()

执行 python manage.py migrate --database='learning_log'

python manage.py migrate --database='learning_log'

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苦行僧(csdn)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值