I have an advanced yii2 template. I'm trying to create a console command.
I've created a controller class and action incide console/controllers folder:
namespace console\controllers;
use yii\console\Controller;
class WorkModelController extends Controller
{
public function actionValidate(){}
}
My action should connect with mysql database, select some data and do something with it. When I running the command: yii work-model/validate I get this error:
C:\OSPanel\domains\localhost>yii work-model/validate Exception
'yii\db\Exception' with message 'could not find driver'
in
C:\OSPanel\domains\localhost\vendor\yiisoft\yii2\db\Connection.php:56
My console/config/main.php and main-local.php files contain next db-config:
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbname',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
];
When I trying connect to the database from another part of app all works fine, but when I trying connect from console I get this error.
Please, help me slove this error.
try to run php -me from the cli and make sure pdo_mysql is there..
if not, then u need to enable it from your php.ini config.
Sometime, when u use server stack such as lamp/xampp, u probably missed out from resync your environment path to use the same version of php.ini of your server stack. to do this, u may simply check/compare php.ini path from both phpinfo() from the browser and php -i from the cli
Related
I'm using spatie/laravel-backup in a WAMP localhost.
It works fine when I type manually in the windows cmd:
php artisan backup:run
But when I try to run the backup using the laravel Artisan class:
Artisan::call('backup:run');
It throw an error:
'mysqldump' not recognized ...
In the laravel mysql config I've also specified the path to the dumper:
'mysql' => [
'driver' => 'mysql',
// ...
'dump' => [
'dump_binary_path' => 'E:/wamp/wamp64/bin/mysql/mysql5.7.9/bin',
],
],
How can i fix that?
EDIT
Probably it's just support "bug" for windows (finded out thanks Loek's answer), as the author says, so can I run a backup in a controller without a command safely? maybe with something like:
use Spatie\Backup\Tasks\Backup\BackupJobFactory;
BackupJobFactory::createFromArray(config('laravel-backup'))->run();
As the command itself.
I believe it's the forward slashes. Try this:
'mysql' => [
'driver' => 'mysql',
// ...
'dump' => [
'dump_binary_path' => 'E:\\wamp\\wamp64\\bin\\mysql\\mysql5.7.9\\bin',
],
],
EDIT
Support for Windows is wonky at best, with multiple "This package doesn't support Windows" comments from the creators on GitHub issues. This one is the latest: https://github.com/spatie/laravel-backup/issues/311
It could also be a permission problem. Executing from the command line is probably happening from another user than executing from the web server, so Windows is denying access to mysqldump.
2nd edit
As long as you make sure the controller only gets called when it needs to be, I don't see why this wouldn't work!
I'm using Laravel 5 but I'm not being able to migrate my database table. I have a macbook pro and I'm using Terminal. I'm using php artisan command:
php artisan migrate.
When I execute this command, I get the following error message: [PDOException]
SQLSTATE[HY000] [2002] Connection refused.
I have configured my database.php following the official tutorial videos on laracasts.com. My database.php file looks like the following:
...
'fetch' => PDO::FETCH_CLASS,
...
'default' => env('DB_CONNECTION', 'sqlite'),
...
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
....
I have read many comments on stackoverflow.com about this issue. Most of them are talking about modifying the ".env" file. The thing is I can't find this file! Which makes me wonder if my installation of Laravel is complete or not! I read that my ".env" file might be overriding my "database.php" file but I can't file the ".env" file!
The env file is most likely hidden.
Run defaults write com.apple.finder AppleShowAllFiles YES and Killall Finder in Terminal.
Open Finder and you should be able to see the .env file.
Edit your .env file.
I am using two Docker container, one having ubuntu and an apache webserver running, the other one a mysql server. The containers are linked and i can connect from the ubuntu container onto the mysql server. For the connection I use in the ubuntu container:
mysql -u root -h mysql
where the second 'mysql' is the name of the container. I can connect to it through the container id as well, so the connection works as well as connecting onto the database from the windows environment.
What doesnt work is the connection from doctrine to the database within the PHP application which is in the ubuntu container.
The config looks like this:
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'mysql',
'port' => '3306',
'user' => 'root',
'password' => '',
'dbname' => 'db_name',
'charset' => 'utf8',
)
)
),
)
But I get the Error message
Uncaught PDOException: could not find driver in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php
and
Zend\ServiceManager\Exception\ServiceNotCreatedException: An abstract factory could not create an instance of doctrine.entitymanager.ormdefault(alias: doctrine.entitymanager.orm_default). in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php
Does anyone have any idea how to solve this and where exactly the error comes from?
I have already tried to put in the container ID as 'host' and commented out the 'password' field as it is not used.
Thanks in advance
Jonathan
You need to have pdo_mysql allowed on your system, it doesn't seem a connectivity issue but a php configuration problem.
Can you try to do this command inside your php container
php -i | grep pdo_mysql
Just to understand if php has this module
I am new with YII 2.0 and i have installed new PHP7.0.2 Version, i was successfully installed the YII2.0 via composer and i enabled my gii code creation.
So that now i want to create a model file for my table users, when i click the model creation via gii page, it showing the "Database Exception – yii\db\Exception".
Can you please suggest your answer for rectify this problem.
The db.php configuration file must define db which looks like the following:
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=myDb',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'rootPassword',
'charset' => 'utf8',
),
OR
Edit php.ini to enable each MySQL-related extension such as the following:
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo_mysql.dll
I have this error when i use php artisan migrate in my Laravel project.
[PDOException]
SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'
this is my database.php file :
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'current_db_name'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
I saw this error in this question and this question but none of them was not helpful.
It clearly mentions that there's no such database named previous_db_name.
From what it seems the database.php file is not where the variable is from.
Check the .env file in your Laravel installation folder to see if that's the database name that you have wrongly specified.
Please run this command:
php artisan config:cache
php artisan cache:clear
AND
restart your server
I had to kill my server and re-run php artisan serve for my project to register the change of my DB_DATABASE name. I kept getting the error even after clearing the cache because I didn't realize this.
In Laravel 5.x You should define the DB Details in two files
.env file in the project folder
.database.php file inside config folder
If you are using PHP artisan serve just restart it. (just in case someone comes searching as I did).
set your database name in .env file too.
I know, it is super late but for people like me, new in laravel and following tutorial from artisansweb. Note that, migrate will not create your database. Rather, it will just create all the tables. Assuming, you have set up your .env file. Now the important part is create the database and users (if you decided to go with custom users) manually. Then, do the php artisan migrate command
hope that helps.
Try this :
<?php
class Config {
private $host = "localhost";
private $db_name = "db_ahp";
private $username = "root";
private $password = "";
public $conn;
public function getConnection() {
$this->conn = null;
try {
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
This might be late but I am just updating in case someone else faces a similar issue. I came across the same error, in my case it happened to be due to a missing database in xampp. i.e I was literally running a migration on an inexisting database in xampp.
Kill your server in case it's running. (Use Ctrl+C on windows.)
Make sure that you created the actual DB in your phpMyAdmin.
Run the following command to clear the cache: php artisan cache:clear
Run php artisan migrate once again.
Everything should be good after this.
Happy coding.