Laravel SQLSTATE[HY000] [1049] Unknown database + PHP artisan migrate - php

I have setup Laravel before and I have never encountered a Database unknown issue. I am trying to install the default Laravel migration on a new project.
I have already created the database on my server.
I am using MAMP Apache port:80 MYSQL Port: 3306
My env setup looks like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=thevinesa
DB_USERNAME=root
DB_PASSWORD=******
When I try to use PHP artisan migrate; I get an error of database unknown
SQLSTATE[HY000] [1049] Unknown database 'thevinesa' (SQL: select * from information_schema.tables where table_schema = thevinesa and table_name = migrations and table_type = 'BASE TABLE') at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
699▕ // If an exception occurs when attempting to run a query, we'll format the error
700▕ // message to include the bindings with SQL, which will make this exception a
701▕ // lot more helpful to the developer instead of just the database's errors.
702▕ catch (Exception $e) {
703▕ throw new QueryException(
704▕ $query, $this->prepareBindings($bindings), $e
705▕ );
706▕ }
707▕ }
+33 vendor frames
34 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
How can I solve this error

Step 1:
Fire this command inside your project
php artisan optimize:clear
Step 2:
Then start project if in local:
php artisan serve
Database is then connected.

Suggestion from #ThandoHlophe
*
I found the issue, so my ~bash directory had the incorrect $PATH, I updated the $PATH and I was able to sort out the issue
It's an simple error, you still did't created your database in mysql or phpmyadmin named thevinesa
DB_DATABASE=thevinesa
Laravel try to find out the database name thevinesa to connect but they didn't find and database named thevinesa . Try to create database.
Then migrate.
php artisan cache:clear
php artisan config:clear
php artisan migrate
command related to database
migrate:fresh Drop all tables and re-run all migrations
migrate:install Create the migration repository
migrate:refresh Reset and re-run all migrations
migrate:reset Rollback all database migrations
migrate:rollback Rollback the last database migration
migrate:status Show the status of each migratioin
Or
Laravel can't connect to your database.
Go to Storage->logs->laravel.log file you will find your error
or you can use php artisan migrate -v if you are in linux . It will show the error details.
And did you create that database?
Check if you have a file bootstrap\cache\config.php. If so remove it.
I didn't find any pdoException Error but If you changed the
`config->database.php` file
Here is my config->databse.php try to compare these
This code taken from Laravel Framework 7.30.4, It might be different but you can have a basic idea.
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
If you want to check you version
php artisan -v
May be error will happens by artisan. For now this is my artisan from Laravel Framework 7.30.4 and php 7.2 look it if you didn't find anything suspecious, didn't change your artisan file take a copy oe backup. And soon as possible show me your error log.
#!/usr/bin/env php
<?php
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Artisan Application
|--------------------------------------------------------------------------
|
| When we run the console application, the current CLI command will be
| executed in this console and the response sent back to a terminal
| or another output device for the developers. Here goes nothing!
|
*/
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);
/*
|--------------------------------------------------------------------------
| Shutdown The Application
|--------------------------------------------------------------------------
|
| Once Artisan has finished running, we will fire off the shutdown events
| so that any final work may be done by the application before we shut
| down the process. This is the last thing to happen to the request.
|
*/
$kernel->terminate($input, $status);
exit($status);

If you're on a MAC using MAMP, adding this to my .env file worked for me
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

Related

php artisan migrate error | Illuminate\Database\QueryException | could not find driver | Windows 10 Pro | MAMP PRO 5 + Laravel 9.x

Hello, everyone! I want to ask a question about error I get with command php artisan migrate.
So here's the error itself:
Illuminate\Database\QueryException
could not find driver (SQL: select * from information_schema.tables where table_schema = laravel_app_db and table_name = migrations and table_type = 'BASE TABLE')
at C:\MAMP\htdocs\laravel_app\vendor\laravel\framework\src\Illuminate\Database\Connection.php:759
755▕ // If an exception occurs when attempting to run a query, we'll format the error
756▕ // message to include the bindings with SQL, which will make this exception a
757▕ // lot more helpful to the developer instead of just the database's errors.
758▕ catch (Exception $e) {
➜ 759▕ throw new QueryException(
760▕ $query, $this->prepareBindings($bindings), $e
761▕ );
762▕ }
763▕ }
1 C:\MAMP\htdocs\laravel_app\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDOException::("could not find driver")
2 C:\MAMP\htdocs\laravel_app\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDO::__construct("mysql:host=127.0.0.1;port=8889;dbname=laravel_app_db", "root", "root", [])
I'm on Windows 10 Pro and I got PHP version 8.1.0 and MAMP PRO 5.
MySQL settings in MAMP PRO 5:
Ports settings in MAMP PRO 5:
Created database in phpMyAdmin:
I was watching the 3 hour Laravel tutorial and got stuck on the moment, where the guy was migrating tables by php artisan migrate command. I successfully installed Laravel and created a bunch of pages there.
Here's what I did trying to solve the problem:
enabled php_openssl.dll, extension=php_mysqli.dll and extension=php_pdo_mysql.dll extensions in php.ini file from this path C:\MAMP\bin\php\php8.1.0
changed DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME and DB_USERNAME in .env file
edited AppServiceProvider.php file like you will see down below
edited database.php file like you will see below AppServiceProvider.php file
Literally NOTHING happens, I still get the error! Maybe I have wrong PHP version. Maybe this guy uses XAMPP local web-server instead of MAMP PRO 5. I really don't know what to do.
AppServiceProvider.php file:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema; //<---- added this line
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191); //<---- added this line
}
}
MySQL properties from database.php file:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '8889'),
'database' => env('DB_DATABASE', 'laravel_app_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Additional info:
Needed extensions from php.ini file:
extension=php_bz2.dll
extension=php_gd.dll
;extension=php_gd2.dll
extension=php_gettext.dll
extension=php_mbstring.dll
extension=php_exif.dll
extension=php_mysqli.dll
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
extension=php_curl.dll
extension=php_openssl.dll
extension=php_imagick.dll
extension=php_pdo_mysql.dll
;extension=php_apc.dll
;extension=php_apcu.dll
;extension=php_eaccelerator.dll
;extension=php_xcache.dll
;zend_extension=php_opcache.dll
Database properties from .env file:
APP_NAME=laravel_app
APP_ENV=local
APP_KEY=base64:TIlPzLirZ+9Jw6Am0m4XDEodGxmbc8wXSAKQ0XDb5fU=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=laravel_app_db
DB_USERNAME=root
DB_PASSWORD=root
So, I fixed this.
I read these posts:
First post to read
Second post to read
What did I do:
I changed 544 line in php.ini from this extension_dir = "C:\MAMP\bin\php\php5.5.0\ext\" to this extension_dir = "C:\MAMP\bin\php\php8.1.0\ext"
Then I got the error from the second post SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
I solved the second error by running this command php artisan config:clear
And then I finally got successful message from cmd

SQLSTATE[HY000] [1049] Unknown database 'laravel'

I am getting this error while trying to save an object into DB.
SQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: insert into cards (card_price, active, updated_at, created_at) values (0, 1, 2019-10-10 15:14:43, 2019-10-10 15:14:43))
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=cardgame
DB_USERNAME=root
DB_PASSWORD=P#assword1!
Database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_T_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
CardController.php
public function generateCards()
{
$card = new Card();
$card->card_price = 0;
$card->active = 1;
$card->save();
}
Web.php
Route::get('/generate-cards', 'CardController#generateCards');
Card.php
class Card extends Model
{
protected $guarded =[];
}
Migration file
public function up()
{
Schema::create('cards', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('card_price');
$table->integer('active');
$table->timestamps();
});
}
I've tried clearing cache and also have edited DB_PASSWORD To DB_T_PASSWORD as this corrected a similar issue earlier. Double checked the DB name, passwords etc & also other projects are already running also. I'm not able to figure it out.
Go to your localhost server and drop the database and recreate a new one named cardgame
Go to your laravel project in console and run php artisan config:cache command. That way all your env variables will be used.
Run php artisan:migrate to run your migrations for your database and create your tables.
If you do the above 3 steps in that order you should be fine.
You must clear the cache because your old configuration is in the cache file, just run this command in your terminal for clear cache:
php artisan config:cache
You can solve this problem by 2 simple step.
Step:1 php artisan config:cache
step:2 php artisan migrate
I had the same problem and I solved it following these steps:
1) Stop the server
2) Run php artisan config:clear .This command line will remove
bootstrap\cache\config.php file
3) Start the server and It should work fine now
create database in php admin then migrate your database again
php artisan migrate , it's work 100%
I was also experiencing the same error and implimented all the solutions mentioned above but the error kept reoccuring. But I edited the.env and env.example file where the database = "laravel" to the name of my project for instance transportsytem and it worked.The best solution to the problem is editing both .env and env.example files with the name of your database that you created .

Laravel: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

after installing a new laravel app 5.7 and trying to migrate I get this error:
Illuminate\Database\QueryException : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = xxx_db and table_name = migrations)
at
C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll
format the error 661| // message to include the bindings with SQL,
which will make this exception a 662| // lot more helpful to the
developer instead of just the database's errors. 663| catch (Exception
$e) {
664| throw new QueryException( 665| $query,
$this->prepareBindings($bindings), $e 666| ); 667| } 668|
Exception trace:
1 PDOException::("PDO::__construct(): The server requested
authentication method unknown to the client [caching_sha2_password]")
C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=xxx_db ",
"root", "**********", [])
C:\xampp\htdocs\xxxxx\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
Please use the argument -v to see more details.
This query solved my problem.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root#123';
Enter to your mysql as root and run this query
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '1234';
You can change 1234 to be your password
Run these commands
php artisan config:clear
php artisan migrate
Note: this worked for me.
Re installed MySQL choosing Legacy Authentication Method as shown in iamge attached SQL Authentication method
Database parameters in .env as follows
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password(if any)
Database parameters in config/database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_name'),
'username' => env('DB_USERNAME', 'database_username'),
'password' => env('DB_PASSWORD', 'database_password'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Run php artisan migrate from the projects terminal
By Providing DB_SOCKET in .env file this issue can be resolved like this :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Please Check Server localhost Port on phpmyadmin and .env file like (3306,3307,8889)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=masterpass
Solution is here with 2 step:
Step 1. You have to edit /etc/mysql/my.cnf file and append this setting in mysqld section:
[mysqld]
default_authentication_plugin= mysql_native_password
Step 2. Then run following mysql command:
CREATE USER 'new_root'#'localhost' IDENTIFIED WITH mysql_native_password BY '123';
GRANT ALL PRIVILEGES ON *.* TO 'new_root'#'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
It is better if you restart your mysql service.
sudo systemctl restart mysql
This problem happens when you have install mysql8 and your code compatible with mysql5
You can run Mysql installer - Community (if you are in windows) and then reconfigure mysql server to use legacy authentication method. it should be solve your problem without pain.
Unfortunetly you changes your mysql user or password. If you user/password changed then go to .env file and change your phpmyadmin user and password. Give your database name
DB_DATABASE=hrms
DB_USERNAME=username
DB_PASSWORD=password
If you are getting through this kind of issue like me, avoid losing your time
My scenario: I had to start an existing Laravel5.1 application on PHP5.6. Docker didn't helped me, so I had to create a virtual machine for that.
This issue happens because of the authentication driver for MySQL. On version 8, it will use caching_sha2_password (and Laravel 5 doesn't know what to do with this.
One way to correct this is to downgrade your MySQL version back to 5.7. I've tried a lot, but my apt crashed after "forcing" it to downloading 5.7. If you want to try, heres a discussion for it.
95% of other discussions on StackOverflow and other forums will say to you just ALTER your MySQL user for the mysql_native_password. For some reason, it DOES NOT work. Even after restarting mysql services and flushing it's privileges, doesn't work. I've tried this kind of solutions for hours.
Luckily we have a hero in THIS discussion and is #Amir Hosseinzadeh. You have to create a new user AFTER changing the MySQL driver configuration. Just follow his/her solution on top and it will work.
Go to your .env file and make sure DB_CONNECTION=mysqland DB connections are correct.

I'm using Laravel 5 and I'm not being able to migrate my database using php artisan migrate

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.

Laravel SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'

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.

Categories