首頁 運維 linux運維 解決linux下mysql密碼錯誤的問題

解決linux下mysql密碼錯誤的問題

Jun 06, 2020 pm 04:01 PM
linux

解決linux下mysql密碼錯誤的問題

linux下mysql密碼錯誤的解決方法:

#前言

今天在伺服器安裝mysql之後,登入發現密碼錯誤,但是我沒有設定密碼呀,最後百度之後得知,mysql在5.7版本之後會自動建立初始密碼。

報錯如下:

[root@mytestlnx02 ~]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
登入後複製

修改密碼

1. 檢查mysql服務是否啟動,如果啟動,關閉mysql服務

//查看mysql服务状态
[root@mytestlnx02 ~]# ps -ef | grep -i mysql
root     22972     1  0 14:18 pts/0    00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql    23166 22972  0 14:18 pts/0    00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root     23237 21825  0 14:22 pts/0    00:00:00 grep -i mysql
//关闭服务
[root@mytestlnx02 ~]# service mysql stop
[root@mytestlnx02 ~]#
登入後複製

2. 修改mysql的設定檔my.cnf

my.cnf設定檔的位置,一般在/etc/my.cnf,有些版本在/etc/mysql/my.cnf

在設定檔中,增加2行程式碼

[mysqld]
skip-grant-tables
登入後複製

作用是登入mysql的時候跳過密碼驗證

然後啟動mysql服務,並進入mysql

[root@mytestlnx02 ~]# service mysqld start
[root@mytestlnx02 ~]#
[root@mytestlnx02 ~]# mysql -u root 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
登入後複製

3. 修改密碼

連接mysql這個資料庫,修改使用者密碼

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update mysql.user set authentication_string=password('root_password') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> exit
登入後複製

4. 重啟mysql服務

先將先前加在設定檔裡面的2句程式碼註解或刪除掉,然後重新啟動mysql服務,就可以使用剛剛設定的密碼登入了。

[root@mytestlnx02 ~]# service mysql start
[root@mytestlnx02 ~]#
[root@mytestlnx02 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
p.s.
登入後複製

在CentOS上的操作方式有所不同。

執行修改密碼的指令一直報錯

mysql> update user set authentication_string=password('xxxxxxxx') where User='root';       
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('root_password') where User='root'' at line 1
登入後複製

不可能是語法問題,檢查了很多遍,最後發現CentOS下應該這樣操作:

查看初始密碼

[root@VM_0_8_centos ~]# grep 'temporary password' /var/log/mysqld.log
2018-09-26T04:25:54.927944Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: DN34N/=?aIfZ
登入後複製

可以看到初始密碼為DN34N/=?aIfZ

使用初始密碼登入

[root@VM_0_8_centos ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.12 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
登入後複製

修改密碼

mysql> ALTER USER 'root' IDENTIFIED BY 'xxxxxxxxx';  
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxxxx';
Query OK, 0 rows affected (0.11 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye
重启服务
登入後複製

就生效了

[root@VM_0_8_centos ~]# service mysqld stop 
Redirecting to /bin/systemctl stop  mysqld.service
[root@VM_0_8_centos ~]# service mysqld start
Redirecting to /bin/systemctl start  mysqld.service
登入後複製

推薦教學: 《linux教學

#

以上是解決linux下mysql密碼錯誤的問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Linux實際上有什麼好處? Linux實際上有什麼好處? Apr 12, 2025 am 12:20 AM

Linux適用於服務器、開發環境和嵌入式系統。 1.作為服務器操作系統,Linux穩定高效,常用於部署高並發應用。 2.作為開發環境,Linux提供高效的命令行工具和包管理系統,提升開發效率。 3.在嵌入式系統中,Linux輕量且可定制,適合資源有限的環境。

將Docker與Linux一起使用:綜合指南 將Docker與Linux一起使用:綜合指南 Apr 12, 2025 am 12:07 AM

在Linux上使用Docker可以提高開發和部署效率。 1.安裝Docker:使用腳本在Ubuntu上安裝Docker。 2.驗證安裝:運行sudodockerrunhello-world。 3.基本用法:創建Nginx容器dockerrun--namemy-nginx-p8080:80-dnginx。 4.高級用法:創建自定義鏡像,使用Dockerfile構建並運行。 5.優化與最佳實踐:使用多階段構建和DockerCompose,遵循編寫Dockerfile的最佳實踐。

apache80端口被佔用怎麼辦 apache80端口被佔用怎麼辦 Apr 13, 2025 pm 01:24 PM

當 Apache 80 端口被佔用時,解決方法如下:找出佔用該端口的進程並關閉它。檢查防火牆設置以確保 Apache 未被阻止。如果以上方法無效,請重新配置 Apache 使用不同的端口。重啟 Apache 服務。

apache怎麼啟動 apache怎麼啟動 Apr 13, 2025 pm 01:06 PM

啟動 Apache 的步驟如下:安裝 Apache(命令:sudo apt-get install apache2 或從官網下載)啟動 Apache(Linux:sudo systemctl start apache2;Windows:右鍵“Apache2.4”服務並選擇“啟動”)檢查是否已啟動(Linux:sudo systemctl status apache2;Windows:查看服務管理器中“Apache2.4”服務的狀態)啟用開機自動啟動(可選,Linux:sudo systemctl

oracle怎麼啟動監聽 oracle怎麼啟動監聽 Apr 12, 2025 am 06:00 AM

啟動 Oracle 監聽器的步驟如下:檢查監聽器狀態(使用 lsnrctl status 命令)對於 Windows,在 Oracle Services Manager 中啟動 "TNS Listener" 服務對於 Linux 和 Unix,使用 lsnrctl start 命令啟動監聽器運行 lsnrctl status 命令驗證監聽器是否已啟動

如何在Debian上監控Nginx SSL性能 如何在Debian上監控Nginx SSL性能 Apr 12, 2025 pm 10:18 PM

本文介紹如何在Debian系統上有效監控Nginx服務器的SSL性能。我們將使用NginxExporter將Nginx狀態數據導出到Prometheus,再通過Grafana進行可視化展示。第一步:配置Nginx首先,我們需要在Nginx配置文件中啟用stub_status模塊來獲取Nginx的狀態信息。在你的Nginx配置文件(通常位於/etc/nginx/nginx.conf或其包含文件中)中添加以下代碼段:location/nginx_status{stub_status

Debian系統中如何設置回收站 Debian系統中如何設置回收站 Apr 12, 2025 pm 10:51 PM

本文介紹兩種在Debian系統中配置回收站的方法:圖形界面和命令行。方法一:使用Nautilus圖形界面打開文件管理器:在桌面或應用程序菜單中找到並啟動Nautilus文件管理器(通常名為“文件”)。找到回收站:在左側導航欄中尋找“回收站”文件夾。如果找不到,請嘗試點擊“其他位置”或“計算機”進行搜索。配置回收站屬性:右鍵點擊“回收站”,選擇“屬性”。在屬性窗口中,您可以調整以下設置:最大大小:限制回收站可用的磁盤空間。保留時間:設置文件在回收站中自動刪除前的保

如何優化debian readdir的性能 如何優化debian readdir的性能 Apr 13, 2025 am 08:48 AM

在Debian系統中,readdir系統調用用於讀取目錄內容。如果其性能表現不佳,可嘗試以下優化策略:精簡目錄文件數量:盡可能將大型目錄拆分成多個小型目錄,降低每次readdir調用處理的項目數量。啟用目錄內容緩存:構建緩存機制,定期或在目錄內容變更時更新緩存,減少對readdir的頻繁調用。內存緩存(如Memcached或Redis)或本地緩存(如文件或數據庫)均可考慮。採用高效數據結構:如果自行實現目錄遍歷,選擇更高效的數據結構(例如哈希表而非線性搜索)存儲和訪問目錄信

See all articles