MySQL用户'root'执行代码时(Windows 7)


MySQL Access denied for user 'root'@'localhost' when executing code (Windows 7)

我知道这个问题已经被问过无数次了,但是我看过了所有的解决方案,到目前为止没有一个能解决我的问题。

所以我可以访问phpmyadmin,我已经尝试了&

现在我的代码是在线的,但我的数据库是在本地主机(使用wamp)。我以前试过,效果很好。

当我尝试执行代码(它应该发送一个反馈到数据库),我有这个错误:

SQLSTATE[28000][1045]用户'root'@'localhost'被拒绝访问(使用password: YES)

这是我如何连接到我的数据库:

try {
$bdd = new PDO('mysql:host=localhost;dbname=lexcelera', 'root', 'password');
}
catch(Exception $e) {
    die('Erreur : ' .$e->getMessage());
}

这是我的config.inc:

/* Authentication type */
$cfg['Servers'][$i]['verbose'] = '';
//$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'password';
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
/* Select mysql if your server does not have mysqli */
$cfg['Servers'][$i]['extension'] = 'mysql';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
我的问题有解决办法吗?我也在用Windows 7(不幸的是,很多可能的解决方案都是针对Linux的)。

谢谢!

你复制粘贴的配置是从phpmyadmin我猜。首先确保你可以连接到mysql服务器,也许从命令行

mysql -u root -p

然后你会被要求输入密码,输入

password

,按回车键。

如果它连接,这意味着你可以用你的root帐户访问mysql服务器,问题在别处。

那么您应该检查您正在尝试连接的数据库(lexcelera)是否已创建。

如果它不工作,这意味着你不能访问你的mysql服务器,这意味着可能mysql的根用户没有配置在粘贴的文本(phpmyadmin配置)。

如果您的代码在线,在远程服务器上,而您的数据库在本地机器上,当然它不起作用。Localhost指向本地机器。当你在远程机器上执行代码时,它会搜索本地数据库