简体   繁体   English

无法通过 MAMP 连接到 MySQL

[英]Can't connect to MySQL through MAMP

So here's my array containing my credentials.所以这是我的包含我的凭据的数组。

'mysql' => array(
    'host' => '127.0.0.1',
    'username' => 'root',
    'password' => '*',
    'db' => 's'
)

And the actual connection和实际连接

 try{
    $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/username'));
}
catch(PDOException $e){
    die($e->getMessage());
}

And this is the error I get这是我得到的错误

SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: YES) SQLSTATE[28000] [1045] 用户“root”@“localhost”的访问被拒绝(使用密码:YES)

But I am aware that this means access is denied, but the credentials are 100% correct.但我知道这意味着访问被拒绝,但凭据是 100% 正确的。 I also tested via command line and through workbench.我还通过命令行和工作台进行了测试。

Are you sure a password is set?你确定设置了密码? What happens if you attempt to connect via the command line line this:如果您尝试通过命令行连接会发生什么:

/Applications/MAMP/Library/bin/mysql -uroot

Also, the password for root in MAMP is root .此外,密码root在MAMP是root This is no deep/dark secret.这不是深奥/黑暗的秘密。 Is the * in your example valid?您示例中的*是否有效?

And in your connector, have you tried using localhost instead of 127.0.0.1 ?在您的连接器中,您是否尝试过使用localhost而不是127.0.0.1 I have seen some MySQL setups work with one but not the other when it should be both.我已经看到一些 MySQL 设置与一个一起工作,但在应该同时使用另一个时却没有。

EDIT: If you somehow mucked up the root password, don't panic!编辑:如果您以某种方式弄乱了root密码,请不要惊慌! You can still reset it this way.您仍然可以通过这种方式重置它。 Warning, some folks feel this method of password reset is “risky” but that is generally true for a production server or any server in the wild.警告,有些人认为这种密码重置方法是“有风险的”,但对于生产服务器或野外的任何服务器来说,这通常是正确的。 From MAMP on your desktop, this should be 100% safe.从桌面上的 MAMP,这应该是 100% 安全的。

First, stop MAMP entirely.首先,完全停止 MAMP。

Next, start it up again from the command line with the skip-grant-tables option like so:接下来,从命令行使用skip-grant-tables选项再次启动它,如下所示:

/Applications/MAMP/Library/bin/mysqld --skip-grant-tables

Once that is done, you can login with 100% no password just like this:完成后,您可以像这样使用 100% 无密码登录:

/Applications/MAMP/Library/bin/mysql -uroot

Then you can reset the root password with this one-liner:然后您可以使用此单行重置root密码:

UPDATE user SET Password=PASSWORD('root') WHERE user='root'; FLUSH PRIVILEGES; exit;

Okay, now find the process running the MySQL daemon with skip-grant-tables from the command line like this:好的,现在从命令行使用skip-grant-tables找到运行 MySQL 守护进程的进程,如下所示:

ps -u [your system username] | grep "mysqld --skip-grant-tables"

A list with two items should be returned: One is the mysqld & the other is the command you just made.应该返回包含两个项目的列表:一个是mysqld ,另一个是您刚刚创建的命令。 Something like this:像这样的东西:

502  1759 ttys004    0:00.11 /Applications/MAMP/Library/bin/mysqld --skip-grant-tables
502  1766 ttys004    0:00.00 grep mysqld

Okay, so now we know the mysqld with skip-grant-tables has process ID 1759, go ahead and kill that like so:好的,现在我们知道带有skip-grant-tablesmysqld进程 ID 为 1759,继续像这样杀死它:

kill 1759

Restart MAMP again & the root password should work as expected now.再次重新启动 MAMP, root密码现在应该可以正常工作了。

Try adding a port to your array!尝试向您的阵列添加端口! For me, it worked!对我来说,它奏效了! The port is 8889 by default.端口默认为 8889。 You can change it by going to MAMP>Preferences, then the Ports tab.您可以通过转到 MAMP>Preferences,然后转到 Ports 选项卡来更改它。 Here's what I think (I haven't tried this!):这是我的想法(我还没有尝试过!):

'mysql' => array(
    'host' => '127.0.0.1',
    'username' => 'root',
    'password' => '*',
    'port' => '8889',
    'db' => 's'
)

您使用Config::get('mysql/username')两次,第二次应该是Config::get('mysql/password')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM