(2059,“身份验证插件'caching_sha2_password'")在Django上运行与MYSQL数据库连接的服务器时
我想配置django项目,以便将其与使用 workbench 8.0 创建的 MYSQL 数据库连接,
然后我想通过运行
I want to configure my django project in order to connect it with database in MYSQL I created with workbench 8.0,
and then I want to run the server by running
python manage.py runserver
从anaconda命令提示符下,
这样我就可以使用Django界面来可视化和更改数据.
请注意,我不想降级Workbench 8.0.
这些是我已执行的步骤:
在anaconda提示符下:
from anaconda command prompt,
so that I can use the Django interface to visualize and alter data.
Please note that I don’t want to downgrade workbench 8.0.
These are the steps I have made:
From anaconda prompt:
pip install mysqlclient
在我的项目文件夹的settings.py中
In my project folder, in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'schema_meta',
'USER': 'root',
'PASSWORD': '<mypassword>',
'HOST': '127.0.0.1',
'PORT': '3306',
},
}
在mysql服务器目录中,我打开cnf.ini并插入[client]部分:
Inside the directory of mysql server, I open cnf.ini and insert a the [client] section:
[client]
database=schema_meta
host=127.0.0.1
user=root
password=<mypassword>
port=3306
default-character-set = utf8
然后从anaconda提示我运行
Then from anaconda prompt I run
Python manage.py runserver
我得到了错误
django.db.utils.OperationalError :( 2059,身份验证插件 无法加载"caching_sha2_password":不可能的行为 模数比.\ r \ n)
django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: Impossibile trovare il modulo specificato.\r\n")
So I try to solve it by following this thread:
django.db.utils.operationalError: (2059,"Authentication Plugin 'caching_sha2_password'")
我打开mysql工作台并运行以下查询:
I open mysql workbench and I run this query:
delete from mysql.user
where user='root'
and host = '127.0.0.1';
flush privileges;
CREATE USER 'root'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY '<mypassword>';
然后,在my.ini文件中更改
And then, in the my.ini file I change
default-authentication-plugin= caching_sha2_password
使用
default-authentication-plugin=mysql_native_password
最后从anaconda提示符下:
Finally from anaconda prompt:
python manage.py runserver
但我再次得到
django.db.utils.OperationalError :( 2059,身份验证插件 无法加载"caching_sha2_password":不可能的行为 模数比.\ r \ n)
django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: Impossibile trovare il modulo specificato.\r\n")
现在,怎么了?为什么它没有将更改保存到身份验证方法中?
Now, what’s wrong? Why it did not get the changes into the authentication method?
为了检查是否没有其他错误,从mysql工作台,从第一个主页"视图中,我右键单击我的数据库,打开编辑连接",然后单击测试连接",然后软件说连接成功.
In order to check that there are no other errors, from mysql workbench, from first "home" view, I right click on my database, I open "edit connection", I click on "test connection", and the software says that the connection is successfull.
此外,我想检查问题是否出在我的Django设置中. 所以从anaconda提示符下我运行
Moreover, I wanted to check if the problem was in my Django settings. So from anaconda prompt I run
pip install pymysql
然后在项目文件夹中创建一个"connect_to_mysql.py"脚本,其中包含以下代码:
Then in the project folders I created a "connect_to_mysql.py" script, with the following code inside:
import pymysql.cursors
mydb = pymysql.connect(host='127.0.0.1',
user='root',
password='<mypassword>',
db='schema_meta',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
print(mydb)
这似乎很好用,因为当我运行
and this seems to work fine, since when I run
connect_to_mysql.py
我从蟒蛇那里得到
pymysql.connections.Connection对象位于0x000002013F2851D0
pymysql.connections.Connection object at 0x000002013F2851D0
我想这意味着连接成功建立".
为了确保问题出在mysql(我想是mysql连接器)中,我创建了一个文件"connect_to_mysql_2.py",其中包含以下代码:
That I guess it means "connection successfully established".
And just to be sure that the problem is into mysql (mysql connector I guess), I create a file "connect_to_mysql_2.py" with this code inside:
import mysql.connector
mydb = mysql.connector.connect(user='root', password='<mypassword>',
host='127.0.0.1', database='meta_schema')
print(mydb)
当我从anaconda运行它时,我再次得到
And when I run it from anaconda, again I get
不支持身份验证插件'{0}'".format(plugin_name)) mysql.connector.errors.NotSupportedError:身份验证插件 不支持"caching_sha2_password"
"Authentication plugin '{0}' is not supported".format(plugin_name)) mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported
这意味着我无法通过在mysql工作台和my.ini文件中进行任何操作来解决问题.
如何使Django与mysql数据库连接并运行服务器?
有没有一种方法可以使用pymysql连接器代替mysql连接器来建立服务器连接器?
That means that I fixed nothing by working on mysql workbench and in my.ini file.
How can I get my Django connected with my mysql database and my server running?
Is there a way to establish the server connector using pymysql connector instead that mysql connector?
我想我解决了.
通过此线程
https://*.com/a/21740692/7658051
在settings.py中的DATABASES条目中,我替换了
In settings.py, at the DATABASES entry, I substituted
'ENGINE': 'django.db.backends.mysql'
使用
'ENGINE': 'mysql.connector.django',
再次,如此处指定,
https://django-mysql .readthedocs.io/en/latest/checks.html#django-mysql-w003-utf8mb4
我也添加了
'OPTIONS': {
# Tell MySQLdb to connect with 'utf8mb4' character set
'charset': 'utf8mb4',
},
# Tell Django to build the test database with the 'utf8mb4' character set
'TEST': {
'CHARSET': 'utf8mb4',
'COLLATION': 'utf8mb4_unicode_ci',
}
然后如果我运行
python manage.py runserver
python manage.py runserver
即使我无法访问 http://127.0.0.1:8000/admin ,
It seems to work, even if I cannot access http://127.0.0.1:8000/admin , because I am connected to a legacy database, so I still have to inspect db and stuff I still have to learn.
作为第二个证明,当我运行时,该连接现在可以正常工作
As second proof that the connection is now working, when I run
connect_to_mysql_2.py
(在Django上运行与MYSQL数据库连接的服务器时,请参见(2059,身份验证插件'caching_sha2_password'"))
(See (2059,"Authentication Plugin 'caching_sha2_password'") when running server connected with MYSQL database on Django)
我终于明白了
mysql.connector.connection_cext.CMySQLConnection对象位于0x000001DFB7521550
mysql.connector.connection_cext.CMySQLConnection object at 0x000001DFB7521550
所以我真的认为这次连接已经打开.
So I really think this time the connection is on.