laravel php artisan migrate Error (timeout) - laravel-5

I am using Laravel 5.6
when I use the command 'php artisan migrate' I get this error (after a minute) :
"Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Operation timed out (SQL: select * from information_schema.tables where table_schema = MYDATABASE and table_name = migrations)"
I have set up the correct database configuration in .env and config/database.php
I checked that my db connection works :
if(DB::connection()->getDatabaseName())
{
echo "Yes! successfully connected to the DB: " . DB::connection()->getDatabaseName();
}
And it works.
if i run the query directly in mysql, it will not work because of the lack of quotes.
if instead I run this in mysql it will work:
select * from information_schema.tables where table_schema = 'MYDATABASE' and table_name = 'migrations'
the issue in php artisan seem to be : "Operation timed out"
(not the usual "File not found" when i tried to look for an answer here.
How to solve the php artisan migrate issue ???
thank you!

I had this same problem and it turned out my host was incorrect.
In my .env file I set DB_HOST to the correct value and it all worked like a charm.

Related

When I run " php artisan migrate " in my project the following error occurs, How can I solve the error?

When I run " php artisan migrate " in my project the following error occurs, How can I solve the error?
Illuminate\Database\QueryException
could not find driver (SQL: select * from information_schema.tables where table_schema = tc_cse-infohub and table_name = migrations and table_type = 'BASE TABLE')
at F:\xammp\htdocs\github\TC_CSE-infoHub\vendor\laravel\framework\src\Illuminate\Database\Connection.php:712
708▕ // If an exception occurs when attempting to run a query, we'll format the error
709▕ // message to include the bindings with SQL, which will make this exception a
710▕ // lot more helpful to the developer instead of just the database's errors.
711▕ catch (Exception $e) {
➜ 712▕ throw new QueryException(
713▕ $query, $this->prepareBindings($bindings), $e
714▕ );
715▕ }
716▕ }
``
1 F:\xammp\htdocs\github\TC_CSE-infoHub\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70 PDOException::("could not find driver")
2 F:\xammp\htdocs\github\TC_CSE-infoHub\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70 PDO::__construct()
```
It could be the wrong mysql port number in your .env file. Check your .env file and make sure the port number matches the same one mysql is using, also the SQL passwords in your env file, because the laravel framework is rejecting the connection hence the error on your screen.
Edit: If you're using windows make sure your php / db extensions are enabled on your xampp / wampp configurations.
For example: ;extension=pdo_mysql.so //uncomment this line just remove ;
after that please try again for php artisan migrate
You should install PDO on your server. Edit your php.ini (look at your phpinfo(), "Loaded Configuration File" line, to find the php.ini file path). Find and uncomment the following line (remove the ; character):
;extension=pdo_mysql.so
After this restart the server and should be good to go.
Edit:
composer update
composer require doctrine/dbal
php artisan cache:clear
php artisan view:clear
php artisan route:clear
Check your .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=username
DB_PASSWORD=password
run php -m and see if it lists pdo
Firest of all, adding phpinfo to your code path find out your php.ini location
<?php
phpinfo();
?>
Since you have already uncommented MySQL extension, make sure to uncomment extension=php_pdo.dll as well in the php.ini and restart the Apache.
Before migration, make sure to run
php artisan cache:clear
php artisan view:clear
php artisan route:clear

How to fix the "Table doesn't exist" error error when running 'php artisan migrate' command?

I have a project on which I need to run php artisan migrate. It should be easy but I am getting an error:
php artisan migrate
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'skz.master_courses' doesn't exist (SQL: select * from
master_courses where date_from > 2021-06-01 and
master_courses.deleted_at is null order by date_from asc limit
1)
I don't understand where the select command comes from. Does anybody know where is it from?
The error usually throw when database doesn't exist in mysql.
But in your case, database exists in mysql server.So you it look like your application executing select query in any one of the service provider boot() method
public function boot()
{
}
So you might need to stop query execution till migration completes.

Laravel project not run by php artisan serve command

error when run php artisan serve command
C:\xampp\htdocs\crm>php artisan serve
In Connection.php line 647:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'crm.clients' doesn't exist (SQL: select * from `clients` where exists (select * from `followups` where `clients`.`id` = `followups`.`client_id` and `followup` = TBRO and date(`date`) = 2018-02-18))
In Connection.php line 319:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'crm.clients' doesn't exist
the trouble seem to be that you don't have the table created. Have you ran the migrate command?
In this case, you have to first update your composer by this command.
composer update
I can suggest you to check your db if there is a db called crm in the case you are in a MySQL environment and also check your connection to the database in the config/database or in the .env . It seems that you cannot create the table with the migrate command and since you doesn’t have the tables the serve command report an error.

Error while migrating from Laravel tinker console (sqlite)

Consider
laravel new bug
Then adding in .env
DB_CONNECTION=sqlite
DB_DATABASE=/home/me/Code/bug/storage/database.sqlite
creating database
touch /home/me/Code/bug/storage/database.sqlite
migrating
php artisan migrate && php artisan migrate:fresh
Now trying to migrate programmatically in tinker
php artisan tinker
>> Artisan::call('migrate');
First time resulting in => 0 (as expected ?) But second time:
>>> Artisan::call('migrate:fresh')
Illuminate\Database\QueryException with message 'SQLSTATE[HY000]:
General error: 17 database schema has changed (SQL: select * from
sqlite_master where type = 'table' and name = migrations)'
Artisan::call('migrate')
Third time:
Symfony\Component\Console\Exception\CommandNotFoundException with
message 'There are no commands defined in the "migrate" namespace.'
Any ideas whats going on here? Can you reproduce?
Update using postgres. The second time running Artisan::call('migrate:fresh') I get:
Symfony\Component\Console\Exception\CommandNotFoundException with
message 'There are no commands defined in the "migrate" namespace.'
UPDATE 2: Issue filed here: https://github.com/laravel/framework/issues/22997
https://github.com/laravel/tinker/issues/37
I don't think if this is a laravel bug, but I found your problem:
Calling a non existing command like Artisan::call('migrate:fresh') will crash. The reason is a typo: tinker does not accept fresh, only refresh. Calling the command via tinker will empty the sqllite file so you can't execute any commands until you migrate without tinker.
Here are my steps:
php artisan migrate # sqllite file is filled with content
php artisan tinker
> Artisan:call('refresh')
> Artisan::call('fresh') # now the sqllite file is empty
> Artisan::call('migrate') # will crash
php artisan migrate
> Artisan::('refresh') # works
so I think it's a tinker bug, not a laravel one. Tinker is doing some kind of snapshot (If you change a Model you have to restart tinker to call it there), maybe the error handling is not implemented is the best way to catch all errors.

How to create a mysql db with Laravel

I'm using Laravel 5.2. I've setup my first migrations and I want to run them. From the video tutorial it doesn't explain how to create a mysql db. I know I can do this manually in phpmyadmin but is there a Laravel way to do it?
This will install the migrations table:
php artisan migrate:install
Is there a similar command that will create the DB?
I'm thinking the process should be:
php artisan DB:install (or similar command)
Install the migrations table:
php artisan migrate:install
Run the migrations:
php artisan migrate
and to rollback the migrations:
php artisan migrate:rollback
Nothing provided out of the box but you could make your own command that could do this for you:
php artisan make:console CreateDatabase
// Note, in 5.3 this is make:command
Then in app/Console/Commands you'll find CreateDatabase.php. Open that sucker up and let's make a few changes:
protected $name = "make:database";
// in Laravel 5.3 + it's protected $signature
Then down below in your file we need a new function:
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the database'],
];
}
Then we'll make another function called fire() which will be called upon invocation of the command:
public function fire()
{
DB::getConnection()->statement('CREATE DATABASE :schema', ['schema' => $this->argument('name')]);
}
And now you can just do this:
php artisan make:database newdb
Now you'll get a newdb database created for you based on your connection configuration.
Edit Forgot the most important part - you need to tell app\Console\Commands\Kernel.php about your new comand, make sure to add it to the protected $commands[] array.
protected $commands = [
///...,
App\Console\Commands\CreateDatabase::class
];
This answer might be useful if you are using different mysql connection also. I am writing code in laravel 5.5
Step:1 Create command
php artisan make:command CreateDatabaseCommand
Step:2 In app/Console/Kernel.php register the command
protected $commands = [
CreateDatabaseCommand::class
];
Step:3 Write logic in your CreateDatabaseCommand.php file
protected $signature = 'make:database {dbname} {connection?}';
public function handle()
{
try{
$dbname = $this->argument('dbname');
$connection = $this->hasArgument('connection') && $this->argument('connection') ? $this->argument('connection'): DB::connection()->getPDO()->getAttribute(PDO::ATTR_DRIVER_NAME);
$hasDb = DB::connection($connection)->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = "."'".$dbname."'");
if(empty($hasDb)) {
DB::connection($connection)->select('CREATE DATABASE '. $dbname);
$this->info("Database '$dbname' created for '$connection' connection");
}
else {
$this->info("Database $dbname already exists for $connection connection");
}
}
catch (\Exception $e){
$this->error($e->getMessage());
}
}
That's all. Now run your command
php artisan make:database {your-database-name} {your-connection-name}:
Note :: You should use second argument only if you want to create database in any different connection from default mysql connection otherwise command will automatically take the default db connection
Hope this will help someone :)
If you're not going to use a Vagrant box or virtual machine for local development then you're going to have to install your database driver of choice and then create a new database.
To do that with MySQL from the command line run:
$ mysql -uroot -p
mysql> create database yourDatabaseName;
Then cp .env.example .env and update your database creds.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=yourDatabaseName
DB_USERNAME=root
DB_PASSWORD=root
You'll have set up the username and password to your database when you first installed the driver. After this you may have to php artisan key:generate and/or 'php artisan config:clearbutphp artisan migrate` should work. Below are some tutorial resources you may find helpful:
Initial database creation and seeding with Laravel 5
How to install MySQL on OSX
Vikash's response worked. (in Laravel 5.8)
Based on the response of Vikash:
How to create a mysql db with Laravel
I have created the following command which creates a MySQL database with the collation that you assign.
I have uploaded it to this GitHub repository
I hope it helps other developers.
You should create the DB, and set the connection parameters to it. Then, Laravel will access the DB and will run the migrations (make the tables) and the seeders.
You can found the parameters for php artisan migrate here: https://laravel.com/docs/5.2/migrations#running-migrations
You can create the database using Laravel Tinker. First configure the right DB_ settings in your .env file.
Then run the following commands:
php artisan tinker
$pdo = new PDO('mysql:host=' . env('DB_HOST'), env('DB_USERNAME'), env('DB_PASSWORD')));
$pdo->exec('CREATE DATABASE ' . env('DB_DATABASE'));
exit
As a one liner it looks like this:
php artisan tinker --execute="(new PDO('mysql:host=' . env('DB_HOST'), env('DB_USERNAME'), env('DB_PASSWORD')))->exec('CREATE DATABASE ' . env('DB_DATABASE'))"
you have to make your modal first and make its migration table also in order to make a database.
you should use: php artisan make:model -m command to make model and migration table. after that open your migration table in any texteditor to add the columns in your database table and then, use this command to migrate:
php artisan migrate

Resources