mysql登录出错问题解决办法

一、远程登录mysql出错,比如:

# mysql -u root -h 192.168.9.9 -p
Enter password: 

ERROR 1045 (28000): Access denied for user 'root'@'192.168.9.9' (using password: YES)

【解决办法】
1. 先用localhost登录
# mysql -u root -p
Enter password: 
2. 执行授权命令
mysql> grant all privileges on *.* to root@'%' identified by '123';
Query OK, 0 rows affected (0.07 sec)
3. 退出再试
mysql> quit
Bye
再试登录:
# mysql -u root -h 192.168.9.9 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.33 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出错,比如:

输入“mysql -uroot -p123456”后出现以下错误:

“Access denied for user 'root'@'localhost' (using password:YES)”
直接输入“mysql” 也回报类似的错误:应该是:

“Access denied for user 'ODBC'@'localhost' (using password:NO)”也是这个。

解决方案:

1、编辑/etc/my.cnf文件,在文件的最后添加一行“skip-grant-tables”,保存并关闭文件。
2、重启MySQL服务。
3、在命令行中输入“mysql -uroot -p”(不输入密码),回车即可进入数据库。
4、执行,“use mysql;”使用mysql数据库。
5、执行,“set password for root = password('654321');”(修改root的密码)
如果是5.7版本之后的话:
password字段改成了
authentication_string

所以更改语句替换为update mysql.user set authentication_string=password('654321') where user='root' ;即可


更新用户表:UPDATE user SET `Host` = '%' WHERE `User` = 'root' LIMIT 1; 
执行强制刷新权限:flush privileges;
6、打开MySQL目录下的my.cnf文件,删除最后一行的“skip-grant-tables”,保存并关闭文件。
7、重启MySQL服务。
8、在命令行中输入“mysql -uroot -p654321”,问题搞定!


猜你喜欢

转载自blog.csdn.net/csgd2000/article/details/81010184