解決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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

熱門話題

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

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

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

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

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

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

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