Laravel 6 multiple database authentication problem - mysql

I have two DB connection as bellow:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sales_report
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=sqlsrv
DB_HOST=192.168.102.11
DB_PORT=1433
DB_DATABASE=Some_Name
DB_USERNAME=XXXXXX
DB_PASSWORD=XXXXXX
I am authenticating with mysql also written code as bellow:
class User extends Authenticatable
{
use Notifiable;
protected $connection = 'mysql';
All DB connections are ok but still it takes too long time to login. It also does not show any result. Could you please help?

Because your env db configuration key_name is the same,
It seems laravel choose the second connection, so it cannot find the connection mysql. The first connection is covered by second.
Change another connection key name for .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sales_report
DB_USERNAME=root
DB_PASSWORD=
DB_SRV_CONNECTION=sqlsrv
DB_SRV_HOST=Host_name
DB_SRV_PORT=1433
DB_SRV_DATABASE=DB_name
DB_SRV_USERNAME=user
DB_SRV_PASSWORD=password
In your config/database.php
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
...
],
'sql_srv' => [
'driver' => 'sqlsrv',
'host' => env('DB_SRV_HOST', '127.0.0.1'),
'port' => env('DB_SRV_PORT', '3306'),
You can check the connection configuration in your tinker:
config('database.connections.mysql')
If it still not work, you can clear the config cache:
php artisan config:clear
php artisan optimize

Change your .env file to:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sales_report
DB_USERNAME=root
DB_PASSWORD=
DB2_HOST=Host_name
DB2_PORT=1433
DB2_DATABASE=DB_name
DB2_USERNAME=user
DB2_PASSWORD=password
Then in your config/database.php change the SQL server configuration to use the new names:
...
'connections' => [
// ...
'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'),
]) : [],
],
// ...
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB2_HOST', 'localhost'),
'port' => env('DB2_PORT', '1433'),
'database' => env('DB2_DATABASE', 'forge'),
'username' => env('DB2_USERNAME', 'forge'),
'password' => env('DB2_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
...
Doing this will set the default database to mysql but you can use the alternative connection in each model or on the query builder:
class User extends Authenticatable
{
use Notifiable;
protected $connection = 'sqlsrv';
or
DB::connection('sqlsrv')->table('users')->...
You can change the default by changing the DB_CONNECTION entry in your .env file but then you need to override the connection to mysql where needed.

Related

Laravel is unable to identify the data in phpmyadmin

I use Laravel
When I try to access Localhost:8000 I get an error
Illuminate\Database\QueryException
SQLSTATE[HY000] [1049] Unknown database 'my_data' (SQL: select `title`,`id` from `table_8` where `id` = 24)
I have a database called my_data in phpmyadmin
XAMPP\volumes\root\htdocs\project\.env
_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= my_data
DB_USERNAME=root
DB_PASSWORD=
XAMPP\volumes\root\htdocs\project\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', 'my_data'),
'username' => env('DB_USERNAME', 'root'),
'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'),
]) : [],
],
I changed the DB_DATABASE to my_data in the .env file
And I ran the command
$php artisan cheat:clear
And yet the error is still the same error
Illuminate\Database\QueryException
SQLSTATE[HY000] [1049] Unknown database 'my_data' (SQL: select `title`,`id` from `table_8` where `id` = 24)
I am use Mac
i think your username and password of phpmyadmin is wrong. please check it out. of different. fix it. and try again

Trying to connect Laravel to Mysql via XAMPP

I'm trying to get my laravel app and the mysql database in XAMPP connected and struggling. On http://localhost:8080/aws-upload/public/ I get the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydb2.files' doesn't exist (SQL: select * from `files`)
It then prompts me to migrate, if I migrate on the chrome button provided by laravel the site appears and starts to work, however if I try to migrate through the terminal I get:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = mydb2 and table_name = migrations and table_type = 'BASE TABLE')
I've created the database in phpMyAdmin called mydb2 and updated my .env file to:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb2
DB_USERNAME=root
DB_PASSWORD=
This is my database.php file:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydb2'),
'username' => env('DB_USERNAME', 'root'),
'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'),
]) : [],
],
Thanks
In your database.php file:
'host' => env('DB_HOST', 'localhost'), try changing localhost to 127.0.0.1
In your .env file:
At the top of the file, you'll see it says APP_URL=http://example.com - change the url to APP_URL=http://127.0.0.1

Trying to connect laravel to mysql but i keep getting an error

I have my laravel app running and i'm trying to connect it to a Mysql database. I'm using XAAMP for this. After getting Apache and Mysql is running (Mysql is running on port 3308). I reconfigured my .env file and database.php file and I've run php artisan config cache. However when i try to create a database from my terminal using create database xxx, xxx database is created but when i check phpmyadmin on my browser, i can't find the xxx datatbase.
Here's my database.php file:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3308'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'test123'),
'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'),
]) : [],
],
And here's my .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3308
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=test123
Solved it but i had to clear up port 3306 so that XAAMP mysql could use it. Cleared it by ending all mysql tasks under task manager.

Laravel using multiple databases (2 MySQL, 1 SQLite) [duplicate]

This question already has answers here:
How to use multiple databases in Laravel
(7 answers)
Closed last month.
I'm developing a project where I need to use multiple databases in Laravel. The first database works like a charm (MySQL) but I can't get the framework to recognize the SQLite DB. The error that shows up is this:
Illuminate \ Database \ QueryException
Database (sqlite) does not exist. (SQL: PRAGMA foreign_keys = ON;)
Here is my .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=sitio1
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION_SECOND=sqlite
DB_DATABASE_SECOND=/Users/imac/CleverOctopusBDD/database/Sitio2.sqlite
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=
and here is my database.php
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_CONNECTION_SECOND', database_path('Sitio2.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'sitiocentral'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
]) : [],
],
And the location of my .sqlite file is this -> database -> Sitio2.sqlite
I've doing some research and I don't really know what am I doing wrong. If you are wondering, this is how one of my sqlite db model looks...
class Client2 extends Model
{
protected $connection = 'sqlite';
protected $fillable = [
'name', 'email', 'phone', 'address'
];
protected $table = 'Client2';
protected $primaryKey = 'idClient';
public $timestamps = false;
}
I hope you can help me out here because I have found information about multiple MySQL connections but not with SQLite, I appreciate any tips given.
EDIT: I just needed to pass the full path to 'database' => env('DB_DATABASE_SECOND', database_path('Sitio2.sqlite')),
does it work if you put the full path to your sqlite file in the database config like this?
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => '/Users/imac/CleverOctopusBDD/database/Sitio2.sqlite',
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],

Laravel 5.4 : SQLSTATE[HY000] [2002] No such file or directory

I know there is a lot of topics on this error but it seems that I already try almost everything and it doesn't work at all.
So I've set up my .env file like this:
APP_NAME=Neuralia
APP_ENV=local
APP_KEY=(key)
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://127.0.0.1:8000
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=neuralia
DB_USERNAME=username
DB_PASSWORD=password
and my config/database.php like this:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'neuralia'),
'username' => env('DB_USERNAME', 'username'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I tried with host => localhost, I cleared my config / cache and dump-autoload but I still get the error when I try to create a user from the register form.
I can use php artisan migrate:refresh etc but not the register / login stuff :/
do you have any ideas please?
Thanks