php SQLSTATE[HY000] [1045] 使用 CakePHP 拒绝用户“用户名”@“本地主机”的访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31154124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' using CakePHP
提问by saurav
I am new to PHP and CakePHP. I am finding problems while wiring my database using CakePHP.
我是 PHP 和CakePHP 的新手。我在使用 CakePHP 连接数据库时发现问题。
Below is my application configuration.
下面是我的应用程序配置。
I am on Bitnami WAMP stack 5.4.40-0. I am using CakePHP 3.0.4 to create a web MVC application
我在 Bitnami WAMP 堆栈 5.4.40-0 上。我正在使用 CakePHP 3.0.4 创建一个 web MVC 应用程序
Entry for datasources in my app.php
file.
在我的app.php
文件中输入数据源。
/**
* Connection information used by the ORM to connect
* to your application's datastores.
* Drivers include Mysql Postgres Sqlite Sqlserver
* See vendor\cakephp\cakephp\src\Database\Driver for complete list
*/
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
/**
* CakePHP will use the default DB port based on the driver selected
* MySQL on MAMP uses port 8889, MAMP users will want to uncomment
* the following line and set the port accordingly
*/
//'port' => 'nonstandard_port_number',
'username' => 'test2',
'password' => 'computer',
'database' => 'jobs',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
/**
* Set identifier quoting to true if you are using reserved words or
* special characters in your table or column names. Enabling this
* setting will result in queries built using the Query Builder having
* identifiers quoted when creating SQL. It should be noted that this
* decreases performance because each query needs to be traversed and
* manipulated before being executed.
*/
'quoteIdentifiers' => false,
/**
* During development, if using MySQL < 5.6, uncommenting the
* following line could boost the speed at which schema metadata is
* fetched from the database. It can also be set directly with the
* mysql configuration directive 'innodb_stats_on_metadata = 0'
* which is the recommended value in production environments
*/
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
],
I have already created a database table called jobsaccording to CakePHP conventions. User test2 has global privileges the same as the root administrator.
我已经根据 CakePHP 约定创建了一个名为jobs的数据库表。用户 test2 具有与 root 管理员相同的全局权限。
But when I am running the bake all command, I am getting the following error:
但是当我运行 bake all 命令时,我收到以下错误:
2015-07-01 06:24:56 Error: [PDOException] SQLSTATE[HY000] [1045] Access denied for user 'test2'@'localhost' (using password: YES)
Stack Trace:
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Driver\PDODriverTrait.php(48): PDO->__construct('mysql:host=127....', 'test2', 'computer', Array)
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Driver\Mysql.php(89): Cake\Database\Driver\Mysql->_connect('mysql:host=127....', Array)
C:\Bitnami\wampstack-5.4.40-0\apache2\htdocs\myjobs\vendor\cakephp\cakephp\src\Database\Schema\BaseSchema.php(46): Cake\Database\Driver\Mysql->connect()
PROBLEM SOLVED (UPDATE)
问题已解决(更新)
I followed Ankit and Spencer's directions.
我遵循 Ankit 和 Spencer 的指示。
I had a couple of problems.
我有几个问题。
Host of my user was not localhost; it was a wildcard
%
. Changed that, then MySQL started refusing connections.I disabled my firewall and found that the port was different from 3306. So I changed the entry in
app.php
. Now my application is baked :)
我的用户的主机不是本地主机;这是一个通配符
%
。改变了这一点,然后 MySQL 开始拒绝连接。我禁用了我的防火墙,发现端口和 3306 不同。所以我更改了
app.php
. 现在我的应用程序已经出炉了:)
回答by spencer7593
That error message usually means that either the password we are using doesn't match what MySQL thinks the password should be for the user we're connecting as, or a matching MySQL user doesn't exist (hasn't been created).
该错误消息通常意味着我们使用的密码与 MySQL 认为我们连接的用户的密码不匹配,或者匹配的 MySQL 用户不存在(尚未创建)。
In MySQL, a user is identified by both a username ("test2") anda host ("localhost").
在 MySQL 中,用户由用户名(“test2”)和主机(“localhost”)标识。
The error message identifies the user("test2") and the host("localhost") values...
错误消息标识了用户(“test2”)和主机(“localhost”)值...
'test2'@'localhost'
We can check to see if the user exists, using this query from a client we can connect from:
我们可以检查用户是否存在,使用来自我们可以连接的客户端的这个查询:
SELECT user, host FROM mysql.user
We're looking for a row that has "test2" for user, and "localhost" for host.
我们正在寻找一行,其中user为“test2 ”,host为“localhost” 。
user host
------- -----------
test2 127.0.0.1 cleanup
test2 ::1
test2 localhost
If that row doesn't exist, then the host may be set to wildcard value of %
, to match any other host that isn't a match.
如果该行不存在,则主机可以设置为通配符值%
,以匹配不匹配的任何其他主机。
If the row exists, then the password may not match. We can change the password (if we're connected as a user with sufficient privileges, e.g. root
如果该行存在,则密码可能不匹配。我们可以更改密码(如果我们以具有足够权限的用户身份连接,例如root
SET PASSWORD FOR 'test2'@'localhost' = PASSWORD('mysecretcleartextpassword')
We can also verify that the user has privileges on objects in the database.
我们还可以验证用户是否对数据库中的对象具有权限。
GRANT SELECT ON jobs.* TO 'test2'@'localhost'
EDIT
编辑
If we make changes to mysql privilege tables with DML operations (INSERT,UPDATE,DELETE), those changes will not take effect until MySQL re-reads the tables. We can make changes effective by forcing a re-read with a FLUSH PRIVILEGES
statement, executed by a privileged user.
如果我们使用 DML 操作(INSERT、UPDATE、DELETE)对 mysql 权限表进行更改,则这些更改将在 MySQL 重新读取表之前生效。我们可以通过强制重新读取FLUSH PRIVILEGES
由特权用户执行的语句来使更改生效。
回答by Ankit Pise
Check Following Things
检查以下事项
- Make Sure You Have MySQL Server Running
- Check connection with default credentials i.e. username : 'root' & password : '' [Blank Password]
- Try login phpmyadmin with same credentials
- Try to put 127.0.0.1 instead localhost or your lan IP would do too.
- Make sure you are running MySql on 3306 and if you have configured make sure to state it while making a connection
- 确保你有 MySQL 服务器运行
- 使用默认凭据检查连接,即用户名:'root' & 密码:'' [Blank Password]
- 尝试使用相同的凭据登录 phpmyadmin
- 尝试将 127.0.0.1 改为 localhost 或您的局域网 IP 也可以。
- 确保您在 3306 上运行 MySql,如果您已配置,请确保在建立连接时声明它
回答by Batuhan AKTA? boheminsan
I saw it's solved, but I still want to share a solution which worked for me.
我看到它已经解决了,但我仍然想分享一个对我有用的解决方案。
.env file:
.env 文件:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=[your database name]
DB_USERNAME=[your MySQL username]
DB_PASSWORD=[your MySQL password]
MySQL admin:
MySQL管理员:
SELECT user, host FROM mysql.user
Console:
安慰:
php artisan cache:clear
php artisan config:cache
Now it works for me.
现在它对我有用。
回答by J?rgen Rudolph L?ker
If you use MAMP, you might have to set the socket: unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock
如果您使用 MAMP,则可能需要设置套接字: unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock
回答by rmiladelaroca
I want to add to the answers posted on above that none of the solutions proposed here worked for me. My WAMP, is working on port 3308 instead of 3306 which is what it is installed by default. I found out that when working in a local environment, if you are using mysqladmin in your computer (for testing environment), and if you are working with port other than 3306, you must define your variable DB_SERVER with the value localhost:NumberOfThePort, so it will look like the following: define("DB_SERVER", "localhost:3308"). You can obtain this value by right-clicking on the WAMP icon in your taskbar (on the hidden icons section) and select Tools. You will see the section: "Port used by MySQL: NumberOfThePort"
我想补充上面发布的答案,这里提出的解决方案都不适合我。我的 WAMP 正在使用端口 3308 而不是 3306,这是默认安装的端口。我发现在本地环境中工作时,如果您在计算机中使用 mysqladmin(用于测试环境),并且使用 3306 以外的端口工作,则必须使用值 localhost:NumberOfThePort 定义变量 DB_SERVER,因此它将如下所示:define("DB_SERVER", "localhost:3308")。您可以通过右键单击任务栏中的 WAMP 图标(在隐藏图标部分)并选择工具来获取此值。您将看到以下部分:“MySQL 使用的端口:NumberOfThePort”
This will fix your connection to your database.
这将修复您与数据库的连接。
This was the error I got: Error: SQLSTATE[HY1045] Access denied for user 'username'@'localhost' on line X.
这是我得到的错误:错误:SQLSTATE[HY1045] 用户 'username'@'localhost' 在 X 行拒绝访问。
I hope this helps you out.
我希望这能够帮到你。
:)
:)