php新手在mac系統中搭建apache+mysql+php的開發環境(按照這篇博客來操作的:http://my.oschina.net/joanfen/blog/171109?fromerr=xvCsafCe),在安裝配置mysql完畢后,登錄mysql,報錯:mac ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO),折騰很久,終於解決,隨手記錄下,備忘。
解決方法:
第一步:如果mysql服務正在進行,將之停止。
第二步:在終端中以管理員權限啟動mysqld_safe,命令如下:
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
執行結果如下:
2016-06-12T08:29:17.6NZ mysqld_safe Logging to '/usr/local/mysql/data/lyqdeMacBook-Pro.local.err'. 2016-06-12T08:29:17.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
第三步:不要關閉當前的終端窗口,新建一個終端窗口,輸入如下命令,回車登錄mysql
/usr/local/mysql/bin/mysql
登錄后,看到歡迎信息:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 37 Server version: 5.7.13 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. mysql>
打開"mysql"這個數據庫,SQL如下:
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>
然后,更新root的密碼,SQL如下:
mysql> update user set authentication_string=password('root') where Host='localhost' and User='root';
注意:
①有的版本的mysql中,密碼可能存儲在password字段中,可以使用"describe user;"命令來查看下表結構再操作
②authentication_string的值一定通過password函數來計算(password('root'))
執行結果如下:
Query OK, 1 row affected, 1 warning (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 1
退出mysql(執行sql語句:exit)
最后一步:將mysqld_safe進程殺死,重啟mysqld。
可能會遇到的問題
登錄mysql
/usr/local/mysql/bin/mysql -uroot -proot
這個時候,如果執行查詢之類的操作,比如執行"show databases;",可能會有如下提示:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
根據提示進行操作,輸入如下SQL語句,這個語句的作用是修改root用戶的口令為root:
mysql> alter user 'root'@'localhost' identified by 'root';
結果:
Query OK, 0 rows affected
至此,問題解決。
參考:http://stackoverflow.com/questions/13480170/access-denied-for-mysql-error-1045