python manage.py dbshell
CommandError: You appear not to have the 'mysql' program installed or on your path.
报错信息如上,我是想用django的命令直接从应用端连数据库查询和操作数据,但在执行命令后发现不可用。但是我执行shell命令通过ORM方式确实可以的连库操作。
python manage.py shell
Python 2.7.5 (default, Apr 11 2018, 07:36:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> User.objects.all().count()
15
所以,这两个命令连库所使用的方式和程序是不同的,shell 是依赖django自己的包,dbshell是依赖另一个包,下面介绍,有点扯远了,收。
cat /etc/redhat-release # 我服务器的操作系统
CentOS Linux release 7.9.2009 (Core)
which mysql # 我的操作系统里没有安装mysql client
/usr/bin/which: no mysql in (/sbin:/bin:/usr/sbin:/usr/bin)
我的解决方法就是:
安装一个包 mysql-community-client-5.7.28-1.el7.x86_64.rpm ,系统版本不同,包的版本也不同,可以在这里下载对应的包,下载链接在页面的下面“Download” 部分。
安装rpm包
rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm --force --nodeps
这里为什么要强制安装呢? 因为不强制安装,这个client还会依赖
mysql-community-common-5.7.28-1.el7.x86_64.rpm
mysql-community-libs-5.7.28-1.el7.x86_64.rpm
依赖是一个很麻烦的事,索性就不安装这两个依赖包了,直接强制安装。
rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm --force --nodeps
warning: mysql-community-client-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-client-5.7.28-1.e################################# [100%]
安装成功之后就可以用了,测试一下吧。
which mysql
/bin/mysql # 已经可以找到启动文件了
python manage.py dbshell # 连库
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3636195924
Server version: 5.7.32-log Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> # 链接库成功,撸sql去吧
附:如果是Ubuntu 系统并且可联网,请使用下面的命令安装
apt-get install mysql-client