简体   繁体   中英

python: CommandError: You appear not to have the 'mysql' program installed or on your path

I am working on a Mysql database for a Django app, so I created a database named maison, configure settings.py as bellow:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'maison',
    'USER': 'root',
    'PASSWORD': '1234',
    'HOST': '127.0.0.1',
    'PORT': '5432',

}
}

And to verify if my database is connected to my app I run the command python manage.py dbshell But it throws that error :

CommandError: You appear not to have the 'mysql' program installed or on your path.

though I have installed all the requirement:

  • Python: 3.6
  • Django: 1.11
  • MySQL: 5.7
  • mysqlclient

Do you have any idea to fixe that error??

In accordance with the code, Django simply runs console app mysql . Try to call it from console without Django. Maybe it not installed, not in PATH enviroment variable or not yet in PATH enviroment variable (try to rehash command or console relogin if you have rehash command).

class DatabaseClient(BaseDatabaseClient):
executable_name = 'mysql'

@classmethod
def settings_to_cmd_args(cls, settings_dict):
    args = [cls.executable_name]
    db = settings_dict['OPTIONS'].get('db', settings_dict['NAME'])
    user = settings_dict['OPTIONS'].get('user', settings_dict['USER'])
    passwd = settings_dict['OPTIONS'].get('passwd', settings_dict['PASSWORD'])
    host = settings_dict['OPTIONS'].get('host', settings_dict['HOST'])
    port = settings_dict['OPTIONS'].get('port', settings_dict['PORT'])
    cert = settings_dict['OPTIONS'].get('ssl', {}).get('ca')
    defaults_file = settings_dict['OPTIONS'].get('read_default_file')
    # Seems to be no good way to set sql_mode with CLI.

    if defaults_file:
        args += ["--defaults-file=%s" % defaults_file]
    if user:
        args += ["--user=%s" % user]
    if passwd:
        args += ["--password=%s" % passwd]
    if host:
        if '/' in host:
            args += ["--socket=%s" % host]
        else:
            args += ["--host=%s" % host]
    if port:
        args += ["--port=%s" % port]
    if cert:
        args += ["--ssl-ca=%s" % cert]
    if db:
        args += [db]
    return args

def runshell(self):
    args = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)
    subprocess.check_call(args)

mysql-client path may not be added in your machine.

for linux please execute the below command line:

echo 'export PATH=/usr/local/opt/mysql-client/bin:$PATH' >> ~/.bash_profile

If it is a windows system please add the mysql-client path in the environment.

尝试运行python manage.py shell ,它对我python manage.py shell ,我认为它也对你有用!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM