MySQL 安装后ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

在RedHat安装MySQL后,打算登录,输入mysql,出现下面的错误:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

试过n次后,才发现登录方式不对,得需要用户名和密码。网上好多资料说默认安装后没有密码的,直接就输入mysql就可以登录,可能是以前版本是那样。
我装的是最新的mysql-5.6,在安装完成后,看到如下信息:

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.

You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.

Also, the account for the anonymous user has been removed.

In addition, you can run:

  /usr/bin/mysql_secure_installation

which will also give you the option of removing the test database.
This is strongly recommended for production servers.

很明显,提示生成一个随机密码的/root/.mysql_secret中,主意该文件为隐藏文件,在目录下输入ls -al可以看到。。在该文件中拷贝该随机密码。
重新登录,输入mysql -uroot -p(从上述文件中找到的随机密码)

[root@wx etc]# mysql -uroot -pLo6KjKPE
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 6
Server version: 5.6.14

Copyright (c) 2000, 2013, 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>

ok,登录成功。
然后输入任何语句都会提示:

ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

再一个看安装完成后的提示信息,得用 SET PASSWORD 改密码。
所以输入:

mysql>  SET PASSWORD = PASSWORD('123456');
//退出后重启服务
/etc/init.d/mysql restart
//重新登录
[root@wx etc]# mysql -uroot -p
Enter password:

ok。一切搞定。

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部