akhil
akhil

Reputation: 347

Exception 'could not find driver' while migration in yii2

This my stack trace:

  Exception 'yii\db\Exception' with message 'could not find driver'

in /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/db/Connection.php:534

#0 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/db/Connection.php(836): yii\db\Connection->open()
        #1 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/db/Connection.php(823): yii\db\Connection->getMasterPdo()
        #2 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/db/Command.php(208): yii\db\Connection->getSlavePdo()
        #3 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/db/Command.php(816): yii\db\Command->prepare(true)
        #4 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/db/Command.php(350): yii\db\Command->queryInternal('fetchAll', NULL)
        #5 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/db/mysql/Schema.php(198): yii\db\Command->queryAll()
        #6 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/db/mysql/Schema.php(97): yii\db\mysql\Schema->findColumns(Object(yii\db\TableSchema))
        #7 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/db/Schema.php(140): yii\db\mysql\Schema->loadTableSchema('migration')
        #8 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/console/controllers/MigrateController.php(121): yii\db\Schema->getTableSchema('{{%migration}}', true)
        #9 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(610): yii\console\controllers\MigrateController->getMigrationHistory(NULL)
        #10 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(102): yii\console\controllers\BaseMigrateController->getNewMigrations()
        #11 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)
        #12 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)
        #13 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/base/Controller.php(151): yii\base\InlineAction->runWithParams(Array)
        #14 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/console/Controller.php(91): yii\base\Controller->runAction('', Array)
        #15 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/base/Module.php(455): yii\console\Controller->runAction('', Array)
        #16 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/console/Application.php(161): yii\base\Module->runAction('migrate', Array)
        #17 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/console/Application.php(137): yii\console\Application->runAction('migrate', Array)
        #18 /opt/lampp/htdocs/advanced/vendor/yiisoft/yii2/base/Application.php(375): yii\console\Application->handleRequest(Object(yii\console\Request))
        #19 /opt/lampp/htdocs/advanced/yii(31): yii\base\Application->run()
        #20 {main}

I am new to yii2. I have added database name,username in advaced/common/config/main-local.php. Using xampp webserver and version of php is 5.6.8 and PDO is also enabled.

This is my db configuration.

'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=yii2advanced',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ],

Upvotes: 13

Views: 41607

Answers (8)

I re-configured php, adding this flag:

--with-pdo-mysql

I'm using LAMP, and this worked for me.

Upvotes: 0

In my case (on Ubuntu):

sudo apt-get install php-mysql  
sudo service apache2 restart

Upvotes: 4

Lee
Lee

Reputation: 21

in my case:

add extension=php_pdo_mysql.dll in php\php.ini

Upvotes: 1

user3345269
user3345269

Reputation: 1

I solved this problem by changing PHP path like this

  1. Go to "My Computer" right click and click "Properties"
  2. Click "Advanced system settings"
  3. Click "Environment Variables"
  4. Find the "Path" in System variables, double click it.
  5. Find your PHP Path or add it if you not have it yet, like "D:\xampp\php"
  6. Click OK and restart your Apache server.
  7. That's it, I can migrate db then.

Upvotes: 0

Levi Saturnino
Levi Saturnino

Reputation: 221

I had a similar situation during Yii2 (Advanced) migration (Xampp in Ubuntu 14.04 and PHP)

This is the message i've got:

Exception 'yii\db\Exception' with message 'could not find driver'

So after googling and reading this thread and others I found out that could be the possible problem

the PHP extension "pdo_mysql" was disabled or not installed at all

and tried this at my terminal:

php -m

after realize that the module was not there...

sudo apt-get install php5-gd php5-mysql

and after a quick install the service must be re-started...

sudo /opt/lampp/lampp restart

....finally (retry migration)

php yii migrate

Migrated up successfully.

Hope you find it useful & work for others.

I found it at ask ubuntu but i'm unable to add a link or code, since this is my first post.

link: http://www.yiiframework.com/forum/index.php/topic/49450-error-on-migrate-command-advanced-yii2-app/

Upvotes: 9

Dan Cundy
Dan Cundy

Reputation: 2859

Found this on Yii2

"Most likely it is your system config. If you are using packages like MAMP, XAMPP or other, you may need to change "localhost" to "127.0.0.1" in your db.php configuration file. Also if you are on Linux you may need to install some other additional packages like php5-mysql and stuff like that."

Upvotes: 19

Kevin Murda
Kevin Murda

Reputation: 191

I had same error, but instead of xampp I used Fedora 25 with Apache, for me 'dnf install -y php-mysqlnd' fixed this error, so you are probably missing extension.

Upvotes: 0

Alexis
Alexis

Reputation: 41

I had exactly the same error. Searching, I found a Brazilian comment regarding a similar problem caused because he had two different php version installed in his computer.

I checked the /opt/lampp/bin directory and I found two different php versions, one just plain "php" and the other "php-5.6.8", so I decided to run the second version, and it works!

test@pctest:~$ /opt/lampp/bin/php-5.6.8 /opt/lampp/htdocs/advanced/yii migrate

Upvotes: 3

Related Questions