I fairly new to laravel and a beginner in monogo db. I have been trying to connect mongodb cluster of mongodb atlas in my laravel project.
But when I am trying to migrate the laravel migration file it is showing error saying mysql error even after I changed the default connection to mongodb.
Can anyone please tell me how can I fix this issue and migrate the current project to mongodb?
PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead", "homestead", "secret", [])
1 PDOException::("SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
")
C:\Users\admin\Desktop\test\test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 68
C:\Users\admin\Desktop\test\test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 68
Since laravel doesnt allow mongodb out of the box, so I am using a mongodb package https://github.com/jenssegers/laravel-mongodb
And I also would like to mention that I have monngodb installed in my php as per the documentation. I can see the confirmation of mongodb on phpinfo() page. My settings are as follows:
My .env
DB_CONNECTION="mongodb"
DB_MONGO_PORT=27017
DB_MONGO_DATABASE=test
DB_MONGO_DSN="mongodb+srv://<USERNAME>:<PASSWORD>#cluster0-
***.mongodb.net/test"
My config/database.php
'default' => env('DB_CONNECTION', 'mongodb'),
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_MONGO_DSN'),
'database' => env('DB_MONGO_DATABASE'),
],
My user migration file
use Illuminate\Support\Facades\Schema;
//use Illuminate\Database\Schema\Blueprint;
use Jenssegers\Mongodb\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
protected $connection = 'mongodb';
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
I'm not sure why it's not working with the default connection set to mongodb, but I've ran into this problem before. The issue I was having is that the connection property in the migration is useless. I had to do the following;
Schema::connection($this->connection)->create('collections', function (Blueprint $table) {
$table->increments('id');
$table->index('slug');
$table->index('world_id');
$table->unique(['world_id', 'slug']);
$table->timestamps();
});
Schema::connection('mongodb') should work for you.
put DB_CONNECTION=mongodb on your .ENV file.
Also remove quotes from MOGN0_DSN and make sure to call php artisan config:cache to dump the autoloaded configuration file
Try this package.
It's easy to use.
https://packagist.org/packages/jenssegers/mongodb
Try .env file as
DB_CONNECTION=mongodb
without qoutes
Related
I started a new project using laravel^6.2 using the laravel new myapp command.
I edit then my .env file to connect to my local database:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8082
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=root
I decide then to migrate the default database structure from laravel. when I say default structure I'm talking about the users migration which is already created from the beginning present in the 2014_10_12_000000_create_users_table.php file:
<?php
// *** This has been generated from `laravel new myapp` ***
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
So I run the php artisan migrate command and then something unexpected happens. Indeed, after that the terminal doesn't do anything as if it was on break. I'm thinking maybe the command takes a while to execute but after several tries that lasted about ten minutes, nothing happens.
So I decide to consult the logs but absolutely nothing's written down, it's totally empty.
So I'm trying to execute php artisan cache:clear and start over but the result is the same: No error messages and nothing happens.
I know the question has been asked here before, but no answer really came up.
Has somebody an idea what I have missed ?
Try changing the port to the default MySQL port 3306.
Port 8082 as you have it sounds unusual, unless you have set that port in your MySQL server.
From your comment:
there are multiple "localhost" servers running on your machine:
your localhost web server, port 80 usually (in your case 8082)
your localhost mysql server, port 3306 usually.
There might also be a localhost postgres server, running (default) at port 5432, a localhost redis server at port 6379 or many others.
I'm trying to use artisan migrate to create tables in sqlite.
I have the following in database.php
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
and this is my migrate class up function
Schema::connection('sqlite')->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Note:- I have set DB_CONNECTION to be sqlite via the environment variable.
On the command line I get nothing to migrate, and no db is created (also no table).
Any ideas how I can get artisan migrate to create an sqlite db in laravel 5?
I have no problem creating mysql tables via artisan.
Thanks
its because you didnt create the db. use touch command like this to create new sqlite database.
touch database/database.sqlite
then configure the environment variable like this
DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database.sqlite
In order to answer this part of the OP's question:
"Any ideas how I can get artisan migrate to create an sqlite db in laravel 5?"
In some cases where deployment and migrations need to be scripted I use a Service Provider to check the Sqlite DB exists and if not create it.
Here are the steps:
1) Create a Provider and save it to app/Providers/SqliteServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class SqliteServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
$databaseFile = config('database.connections.sqlite.database');
if (!file_exists($databaseFile)) {
info('Make Sqlite File "' . $databaseFile . '"');
file_put_contents($databaseFile, '');
}
}
}
2) Register the provider in config/app.php. For example in a fresh L5.8 install the SqliteServiceProvider would be placed under the RouteServiceProvider like so:
'providers' => [
//...
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\SqliteServiceProvider::class, // <--- HERE
],
3) Add/replace the following to the 'connections' element of config/database.php
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_SQLITE_FILEPATH', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
4) In my .env file I add this:
DB_SQLITE_FILEPATH=/full/path/to/mysqlitefilename.sqlite
...but if you prefer you can skip this step and the DB will be created as database/database.sqlite.
5) Run your migration as usual and the sqlite file should be created then populated.
Ok I got it. artisan migrate seems to ignore the env DB_CONNECTION and the database.php default (which reads the env anyway). Instead it like the .env file DB_CONNECTION, with this set to sqlite it works (plus, as mentioned above you do need to manually create the database file, which is a pain for making dbs on the fly.
Also means you can't really change artisan migrate database types (ie: sqlite and mysql) on the fly as you need to change the .env variable each time you change the target db type.
go laravel.
That's right, you need to create empty database.sqlite file.
You should also use artisan --env parameter if you want artisan to load the concrete .env file. For example, artisan --env=testing migrate will force artisan to load .env.testing file.
I use Laravel 5.2 and want to change default user table's columns. I wrote
in the CreateUsersTable
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function ($table) {
$table->string('role');
$table->string('info');
$table->string('age');
$table->string('hobies');
$table->string('job');
});
}
and i run these commands in the git bash but user table didn't change.
php artisan migrate:refresh
php artisan migrate
How can i solve it?
You should run composer dumpauto -o after running php artisan migrate command to register new migrations, so they could be rolled back.
Try it. If it will not work, try to delete all tables, including migrations and run this command:
php artisan migrate:install
create new migration file and do something like this
public function up() {
DB::statement('ALTER TABLE user ADD some_field INT');// your query here
}
php artisan migrate
You only need to change what it inside of Schema::create and delete your Schema::table
Then, you need to implement tour down() mehod and add Schema::drop('users'); in order to drop your table when you run migrate:refresh.
You can read the 'Database migrations' in the laravel documentation for further details.
delete the first statement and leave last one only .
then run php artisan migrate:refresh
I tried to implement the default auth login system in laravel
php artisan make:migration create_users_table
Created Migration: 2016_03_10_115611_create_users_table
On running the above command only migration file is created and not the tables are created.
Also i have tried to reinstall composer but it didn't work
//In 2016_03_10_115611_create_users_table file
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
on running
php artisan migrate
it took a few amounts of time and returned
[ErrorException]PDO::__construct(): MySQL server has gone away
Thanks to everyone i have not configured the .env file properly so that it doesn't work
What's your MySQL version?
Or you not yet modify .env
Try SQLite and change settings database to sqlite. I think this problem on php extension.
First you have run this command to install migration
php artisan migrate:install
Note :- if you look at database/migrations there will be already two migrations files
2014_10_12_000000_create_users_table.php
2014_10_12_100000_create_password_resets_table.php
php artisan migrate
I have a fairly simple migration that produces the following error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Call to undefined method Illuminate\Support\Collection::pluck()
I can rollback but I cannot migrate up. I'm using "laravel/framework": "5.1.*#dev", and this is the migration:
public function up()
{
Schema::table('tutorials', function (Blueprint $table) {
$table->string('url');
$table->string('title');
$table->text('description');
$table->string('image');
});
}
The pluck method is defined in Illuminate\Support\Collection so I'm sort of at a loss. I've run composer dump, as well. Any ideas?
Consider using laravel version "5.1.*" which will evaluate to 5.1.4
This is because #dev is constantly under development and not all of its functional may be in place. Today one thing will work while the next day another fails. Recommendation is to use a stable version then run the migrations to see if they work.