Zend 2无法连接到数据库

时间:2014-06-05 19:36:13

标签: php mysql zend-framework

我正在使用带有MySQL的wamp服务器来编写我的第一个zend 2应用程序。

我有以下问题:

Message:
Connect Error: SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'kljpeer'
Stack trace:
#0 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Db\Adapter\Driver\Pdo\Pdo.php(249): Zend\Db\Adapter\Driver\Pdo\Connection->connect()
#1 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Db\Sql\Sql.php(120): Zend\Db\Adapter\Driver\Pdo\Pdo->createStatement()
#2 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Db\TableGateway\AbstractTableGateway.php(232): Zend\Db\Sql\Sql->prepareStatementForSqlObject(Object(Zend\Db\Sql\Select))
#3 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Db\TableGateway\AbstractTableGateway.php(208): Zend\Db\TableGateway\AbstractTableGateway->executeSelect(Object(Zend\Db\Sql\Select))
#4 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Db\TableGateway\AbstractTableGateway.php(195): Zend\Db\TableGateway\AbstractTableGateway->selectWith(Object(Zend\Db\Sql\Select))
#5 D:\Git\klj-website\module\Website\src\Website\Model\PostTable.php(24): Zend\Db\TableGateway\AbstractTableGateway->select()
#6 D:\Git\klj-website\module\Website\src\Website\Controller\HomeController.php(23): Website\Model\PostTable->fetchAll()
#7 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php(83): Website\Controller\HomeController->indexAction()
#8 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#9 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#10 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#13 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#14 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#15 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#16 D:\Git\klj-website\vendor\zendframework\zendframework\library\Zend\Mvc\Application.php(313): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#17 D:\Git\klj-website\public\index.php(17): Zend\Mvc\Application->run()
#18 {main}

这是我的配置文件:global.php

return array(
    'db' => array(
         'driver'         => 'Pdo',
         'dsn'            => 'mysql:dbname=kljpeer;host=localhost',
         'driver_options' => array(
             PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
         ),
     ),
     'service_manager' => array(
         'factories' => array(
             'Zend\Db\Adapter\Adapter'
                     => 'Zend\Db\Adapter\AdapterServiceFactory',
         ),
     ),
);

这是我的配置文件:local.php.dist

return array(
     'db' => array(
         'username' => 'root',
         'password' => '',
     ),
);

1 个答案:

答案 0 :(得分:3)

尝试将 local.php.dist 重命名为 local.php

├───config
│   │   application.config.php
│   │
│   └───autoload
│           global.php
│           local.php   - your local settings
│
  

Zend Framework 2的ModuleManager合并了所有配置   每个模块的module.config.php文件然后合并到文件中   config / autoload(* .global.php,然后* .local.php文件)

User Guide - Database and models

e.g:

// global.php
 return array(
     'db' => array(
         'driver'         => 'Pdo',
         'dsn'            => 'mysql:dbname=kljpeer;host=localhost',
         ...
     ),
 );

// local.php
 return array(
     'db' => array(
         'username' => 'YOUR USERNAME HERE',
         'password' => 'YOUR PASSWORD HERE',
     ),
 );