[Django] Django 连接 MySQL数据库 以及 makemigrations&migrate 过程详解总结

安装MySQL数据库

Django自带的是SQLite数据库的, 如果要使用MySQL数据库, 则需要重新安装, 安装教程参考
Centos7安装MySQL8过程详解笔记 (附相关错误解决办法)

安装mysqlclient包

  • python访问mysql数据库 需要第三方包, Django推荐使用mysqlclient.
  • 安装命令pip3 install mysqlclient
    (blog) [root@centos7 testBlog]# yum install mysqlclient
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.aliyun.com
     * epel: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    No package mysqlclient available.
    Error: Nothing to do
    (blog) [root@centos7 testBlog]# pip3 install mysqlclient
    Collecting mysqlclient
      Using cached mysqlclient-1.4.6.tar.gz (85 kB)
        ERROR: Command errored out with exit status 1:
         command: /root/env/blog/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-yl6cmbzb/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-yl6cmbzb/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-yl6cmbzb/mysqlclient/pip-egg-info
             cwd: /tmp/pip-install-yl6cmbzb/mysqlclient/
        Complete output (12 lines):
        /bin/sh: mysql_config: command not found
        /bin/sh: mariadb_config: command not found
        /bin/sh: mysql_config: command not found
        Traceback (most recent call last):
          File "<string>", line 1, in <module>
          File "/tmp/pip-install-yl6cmbzb/mysqlclient/setup.py", line 16, in <module>
            metadata, options = get_config()
          File "/tmp/pip-install-yl6cmbzb/mysqlclient/setup_posix.py", line 61, in get_config
            libs = mysql_config("libs")
          File "/tmp/pip-install-yl6cmbzb/mysqlclient/setup_posix.py", line 29, in mysql_config
            raise EnvironmentError("%s not found" % (_mysql_config_path,))
        OSError: mysql_config not found
        ----------------------------------------
    ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
    
  • 上述报错显示mysql_config变量未找到, 需要安装mysql相关依赖
    yum install mysql-devel
    安装成功后, 继续执行pip3 install mysqlclient
    (blog) [root@centos7 bin]# pip3 install mysqlclient
    Collecting mysqlclient
      Using cached mysqlclient-1.4.6.tar.gz (85 kB)
    Building wheels for collected packages: mysqlclient
      Building wheel for mysqlclient (setup.
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值