【发布时间】:2015-09-18 04:27:20
【问题描述】:
我是 PHP 新手和CakePHP。我在使用 CakePHP 连接数据库时发现问题。
下面是我的应用配置。
我在 Bitnami WAMP 堆栈 5.4.40-0 上。 我正在使用 CakePHP 3.0.4 创建一个 Web MVC 应用程序
在我的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'],
],
我已经根据 CakePHP 的约定创建了一个名为 jobs 的数据库表。用户 test2 具有与 root 管理员相同的全局权限。
但是当我运行 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()
问题已解决(更新)
我听从了 Ankit 和 Spencer 的指示。
我遇到了几个问题。
我的用户的主机不是本地主机;这是一个通配符
%。改变了这一点,然后 MySQL 开始拒绝连接。我禁用了防火墙,发现端口与3306不同。所以我更改了
app.php中的条目。现在我的应用程序已经出炉了 :)
【问题讨论】:
-
Access denied for user 'test2'@'localhost' (using password: YES)这很明确。 是否用户 test2 有访问权限? IE。这行得通吗:mysql -utest2 -pcomputer jobs? -
奇怪,将
%更改为localhost真的对我有用...而且它可能会创建另一个用户 - 删除带有%主机的用户 -
在这些更改之后重新启动 MySQL 是锦上添花。
-
当您错过了 php 文件的扩展名时会发生同样的错误。在我的情况下,app.php 是没有扩展名的应用程序,添加扩展名后,问题就解决了。
标签: php mysql cakephp cakephp-3.0