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 .
Related
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
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'm trying to get started with Laravel + PostgreSQL and been following the database tutorial.
Unfortunately, after updating the database configuration file and running php artisan migrate, the following error appears:
[InvalidArgumentException]
Database [postgres] not configured.
What puzzles me is that I didn't specify the "postgres" database in the configuration, but another database I set through cPanel, say "example_database".
Here's some relevant parts of my /config/database.php configuration:
'default' => env('DB_CONNECTION', 'postgres')
And inside the connections array of the same file:
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'example_database'), // This seems to be ignored
'username' => env('DB_USERNAME', 'example_username'),
'password' => env('DB_PASSWORD', 'example_password'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public'
],
The actual database credentials I'm using are working perfectly on my SQL Workbench client, so this seems to be a Laravel config problem. Any ideas? I have searched around for at least an hour to no avail.
You have to enter your configuration in the .env file.
The configuration you made will only be loaded if they are not already defined in .env
You need to use pgsql instead of postgres.
DB_CONNECTION=pgsql
DB_HOST=localhost
DB_DATABASE=DB_NAME
DB_USERNAME=USER
DB_PASSWORD=PW
Laravel sometimes caches your configurations. If you run into this problem while everything looks alright try running
php artisan config:cache
I know this is an old question and it already has an answer; however, here is an small explanation why:
If you check your database.php in the config directory, you will see that you have few connections types, including pgsql. So, the key have to match to the DB_CONNECTION in .env file. You can definitely replace pqsql connection key with postgres, and it will work on the same way.
However, I would recommend replacing the value DB_CONNECTION, instead of modifying the config.
DB_CONNECTION=pgsql
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.
My website works, its clearly accessing the database. I can log in and i can create records, etc...
Ive created a new migration, and I'm trying to insert it, but when i run
php artisan migrate
i get the error
[PDOException]
SQLSTATE[HY000] [2002] Connection Refused
My database config has the passwords hidden in the environment so my config looks like this
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOSTNAME'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
my environment variables are loading correctly. What am i missing?
I figured it out. Apparently the environment that artisan was seeing was different from the environment i wanted it to see, so it was using the wrong host
php artisan migrate --env=production