I am working on a cms project. I was created a portal and thenn new database connection created. So,there is a table users i wan that if someone create new user in that portal then it will be added to the master database that is another database. So, in laravel how can we make connection with another database and save the data of the portal to our master database table also.
Trying to make a sync connection that if in cms portal if admin create a user then that data must be submitted to the master table.
You can do it like this:
In .env add the following: note that you can customize your connection to another database, like postgres/mariadb etc.. check the documentation here for supported databases: https://laravel.com/docs/9.x/database#introduction
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=user
DB_PASSWORD=password
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=localhost
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=db2
DB_USERNAME_SECOND=user2
DB_PASSWORD_SECOND=password2
In config/database.php: note that you can customize your connection to another database, like postgres/mariadb etc.. check the documentation here for supported databases: https://laravel.com/docs/9.x/database#introduction
'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_SECOND', '127.0.0.1'),
'port' => env('DB_PORT_SECOND', '3306'),
'database' => env('DB_DATABASE_SECOND', 'forge'),
'username' => env('DB_USERNAME_SECOND', 'forge'),
'password' => env('DB_PASSWORD_SECOND', ''),
'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'),
]) : [],
],
In the sample model:
class Job extends Model
{
protected $connection = 'mysql2';
}
Related
I want to sync my local database to online database when i click on a button in blade page. I looked online for the solution but did not found anything that worked for me.
I have updated my database .php file I have added local and server database connection credentials.
I have found a solution but that is not completed.
solution that i found
database.php
'local' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('LOCAL_DB_HOST', '127.0.0.1'),
'port' => env('LOCAL_DB_PORT', '3306'),
'database' => env('LOCAL_DB_DATABASE', 'forge'),
'username' => env('LOCAL_DB_USERNAME', 'forge'),
'password' => env('LOCAL_DB_PASSWORD', ''),
'unix_socket' => env('LOCAL_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'),
]) : [],
],
'server' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('SERVER_DB_HOST', '127.0.0.1'),
'port' => env('SERVER_DB_PORT', '3306'),
'database' => env('SERVER_DB_DATABASE', 'forge'),
'username' => env('SERVER_DB_USERNAME', 'forge'),
'password' => env('SERVER_DB_PASSWORD', ''),
'unix_socket' => env('SERVER_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'),
]) : [],
],
controller code
// Connect to local database
return $local_database = DB::connection('local-db')->table('table_name');
// Get table data from production
foreach ($local_database->table('table_name')->get() as $data) {
// Save data to staging database - default db connection
DB::table('table_name')->insert((array) $data);
// This query can do only one time for multiple time we have to do
DB::table('table_name')->insertOrUpdate((array) $data);
// This query also update column only, if you do some changes in
//previous data it will not sync for that i dont know this query is
//correct or not but it worked.
DB::table('table_name')->delete((array) $data);
DB::table('table_name')->insertOrIgnore((array) $data);
}
env
SERVER_DB_CONNECTION=server
SERVER_DB_HOST=127.0.0.1
SERVER_DB_PORT=3306
SERVER_DB_DATABASE=sync_db
SERVER_DB_USERNAME=root
SERVER_DB_PASSWORD=
LOCAL_DB_CONNECTION=local
LOCAL_DB_HOST=127.0.0.1
LOCAL_DB_PORT=3306
LOCAL_DB_DATABASE=sync_db
LOCAL_DB_USERNAME=root
LOCAL_DB_PASSWORD=
I am developing a laravel application. I connect my laravel application with a mysql database. But I need a table data that is in another server and that database is sql database is in IIS server.
'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' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=line
DB_USERNAME=root
DB_PASSWORD=
I need help to use two databases from different server (one from mysql and another from sql) on my same laravel application.
In your 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' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
// Add this (Connect sql server)
//TCP port 1433 is typically the port used by a default instance of SQL Server
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env(''),
'host' => env('DB_HOST', 'X.X.X.X'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'database_name'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8mb4',
'prefix' => '',
'prefix_indexes' => true,
],
Get your mysql table
$mysql = DB::connection('mysql')->table('xxx')->select('*')->get();
Get your sql server table
$sqlsrv= DB::connection('sqlsrv')->table('xxx')->select('*')->get();
I'm doing a PHP Laravel (7.7.1) project with a mariadb database (10.4.12).
Actually, I'm trying to use a remote mariadb database (10.1.44) on a virtual private server under Debian Stretch (9.11) with my local mariadb database (10.4.12) under Macos Catalina (10.15.4).
The problem is that my Laravel project can't access to the remote database, I saw that the best solution is to use a ssh tunnel but no one solution worked for me.
Here is my .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=name
DB_USERNAME=username
DB_PASSWORD=password
DB_CONNECTION_GAME=mysql
DB_HOST_GAME=11.22.3.444
DB_PORT_GAME=3306
DB_DATABASE_GAME=name
DB_USERNAME_GAME=username
DB_PASSWORD_GAME=password
Here is my database.php file
'mysql' => [
'driver' => env('DB_CONNECTION', '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'),
]) : [],
],
'mysqlgame' => [
'driver' => env('DB_CONNECTION_GAME', 'mysql'),
'url' => env('DATABASE_URL_GAME'),
'host' => env('DB_HOST_GAME', '127.0.0.1'),
'port' => env('DB_PORT_GAME', '3306'),
'database' => env('DB_DATABASE_GAME', 'forge'),
'username' => env('DB_USERNAME_GAME', 'forge'),
'password' => env('DB_PASSWORD_GAME', ''),
'unix_socket' => env('DB_SOCKET_GAME', ''),
'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 is my database model
class Whitelisted extends Model
{
/**
* The attributes that are mass assignable.
* #var array
*/
protected $fillable = [
'name', 'identifier'
];
protected $connection = 'mysqlgame';
protected $table = 'user_whitelist';
}
And finnaly, here is the error code from Illuminate
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `user_whitelist`)
What is the best solution to have two databases with one local and the other connected to a remote server ?
Thanks !
Basically what I am trying to do is sharing session between laravel applications using a common database.
what I've tried so far is
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=login_test
DB_USERNAME=root
DB_PASSWORD=
DB_DATABASE_ADMIN=login_admin
DB_USERNAME_ADMIN=root
DB_PASSWORD_ADMIN=
SESSION_DRIVER=database
SESSION_CONNECTION=login_admin //getting error here: Database [login_admin] not configured.
session.php
'driver' => env('SESSION_DRIVER', 'file'),
'files' => storage_path('framework/sessions'),
'connection' => env('SESSION_CONNECTION', null),
'table' => 'sessions',
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', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE_ADMIN', 'forge'),
'username' => env('DB_USERNAME_ADMIN', 'forge'),
'password' => env('', ''),
'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'),
]) : [],
]
so here I want to set the sessions table into my common database named login_admin
I am already sharing a common users table between the projects so I've no issue on connecting multiple databases.
I've checked previous relevant questions but did not find how to share the table into common database
/*
|--------------------------------------------------------------------------
| Session Database Connection
|--------------------------------------------------------------------------
|
| When using the "database" or "redis" session drivers, you may specify a
| connection that should be used to manage these sessions. This should
| correspond to a connection in your database configuration options.
|
*/
According to your code SESSION_CONNECTION field should be the connection name, which is mysql2 in your case and not login_admin
I use Laravel and XAMPP on Mac OS. here my .env file
when I request the data from database I get this error
SQLSTATE[HY000] [2006] MySQL server has gone away
here the screen shot
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8443
DB_DATABASE=test
DB_USERNAME=rdxcft
DB_PASSWORD=rdxcft
and my 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'),
]) : [],
],
thank for your attention