简体   繁体   English

我正在尝试使用 PHP 连接到 mysql 数据库,但它不起作用

[英]I'm trying to connect to a mysql database using PHP but it won't work

I've Installed mysql, php, apache using a wamp configuration Where to access localhost is would be http://localhost:81 the PHP script I'm using is I've Installed mysql, php, apache using a wamp configuration Where to access localhost is would be http://localhost:81 the PHP script I'm using is

if(mysql_connect('localhost', 'root', 'exobytes15')) {
mysql_select_db('testDB');
}
else {
echo 'Could not Connect to the database';
 }

But this gives me the error但这给了我错误

1045: Access denied for user 'root'@'localhost' (using password: YES) 1045:用户'root'@'localhost'的访问被拒绝(使用密码:是)

What should I do to fix this problem?我应该怎么做才能解决这个问题?

Make sure you have that your database is running, and that the user has access to it.确保您的数据库正在运行,并且用户有权访问它。

You can use either use the plain old mysql commandline client for this or phpmyadmin if your wamp stack has that installed.您可以使用普通的旧 mysql 命令行客户端或 phpmyadmin 如果您的 wamp 堆栈已安装。

You're connecting with the wrong password: Reset the password您正在使用错误Reset the password

or more likely: you don't have privileges to connect from localhost.或更可能:您没有从本地主机连接的权限。 Run the command:运行命令:

GRANT ALL PRIVILEGES ON *.* TO 'root'@localhost WITH GRANT OPTION;

Test to see if you can connect.测试是否可以连接。

Never do: GRANT ALL PRIVILEGES ON *.* TO 'root'@% WITH GRANT OPTION;永远不要: GRANT ALL PRIVILEGES ON *.* TO 'root'@% WITH GRANT OPTION;

Because that will put your database server at risk from remote login attempts.因为这会使您的数据库服务器面临远程登录尝试的风险。

Either you didn't grant root@localhost the necessary rights to access the database or you're providing the wrong password.要么您没有授予 root@localhost 访问数据库的必要权限,要么您提供了错误的密码。

Note: granting access to root@`%` does NOT grant access to root@localhost...注意:授予对 root@`%` 的访问权限不会授予对 root@localhost 的访问权限...

Maybe u are running mysql on a port other than the default port.也许您在默认端口以外的端口上运行 mysql。

If it is the case use this:如果是这种情况,请使用:

mysql_connect('localhost:PORT', 'root', 'exobytes15')

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

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