mac mysql Access denied for user問題有效解決方法
可能有些讀者在安裝完 mysql 之後出現以下錯誤提示:
mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
這是 mysql 在 linux 系統下的一個小缺陷,未給出 root 密碼,導致初次無法登陸。這裡給出 mysql 密碼修改的有效方案,該方案在5.7.12親測有效:
0) 在使用者目錄下輸入以下命令:
touch bashrc
sudo chmod +w bashrc
sudo vi /etc/bashrc
在bashrc的末尾增加以下兩個命令別名,便於快速使用mysqlalias mysql='/usr/local/mysql/bin/mysql' alias mysqladmin='/usr/local/mysql/bin/mysqladmin'
1) 停止 mysql 服務
sudo /usr/local/mysql/support-files/mysql.server stop
2) 在安全模式下啟動 mysql
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
3) 新建一個命令視窗(可在當前視窗下按快捷鍵command+N建立):
mysql -u root
4) 在命令視窗下修改命令
mysql > UPDATE mysql.user SET Password=PASSWORD('1234') WHERE User='root';
若上面的命令不行,試試下面的:
mysql> update mysql.user set authentication_string=password('1234') where user='root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 1
如未成功請參看【補充】
5)
mysql > FLUSH PRIVILEGES;
6) 新建命令視窗,用密碼1234再嘗試登陸
mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.12 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
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.
看到上述資訊,就表明成功用 root 登陸 mysql。
【補充】也許不同版本 mysql.user 欄目名稱會有不同,如果上述 4)命令報錯:
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
那麼就更改命令為:
mysql> update mysql.user set authentication_string=password('1234') where user='root';