Using MySQL with Django - Access denied for user '@'localhost

后端 未结 7 2137
臣服心动
臣服心动 2020-12-07 17:46

So I\'m learning Django (1, 3, 1, \'final\', 0) through this resource: http://www.djangobook.com/en/2.0/chapter05/

I installed \'mysql-server\' and \'python-mysqldb\

相关标签:
7条回答
  • 2020-12-07 18:49

    Your user does not have an access to database. Use the commands below to set up your database.

    DROP DATABASE IF EXISTS `mydb`;
    CREATE DATABASE `mydb`
        DEFAULT CHARACTER SET utf8
        DEFAULT COLLATE utf8_general_ci;
    
    USE 'mysql';
    GRANT ALL PRIVILEGES ON mydb.* TO 'mydb_user'@'localhost' IDENTIFIED BY 'your_password'
    
    WITH GRANT OPTION;
    FLUSH PRIVILEGES;
    

    Also, you need to have enough privileges to run it. Save it as script.sql then,

    $mysql -u root -p < script.sql
    

    Than on to settings.py where you need to make sure your db settings are set up properly

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'mydb',                  
            'USER': 'mydb_user',             
            'PASSWORD': 'your_password',                  
            'HOST': '',                     
            'PORT': '',                      
        }
    }
    

    and

    python manage.py syncdb
    

    and you're done.

    0 讨论(0)
提交回复
热议问题