Multiple database in Laravel with a remote database - php

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 !

Related

How to connect with another database to submiit the data in laravel

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';
}

How to use two different database one from sql server and another from mysql server in same laravel application

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();

Laravel Error SQLSTATE[HY000] [2002] No such file or directory in Laravel 6

i have some issue about connection on laravel into database MySQL like this.
My laravel files and database has been published on hosting.
Illuminate\Database\QueryException.
SQLSTATE[HY000] [2002] No such file or directory (SQL: SELECT * FROM tbl_master_users WHERE identity_number = '12345' AND password ='password')
I tried to looking for some reference to solve this issue, but it's not working on me.
this is .env file : (I tried to change DB_HOST into 127.0.0.1 but it still same)
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db_users
DB_USERNAME=uname
DB_PASSWORD=password
this is my configuration on 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' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Socket :

Not able to connect Laravel app to database which is hosted on Azzure over ssl

I am struggling in connecting Laravel app with MYSQL database. I can connect to database over MySQL Workbench and also with PDO connection custom string but with Laravel I cannot. Not sure what is wrong.
Custom connection string: It works file is inside laravel app. I put this just for checking
$conn = mysqli_init();
mysqli_ssl_set($conn,NULL,NULL, "BaltimoreCyberTrustRoot.crt.pem", NULL, NULL) ;
mysqli_real_connect($conn, 'sever.host', 'server-username', 'server-password', 'database', 3306, MYSQLI_CLIENT_SSL);
if (mysqli_connect_errno($conn)) {
die('Failed to connect to MySQL: '.mysqli_connect_error());
}
else{
echo "Connected";
}
Then I added in .env file following, beside defining username password host and db name.
BaltimoreCyberTrustRoot.crt.pem file is on same path as .env file
MYSQL_SSL_KEY=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CERT=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CA=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CIPHER=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL=true
In config/database.php looks like this
'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,
'options' => (env('MYSQL_SSL')) ? [
PDO::MYSQL_ATTR_SSL_KEY => env('MYSQL_SSL_KEY'), // /path/to/key.pem
PDO::MYSQL_ATTR_SSL_CERT => env('MYSQL_SSL_CERT'), // /path/to/cert.pem
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_SSL_CA'), // /path/to/ca.pem
PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER')
] : [],
Not sure what is wrong, any idea?
I get these errors:
"error":"SQLSTATE[HY000] [2002] An attempt was made to access a socket in a way forbidden by its access permissions.
[{"file":"D:\home\site\wwwroot\vendor\laravel\framework\src\Illuminate\Database\Connection.php","line":624,"function":"runQueryCallback","class":"Illuminate\Database\Connection","type":"->","args":["select * from backend_token limit 1",[],{}]},
It helps to know which version of Laravel you are on to be absolutely sure, but on Laravel 8 my database config has:
'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'),
]) : [],
],
Don't change your database.php config but in your .env add MYSQL_ATTR_SSL_CA=/path/to/BaltimoreCyberTrustRoot.crt.pem
FYI sometimes if you are using private IP addresses then edit your hosts file
10.10.10.10 hostname.domain.com
Where of course the ip number matches the ip of the host.
If that doesn't work, add the cipher line from you config PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER'),

Laravel : SQLSTATE[HY000] [2006] MySQL server has gone away

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

Categories