简体   繁体   中英

Zend Framework 2 Database Error

I am a newbie just start learning Zend Framework 2. When I am following the tutorial http://framework.zend.com/manual/2.3/en/user-guide/database-and-models.html , I have a problem with find database.

I found that it can connect to the localhost database, but cannot find any database.

Connect Error: SQLSTATE[HY000] [1049] Unknown database 'db_vote'

It can only find the database "mysql". But cannot find the new table if I create a new table inside "mysql".

The other thing weird is I changed my root password in phpmyadmin, but in zf2, if I change to new password it said access denied, but it works with the old password.

Seems like zend is connecting to the other "localhost"? Weird thoughts... Anyone can help me? Thanks in advance.

Code in Module.php:

public function getServiceConfig(){
    return array(
        'factories' => array(
            'Album\Model\AlbumTable' => function($sm){
              $tableGateway = $sm->get('AlbumTableGateway');
              $table = new AlbumTable($tableGateway);
              return $table;  
            },
            'AlbumTableGateway' => function($sm){
              $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
              $resultSetPrototype = new ResultSet();
              $resultSetPrototype->setArrayObjectPrototype(new Album());
              return new TableGateway('album_zend', $dbAdapter, null, $resultSetPrototype); 
            },
        ),
    );
}

global.php:

return array(
'db' => array(
    'driver'        => 'Pdo',
    'dsn'           => 'mysql:dbname=db_vote; 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:

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

Problem:

root@localhost

root@%

can be two different users.

If you look into user table in mysql database you will notice more than one root user. User for the web app might be different from the one you used to connect though the console.

How to investigate it:

Check which user is connected through the console (or any other tool you have used):

SELECT USER();

Make sure that you either stick to one user (root@localhost for instance) or make sure that the web app user has the right privileges:

USE mysql;
SELECT * FROM db;

Whenever you change privileges don't forget to flush them:

FLUSH PRIVILEGES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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