I got Laravel SQLSTATE[HY000] [1049] Unknown database Error, when I try to migrate using php artisan migrate:install/ migrate. I have properly named database on my localhost SQL server. I'm using XAMPP to run it.
here's my .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=todo
DB_USERNAME=root
DB_PASSWORD=root
here's database.php:
'default' => env('DB_CONNECTION', 'mysql'),
'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'),
]) : [],
],
also here's create_todo_table.php file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTodoTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('todo', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('yettodo');
$table->string('done');
$table->string('user');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('todo');
}
}
but constantly I keep getting this:
C:\Users\gracj\todo\todo\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDOException::("SQLSTATE[HY000] [1049] Unknown database 'todo'")
I am fully aware that there are plenty of threads like this on the Internet. The case is I feel like I've tried everything I could find (including installing different MySQL version - now I'm on 5.7, earlier I was on 8.0). I feel hopeless.
I had to change DB_PORT to 3307 since I had "3306 is occupied" error. I'm on Laravel 8 and PHP 8.0.2.
Please help!
Ok, fixed - very counterintuitive. Here's what I did.
I deleted and created todo database on my server couple of times, every time "php artisan config:clear" afterwards. I did try it earlier, BUT, I changed the DB_PORT in my .env file back to 3306 EVEN TOUGH XAMPP says it's busy, so in XAMPP I had to change it to 3307 (image below). As you can see, these aren't the same! Could you explain that? I'm worried since it might work just once and after few lines of code or migrations, it is possible for it to come back...
Faced a similar issue. However, mine was solved after creating a table on myphpAdmin.
Try:
open XAMP->click on admin in MySQL row and create database as stated in your DB_DATABASE=
try running php artisan migrate again.
First set your Laravel .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=todo
DB_USERNAME=forge
DB_PASSWORD=forge
if so then run:
php artisan config:clear
Related
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
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 .
I checked my config/database.php
I have:
'default' => env('DB_CONNECTION', 'mysql'),
and in my connection I have:
'mysql' => [
'driver' => 'mysql',
'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' => '',
'strict' => true,
'engine' => null,
but it seems that my DB configuration areh happening somewhere elese, I also defined my varioables in my .env file. But when I run the migrate in my app directory, it errors out :
[Illuminate\Database\QueryException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name
'usernam e' (SQL: alter table users add username varchar(191)
null)
although I have already taken the migrate file for users out (renamed the php extension of it)
Any help will be appreciated.
edit: does thi happen that migration files are stored somewhere other than /database/migrations ?
The error duplicate column name 'username' indicates the database has a table called users which has the column username in.
What seemed to happen: you took out the migration file out of Laravel before you did php artisan migrate:rollback. This left the database with all the schema structure that the migration wrote out. So obviously you can't run the same migration again because the same structure is already found in the database.
What you should do: log into your mysql server, drop, and create the database again.
mysql -u root -p then it will prompt you for the password of your mysql server.
drop database your-db-name
create database your-db-name
Then run the migrations again, php artisan migrate.
Further diagnose method
You can create a file 'database.sqlite' under the /database/ folder and change your .env's DB_CONNECTION to sqlite.
Run migrations and rollback and run it again. If it messes up at any point, it means the migration files have got a problem in them.
I have a search function for my database but sometimes I get this message:
[2016-02-04 07:03:18] local.ERROR: PDOException: SQLSTATE[HY000] [1044] Access denied for user ''#'localhost' to database 'forge' in C:\xampp\htdocs\reko\api\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:55
Stack trace:
#0 C:\xampp\htdocs\reko\api\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php(55): PDO->__construct('mysql:host=loca...', 'forge', '', Array)
...
In one of ten calls I get this 500 error message, but I don't know why. The other calls give the right result.
.env
APP_ENV=local
APP_DEBUG=true
APP_KEY=bJM6O0MnrIPaTNwKKOqNJkGinRDv1fnc
DB_HOST=localhost
DB_DATABASE=reko
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
Search function:
public function search(Modul $modul, Request $request)
{
$question = Question::whereModulId($modul->id)
->where('value', 'LIKE', '%' . $request->get('keywords') . '%')
->with('tags')
->whereHas('exams', function ($query) use ($request) {
$query->where('date', '>=', $request->get('year').'-01-01');
});
if (!$request->get('parent'))
$question->where('type', '<>', 'parent');
if (!$request->get('own'))
$question->where('type', '<>', 'own');
if (!$request->get('normal'))
$question->where('type', '<>', 'normal');
if ($request->get('answered'))
$question->has('answers');
return $question->paginate(10);
}
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I haven't modified the database.php file and all other calls work great.
PLEASE RUN THIS COMMAND
php artisan cache:clear
THEN
php artisan config:cache
This seems to hold most of the answers : Laravel 5.2 not reading env file
Remember to not to edit your core files as one of the users said.
Just do the safe checks, clear cache, should work.
Hope it helps.
As a workaround you can add to your DB forge account with all privileges.
Try php artisan clear:cache to clear your cache and re-configure your cache php artisan config:cache it might works for you .
I had the same issue once. what I discovered was that my default connection was set to PostgreSQL instead of MySQL so I changed my default connection in database.php
'default' => env('DB_CONNECTION', 'mysql'),
other wise do add following to your .env
DB_CONNECTION=mysql
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=
try adding remaining methods in your .env file
Too often we have to face this error when hosting.
You should see that you have given Privileged Users permission to the database in the hosting.
First
First I need to see if the database name is correct.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=zhara_demo
DB_USERNAME=zhara_admin
DB_PASSWORD=zhara#2021
or
Try php artisan clear:cache to clear your cache and re-configure
your cache php artisan config:cache it might works for you .
I am using schema builder to create a Mysql table. I am using Ubuntu 14.04.
My connection for Mysql in config/database.php is :
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'firstapp'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
And Code for creating table with schema builder on routes.php
Route::get('/', function () {
Schema::create('art', function($newtable)
{
$newtable->increments('id');
$newtable->string('artists');
$newtable->string('title',500);
$newtable->text('discription');
$newtable->date('created');
$newtable->date('exhibition_date');
$newtable->timestamps();
});
return view('welcome');
});
Thanks
As far a as i know , you must create a migration file using
php artisan migrate:make yourtable
Then yo must go to database folder you will find a new migration file created , then you must open it , and fill it with your schema , once you finish , then run migration
php artisan migrate
And that's all you will see your new table created.
Migrations laravel 5
First i was using xampp mysql and this was also started /etc/init.d/mysql from terminal, Since i am using Ubuntu 14.04.
I solved the problem which i posted above then another problem came up Saying SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES).
I changed the .env file which is placed in the root directory of laravel and i changed the Database Name name and password which i was using for mysql.
It solved that problem too and i am up and running.