MySQL Shell連接資料庫報MySQL Error 1045 (28000)錯誤淺析

来源:https://www.cnblogs.com/kerrycode/archive/2023/11/15/17833536.html
-Advertisement-
Play Games

這裡簡單總結一下mysql shell訪問資料庫時報MySQL Error 1045 (28000): Access denied for user 'root'@'::1' (using password: YES)的原因以及如何解決這個問題 這裡測試的環境為MySQL 8.0.35,我們先來看看 ...


這裡簡單總結一下mysql shell訪問資料庫時報MySQL Error 1045 (28000): Access denied for user 'root'@'::1' (using password: YES)的原因以及如何解決這個問題

這裡測試的環境為MySQL 8.0.35,我們先來看看報錯案例:

$ mysqlsh -h localhost -P 7306 -u root -p
Please provide the password for 'root@localhost:7306': ***********
MySQL Shell 8.0.35

Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help'\quit' to exit.
Creating a session to 'root@localhost:7306'
MySQL Error 1045 (28000): Access denied for user 'root'@'::1' (using password: YES)

先用root賬號連接數據(socket方式),檢查用戶信息,如下所示,root賬號限定為localhost

$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 8.0.35 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
13 rows in set (0.00 sec)

mysql> 

然後,檢查變數skip_name_resolve,如下所示,skip_name_resolve為ON

mysql> show variables like 'skip_name_resolve';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| skip_name_resolve | ON   |
+-------------------+-------+
1 row in set (0.01 sec)

mysql>

資料庫參數skip_name_resolve設置為ON,它是禁止功能變數名稱解析的,一般都推薦將這個參數設置為ON, 因此伺服器不會嘗試解析連接客戶端的名稱或每次都在主機名緩存中查找它們(甚至localhost也會被解析/搜索),根據官方文檔的解釋,它會限制@localhost的連接。

官方文檔的詳細解釋:

Depending on the network configuration of your system and the Host values for your accounts, clients may need to connect using 
an explicit --host option, such as --host=127.0.0.1 or --host=::1.

An attempt to connect to the host 127.0.0.1 normally resolves to the localhost account. However, this fails if the server is run
with skip_name_resolve enabled. If you plan to do that, make sure an account exists that can accept a connection. For example,
to be able to connect as root using --host=127.0.0.1 or --host=::1, create these accounts:

CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root-password';
CREATE USER 'root'@'::1' IDENTIFIED BY 'root-password';

那麼怎麼解決這個問題呢?一共有下麵幾種方法。

方案1:skip_name_resolve設置為OFF

我們需要在參數文件my.cnf中 將參數skip-name-resolve註釋或者設置skip_name_resolve設置為OFF的.

註意事項,雖然官方文檔中,參數skip-name-resolve是Boolean類型,但是如果你像下麵這樣設置是不會生效的,具體原因不是很清楚

skip-name-resolve=0 

skip-name-resolve=FALSE

正確的做法

方法1:
skip-name-resolve=OFF

方法2:
#skip-name-resolve  註釋掉參數

修改skip_name_resolve的值為OFF後,重啟一下MySQL實例,然後我們驗證一下測試結果。

mysql> show variables like 'skip_name_resolve';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| skip_name_resolve | OFF   |
+-------------------+-------+
1 row in set (0.01 sec)

mysql>
$ mysqlsh -h localhost -P 7306 -u root -p
MySQL Shell 8.0.35

Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help'\quit' to exit.
Creating a session to 'root@localhost:7306'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 10
Server version: 8.0.35 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
 MySQL  localhost:7306 ssl  JS > 

這種方案需要修改參數,需要重啟MySQL實例,所以一般來說,不建議使用。

方案2:新增賬號

如下所示,我們新增下麵賬號

CREATE USER 'root'@'::1' IDENTIFIED BY '********';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'::1';
FLUSH PRIVILEGES;

當然,如果報錯為MySQL Error 1045 (28000): Access denied for user 'root'@'127.0.0.1' (using password: YES)的話,那麼可以創建下麵用戶

CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY '**********';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';
FLUSH PRIVILEGES;

創建賬號後,mysqlsh就可以連接,不會報上面錯誤了,如下所示:

$ mysqlsh -h localhost -P 7306 -u root -p
Please provide the password for 'root@localhost:7306': ***********
Save password for 'root@localhost:7306'? [Y]es/[N]o/Ne[v]er (default No): y
MySQL Shell 8.0.35

Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help'\quit' to exit.
Creating a session to 'root@localhost:7306'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 20
Server version: 8.0.35 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
 MySQL  localhost:7306 ssl  JS > 

方案3:使用socket方式連接

mysql shell也可以使用socket連接,一般的方式如下:

mysqlsh -h localhost  -u root -p -S /tmp/mysql.sock  #根據實際情況填寫具體的mysql.sock文件

\connect root@localhost?socket=(/tmp/mysql.sock)

測試驗證如下所示:

$ mysqlsh -h localhost  -u root -p -S /tmp/mysql.sock
Please provide the password for 'root@/tmp%2Fmysql.sock': ***********
Save password for 'root@/tmp%2Fmysql.sock'? [Y]es/[N]o/Ne[v]er (default No): y
MySQL Shell 8.0.35

Copyright (c) 2016, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help'\quit' to exit.
Creating a session to 'root@/tmp%2Fmysql.sock'
Fetching schema names for auto-completion... Press ^C to stop.
Your MySQL connection id is 22
Server version: 8.0.35 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.
 MySQL  localhost  JS > 
掃描上面二維碼關註我 如果你真心覺得文章寫得不錯,而且對你有所幫助,那就不妨幫忙“推薦"一下,您的“推薦”和”打賞“將是我最大的寫作動力! 本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接.
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 來源:blog.csdn.net/weixin_42653522/article/details/117151913 1、前言 ApplicationContext 中的事件處理是通過 ApplicationEvent 類和 ApplicationListener 介面提供的。如果將實現了 Appl ...
  • 學習視頻:【孫哥說Spring5:從設計模式到基本應用到應用級底層分析,一次深入淺出的Spring全探索。學不會Spring?只因你未遇見孫哥】 第二章、第一個Spring程式 1.軟體版本 1.JDK1.8+ 2.Maven3.5+ 3.IDEA2018+ 4.SpringFramework 5. ...
  • 目錄1.修飾類時2.修飾方法時3.修飾屬性和局部變數時3.1修飾局部變數時3.2修飾成員變數時3.3修飾類變數時4.final與普通變數的區別5.final用於引用 1.修飾類時 1.只能是公共的(public)就算不寫也是public 2.被final修飾的類不可以被繼承 //前面預設有個publ ...
  • SciPy庫本身是針對科學計算而不是圖像處理的,只是圖像處理也包含了很多數學計算,所以Scipy也提供了一個專門的模塊ndimage用於圖像處理。 ndimage模塊提供的功能包括輸入/輸出圖像、顯示圖像、基本操作(如裁剪、翻轉、旋轉等)、圖像過濾(如去噪、銳化等)、圖像分割、分類、特征提取以及註冊 ...
  • 目錄 Welcome to YARP - 1.認識YARP並搭建反向代理服務 Welcome to YARP - 2.配置功能 2.1 - 配置文件(Configuration Files) 2.2 - 配置提供者(Configuration Providers) 2.3 - 配置過濾器(Confi ...
  • 一:背景 1. 講故事 前幾個月有位朋友找到我,說他們的的web程式沒有響應了,而且監控發現線程數特別高,記憶體也特別大,讓我幫忙看一下怎麼回事,現在回過頭來幾經波折,回味價值太濃了。 二:程式到底經歷了什麼 1. 線上程上找原因 這個程式記憶體高,線程高,無響應,尼瑪是一個複合態問題,那怎麼入手呢?按 ...
  • 前言: 之前對於項目上播放大解析度視頻(特別是大於4k解析度的)常常會感覺相當的頭疼,最開始使用的是Unity自帶的VideoPlayer,發現效果並不理想,更換為AVPro後發現播放是流暢了 但不能操作視頻快進,只要一快進就會出現卡頓,最後偶然間發現了一款用於播放Hap格式視頻的插件才最終解決了這 ...
  • 1.HighLightingSystem 用於3D物體高亮顯示 在項目中的使用:導入插件後在需要高亮顯示的3d物體上附加Highlighter組件,在需要顯示高亮效果的攝像機上附加Highlighting Renderer組件。在代碼中調整Highlighter屬性即可控制物體高亮效果的開關、閃爍。 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...