首先让我提一下,我已经看了很多建议的问题,但没有找到相关的答案。这就是我正在做的。

我连接到Amazon EC2实例。我可以用这个命令登录MySQL根目录:

mysql -u root -p

然后我用host %创建了一个新的用户帐单

CREATE USER 'bill'@'%' IDENTIFIED BY 'passpass';

授予用户bill的所有权限:

grant all privileges on *.* to 'bill'@'%' with grant option;

然后我退出root用户,尝试用bill登录:

mysql -u bill -p

输入正确的密码并得到以下错误:

错误1045(28000):用户“账单”@“localhost”(使用密码:YES)的访问被拒绝


当前回答

当你输入mysql -u root -p时,你是在通过本地unix套接字连接到mysql服务器。

不管你给的授权是什么,'bill'@'%'只匹配TCP/IP连接。

如果你想要授予对本地unix套接字的访问权限,你需要授予'bill'@'localhost'权限,奇怪的是,这与'bill'@'127.0.0.1'不一样。

您也可以使用TCP/IP连接mysql命令行客户端,以匹配您已经授予的权限,例如运行mysql -u root -p -h 192.168.1.123或任何您的本地IP地址。

其他回答

省得你自己头疼……您的问题可能是口令周围没有引号。至少我的案子绕了我三个小时。

[client]
user = myusername
password = "mypassword"   # <----------------------- VERY IMPORTANT (quotes)
host = localhost

http://dev.mysql.com/doc/refman/5.7/en/option-files.html

搜索“这是一个典型的用户选项文件:”,看看他们在那里陈述的例子。祝你好运,我希望能为别人节省一些时间。

Try:

~$ mysql -u root -p
Enter Password:

mysql> grant all privileges on *.* to bill@localhost identified by 'pass' with grant option;

对我来说,在进入mysql时没有指定-p参数。

mysql -p

我没有问题,但它是错误的调用mysql没有密码。

当你的密码包含一些特殊字符,如@,$等,也会发生这种情况。 为了避免这种情况,你可以把password用单引号括起来:

$ mysql -usomeuser -p's0mep@$$w0Rd'

或者在输入时不用密码。请留空,然后在终端要求时输入。这是推荐的方法。

$ mysql -usomeuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 191
Server version: 5.5.46-0ubuntu0.14.04.2 (Ubuntu)

Copyright (c) 2000, 2015, 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错误1045(28000):拒绝访问用户'user'@'localhost' (使用密码:YES);

Wampserver 3.2.0新的安装或升级

也许xamp使用mariaDB作为默认值就可以了。

Wamp服务器自带mariaDB和mysql, mariaDB默认安装在3306端口,mysql默认安装在3307端口,有时端口是3308。

连接mysql!

在安装时,它要求使用mariaDB或MySql,但mariaDB是默认的,你不能改变它,检查MySql选项并安装。

安装完成后,将在默认端口3306上运行mariaDB,在另一个端口3307或3308上运行mysql。

右击wampserver图标,它的运行应该在右下角,转到工具,看到正确的mysql运行端口。

并将其包含在数据库连接中,如下所示:

$host = 'localhost';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$port = '3308';//Port

$dsn = "mysql:host=$host;dbname=$db;port=$port;charset=$charset"; //Add in connection
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

注意:我使用pdo。

更多信息请访问:https://sourceforge.net/projects/wampserver/