How to join tables on different databases from different servers - laravel

[Laravel 6]
I'm trying to join 2 tables from different server databases.
Here's the database configuration :
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'db1'),
'username' => env('DB_USERNAME', 'table1'),
'password' => env('DB_PASSWORD', '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'),
]) : [],
],
'external' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('EXTERNAL_HOST', '123.456.789.2'),
'port' => env('EXTERNAL_PORT', '3306'),
'database' => env('EXTERNAL_DATABASE', 'db2'),
'username' => env('EXTERNAL_USERNAME', 'table2'),
'password' => env('EXTERNAL_PASSWORD', 'password'),
'unix_socket' => env('EXTERNAL_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'),
]) : [],
],
Here's the query :
return $query->join('db2.table2', function($join) {
$join->on('table1.gateway', '=', 'db2.table2.gateway');
$join->on('table1.prefixlen', '=', 'db2.table2.prefixlen');
$join->on('table1.vlan', '=', 'db2.table2.vlan');
})->select('table1.*');
But I received this error message :
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db2.table2' doesn't exist (SQL: select count(*) as aggregate from table1 inner join db2.table2 on table1.gateway = db2.table2.gateway and table1.prefixlen = db2.table2.prefixlen and table1.vlan = db2.table2.vlan)
I've tried googling around in the last couple days, but I couldn't figure out the right way to do it.
Any clues would be much appreciated.

Related

Laravel Migration is not creating the table in different database

I have this migration file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::connection('DB1')->create('feed_mappings', function (Blueprint $table) {
$table->bigIncrements('id_feed')->autoIncrement();
$table->string('id_project', 255);
$table->string('import_field_slug', 155);
$table->string('internal_field_slug', 155);
$table->tinyInteger('import');
$table->tinyInteger('custom_field')->default(0);
$table->dateTime('date_add');
$table->dateTime('date_upd');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('feed_mappings');
}
};
And I have this config in the database.php file:
'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' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'DB1' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '8889'),
'database' => env('DB_DATABASE', 'DB1'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Now, I have run this command :
php artisan migrate:refresh --seed
Now, All the migration file is creating a tables on default database but I want this feed_mappings should go to DB1 database.
BUT this feed_mapping is not going to DB1 database. Can you tell me why?
So the issue is in your database.php file where you are using the same variables for connections 1 and 2
'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' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'DB1' => [
'driver' => 'mysql',
**'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '8889'),
'database' => env('DB_DATABASE', 'DB1'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),**
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => 'InnoDB',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
what you need to do is create separate variables for db connections e.g:
DB_HOST // for connection 1
DB_HOST_2 // for connection 2
and use these in your config files for more info visit: https://codelapan.com/post/how-to-use-multiple-database-connections-in-laravel-8
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', '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'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST_2', '127.0.0.1'),
'port' => env('DB_PORT_2', '3306'),
'database' => env('DB_DATABASE_2', 'forge'),
'username' => env('DB_USERNAME_2', 'forge'),
'password' => env('DB_PASSWORD_2', ''),
'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'),
]) : [],
],
Setup .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_multiple_database1
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION_2=mysql
DB_HOST_2=127.0.0.1
DB_PORT_2=3306
DB_DATABASE_2=laravel_multiple_database2
DB_USERNAME_2=root
DB_PASSWORD_2=
in Model example Post
protected $connection = 'mysql2';
in migration file
public function up()
{
Schema::connection('mysql2')->create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->timestamps();
});
}
public function down()
{
Schema::connection('mysql2')->dropIfExists('posts');
}

SSL Certificate for SingleStore managed instance on Laravel Vapor Environment

First time setting up SingleStore with Laravel Vapor on AWS.
I am stuck as to where / how to point the PDO connection to use the SSL / pem from SingleStore. I have the pem deploying to the app root.
My DB config is:
'singlestore' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3307'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'prefix_indexes' => true,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
PDO::ATTR_EMULATE_PREPARES => true
]) : [],
],
And in the Vapor environment I have tried:
MYSQL_ATTR_SSL_CA=singlestore_bundel.pem
MYSQL_ATTR_SSL_CA=~/singlestore_bundel.pem
MYSQL_ATTR_SSL_CA=~/public/singlestore_bundel.pem
How do I point the PDO connection to the pem?
Thanks
Jack Ellis helped me out on how he does it with Fathom.
In the database config file, for SingleStore:
'options' => extension_loaded('pdo_mysql') && env('APP_ENV') !== 'testing' && env('APP_ENV') !== 'local' ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => resource_path('singlestore/singlestore_bundle.pem'),
PDO::ATTR_EMULATE_PREPARES => true,
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => true,
PDO::ATTR_PERSISTENT => (env('IS_HEROKU') == 'yes')
]) : [],

Import databases infos from mysql in database.php [duplicate]

This question already has answers here:
Laravel: connect to databases dynamically
(8 answers)
Closed 3 years ago.
I would like to get databases informations stored in my database and add it in the database.php.
I tried to get all infos in the database but I always have an error like this :
[2019-04-18 20:22:20] laravel.ERROR: Call to a member function
connection() on null {"exception":"[object]
(Symfony\Component\Debug\Exception\FatalThrowableError(code: 0):
Call to a member function connection() on null at
/var/www/vhosts/xxxx.net/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1249)
I added this on the top of database.php :
<?php
use App\Campaign;
$database_infos = Campaign::all();
$campains_db = array();
foreach ($database_infos as $info) {
if (isset($info->db_name)) {
$campains_db[$info->keyword] = array(
'driver' => 'mysql',
'host' => $info->db_host,
'database' => $info->db_name,
'username' => $info->db_user,
'password' => decrypt($info->db_password),
'prefix' => $info->db_prefix,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'strict' => false,
'engine' => null,
);
}
}
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
$campains_db,
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'wordpress' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
'migrations' => 'migrations',
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
];
I found this on an other post :
The simplest solution is to set your database config at runtime.
Laravel might expect these settings to be loaded from the
config/database.php file, but that doesn't mean you can't set or
change them later on.
The config loaded from config/database.php is stored as database in
Laravel config. Meaning, the connections array inside
config/database.php is stored at database.connections.
Config::set("database.connections.mysql", [
"host" => "...",
"database" => "...",
"username" => "...",
"password" => "..."
]);

Laravel 5 using 2 db connections join 2 tables

How can I join 2 tables which are located on 2 different servers.
I setup db configs:
'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' => false,
'engine' => null,
'options' => [
\PDO::ATTR_EMULATE_PREPARES => true
]
],
'mysql2' => [
'driver' => 'mysql',
'host' => '132.133.22.9',
'port' => '3306',
'database' => 'contracts',
'username' => 'asdsdsa',
'password' => 'asdsad',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
'options' => [
\PDO::ATTR_EMULATE_PREPARES => true
]
],
I'm able to access only one like DB::connection('mysql')->table('elements')->get();
How can I use leftJoin for two tables on two different servers?
lets say I have:
elements (server1):
id, class_id
classes (server2)
id, name
You can use
database one (test) include elements table and database two (test2) include classes table .
$result= DB::table('elements')
->leftjoin('test2.classes', 'elements.class_id', '=', 'classes.id')
->get();
dd($result);
you don't really need to specify first DB connection. It's by default.
test it :
for exammple i have two databases and one connection :
database one (test) include elements table and database two (test2) include classes table .
$result= DB::connection('mysql')->table('elements')
->join('test2.classes', 'elements.class_id', '=', 'classes.id')
->get();
dd($result);

How to properly configured Multi-tenant in laravel?

I have created multi-tenant application, still now that working as expected.
Here is my sample code:
middleware :
public function handle($request, Closure $next)
{
$user = Auth::user();
if (! is_null($user)) {
Config::set('database.connections.tenant.host', $user->hostname);
Config::set('database.connections.tenant.database', $user->database);
Config::set('database.connections.tenant.username', $user->username);
Config::set('database.connections.tenant.password', $user->password);
}
return $next($request);
}
database.php :
'connections' => [
'main' => [
'driver' => env('DB_DRIVER', 'mysql'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', '123'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'tenant' => [
'driver' => env('DB_DRIVER', 'mysql'),
'host' => '',
'port' => env('DB_PORT', '3306'),
'database' => '',
'username' => '',
'password' => '',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
My doubt is : Above code is is possible to collapse DB connection, When multiple user access same time ? (each user has different DB)
for example :user A has school database and user B has college database
user A is try to logging [loading the page] that situation user B is login.
user B has no problem but user A -- get student details from college database or student database?

Resources