Laravel Hyn/Tenancy Database [tenant] not configured - laravel

I followed the tutorial in this web and with homestead, got results right.
But, with XAMPP on Windows, when I try to login as a tenant, "Database [tenant] not configured" error occurs.
URL:
https://www.seismicpixels.com/creating-a-laravel-saas-framework-part-1/
https://www.seismicpixels.com/creating-a-laravel-saas-framework-part-2/
(At part-2, step 4, the error occurs and I cannot login as the tenant)
Environment:
XAMPP for Windows 7.3.9
PHP 7.3.9
Apache/2.4.41 (Win64)
mysql Ver 15.1 Distrib 10.4.11-MariaDB, for Win64 (AMD64)
Laravel Framework 5.8.37
hyn/multi-tenant 5.4.5
I am very new to Laravel and if Im missing any information to ask help, please kindly let me know.
Thank you in advance.
added on 26th
I added 'tenant' in database.php then I get the following error now:
SQLSTATE[HY000] [1045] Access denied for user ''#'localhost' (using password: NO) (SQL: select * from users where email = billy#example.com limit 1)
it seems hyn/tenancy cannot get the user database name (uuid) and it's password.
is this hyn/malti-tenant bug?
my database.php------------
'connections' => [
'system' => [
'driver' => 'mysql',
'host' => env('TENANCY_HOST', '127.0.0.1'),
'port' => env('TENANCY_PORT', '3306'),
'database' => env('TENANCY_DATABASE', 'tenancy'),
'username' => env('TENANCY_USERNAME', 'tenancy'),
'password' => env('TENANCY_PASSWORD', 'tenancy'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'tenant' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => '',
'username' => '',
/*'database' => '3d118cf757154f6d9be5cb1935078588',
'username' => 'root',*/
'password' => '',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
my .env---------------------------
DB_CONNECTION=system
TENANCY_HOST=127.0.0.1
TENANCY_PORT=3306
TENANCY_DATABASE=tenancy
TENANCY_USERNAME=tenancy
TENANCY_PASSWORD=tenancy

At the end, using stancl/tenancy solved it all.
Strongly recommend using stancl now.
https://github.com/stancl/tenancy

Related

Laravel, MySQL, Azure, SSL Connection is required

I'm trying to deploy a laravel 5.8 application to Azure, but I'm getting the following error;
"SSL connection is required. Please specify SSL options and retry."
In the config folder there wasn't a database.php file, so I created one using the database.php in the Laravel Git Repository. I downloaded the BaltimoreCyberTrustRoo.crt.pem file from MS and placed this inside the root of my application inside a folder called ssl. I've then modified the database.php file to this;
'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,
'sslmode' => env('DB_SSLMODE', 'prefer'),
'options' => (env('MYSQL_SSL') && extension_loaded('pdo_mysql')) ? [
PDO::MYSQL_ATTR_SSL_KEY => base_path('ssl/BaltimoreCyberTrustRoot.crt.pem'),
] : []
],
However, even with this change, I'm still seeing the same error.
I've attached a screenshot of the folder structure incase it is useful.
I managed to resolve this by changing the mysql options value to;
'options' => [
PDO::MYSQL_ATTR_SSL_KEY => base_path('ssl/BaltimoreCyberTrustRoot.crt.pem'),
]
instead of
'options' => (env('MYSQL_SSL') && extension_loaded('pdo_mysql')) ? [
PDO::MYSQL_ATTR_SSL_KEY => base_path('ssl/BaltimoreCyberTrustRoot.crt.pem'),
] : []
Mine is a non production(just learning) scenario, so I disabled ssl.
If it is enabled, I am getting this error.

Laravel 6 No such file or directory driver PDOConnection

I have next error :
Illuminate\Database\QueryException
include(/home/xxxx/xxxxxx/vendor/composer/../doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php): failed to open stream: No such file or directory (SQL: select * from `users`)
I tried to change .env and database.php host to localhost or 127.0.0.1 or change port to 33060, composer update / install / dump-autoload , etc (All solution i found on website)
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'xxxxx'),
'username' => env('DB_USERNAME', 'xxxx'),
'password' => env('DB_PASSWORD', 'xxxx'),
'unix_socket' => env('DB_SOCKET', '/var/lib/mysql/mysql.sock'),
'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'),
]) : [],
],
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
Laravel Framework 6.18.6
There is a much stronger way of cleaning a confused install...
composer dumpautoload
Works nearly every time for me, including in cases where this particular error was encountered.

How to change table prefix when a user logs in to account?

I am in new laravel, In my application When a user logs into his account I want to change the default database prefix from the env file. I setup wildcard subdomain for every users. When every user logs in I want to change the database prefix according to the sub domain.
So is it possible to change the laravel prefix globally if a user logs in Laravel?. Please suggest me if there is any solution. Thanks is advance.
Of course you can:
config/database.php
'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,
],
'mysql_earth' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_EARTH_DATABASE', 'earth'),
'username' => env('DB_EARTH_USERNAME', 'earth'),
'password' => env('DB_EARTH_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mysql_moon' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_MOON_DATABASE', 'moon'),
'username' => env('DB_MOON_USERNAME', 'moon'),
'password' => env('DB_MOON_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=forge
DB_USERNAME=forge
DB_PASSWORD=forgepw
DB_EARTH_DATABASE=earth
DB_EARTH_USERNAME=erth
DB_EARTH_PASSWORD=earthpw
DB_MOON_DATABASE=moon
DB_MOON_USERNAME=moon
DB_MOON_PASSWORD=moonpw
Use in your Controller example put your conditions on the connection and then you can have the query in each database:
$db = \DB::connection('mysql');
$db = \DB::connection('mysql_earth');
$db = \DB::connection('mysql_moon');
$products = $db->table('products')
->distinct()
->select("*" )
->orderBy('products.id','asc')
->get();

Laravel Hyn\Multi-Tenant Database [tenant] not configured

I am working on a multi-tenant laravel application, and have run into an issue with the hyn\multi-tenant package. The documentation for hyn\multi-tenant states that the tenant database connection will be handled by the package, and that as long as the system connection is available and the user has privileges to add and modify databases, the package will handle all tenant database connections.
Upon trying to create a tenant in my application I get the error: Database [tenant] is not configured.
I have seen many answers to this issue on SO, however they all refer to the Customer model, or localhost configurations. Hyn removed the Customer model, and the issue I am having is also happening on my DigitalOcean server published through Laravel Forge.
I would be grateful to anyone that might be able to provide some assistance.
My .env (local)
APP_NAME="Multi-Tenant"
APP_ENV=local
APP_KEY=base64:j1aLzU7m5LWK1keo/FjgbtpwTpVZ1NBj29zuXIByHek=
APP_DEBUG=true
APP_URL_BASE=localhost:8888/lms/public
APP_URL=http://${APP_URL_BASE}
LOG_CHANNEL=stack
DB_CONNECTION=system
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=lmssystem
DB_USERNAME=lmssystem
DB_PASSWORD=lmssystem
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
LIMIT_UUID_LENGTH32=true
My database.php
return [
'default' => env('DB_CONNECTION', 'system'),
'connections' => [
'system' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '8889'),
'database' => env('DB_DATABASE', 'lmssystem'),
'username' => env('DB_USERNAME', 'lmssystem'),
'password' => env('DB_PASSWORD', 'lmssystem'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => 'InnoDB',
],
],
'migrations' => 'migrations',
'redis' => [
'client' => '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),
],
],
];
This issue turned out to be a logic error in my application not a database configuration issue. I was forced to deviate from Ashok's article on Medium [https://medium.com/#ashokgelal/a-full-featured-multi-tenant-app-with-laravel-part-1-4049a3cc229d][1] since the Hyn\Multi-Tenant package no longer supports the customer model.
After developing my own client model, and extending the existing Website and Hostname models to interact with the client model, I had to re-write the tenant:create command. In this, I created websites and hostnames directly from their extended models, rather than via the repositories as per the Hyn documentation (copied below)
Tenancy is heavily driven by events. For event listeners to properly work, you have to use the repositories to create new websites and hostnames.
use Hyn\Tenancy\Models\Website;
use Hyn\Tenancy\Contracts\Repositories\WebsiteRepository;
$website = new Website;
app(WebsiteRepository::class)->create($website);
dd($website->uuid);
$hostname = new Hostname;
$hostname->fqdn = 'luceos.demo.app';
app(HostnameRepository::class)->attach($hostname, $website);
Creating via the repositories resolved the:
Database [tenant] not configured
Error.
this code may be solve your problem
config/database.php :
'connections' => [
'system' => [
'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,
],
],
create a "tenant" connection on the config/database.php file
'tenant' => [
'driver' => 'mysql',
'host' => env('TENANCY_HOST', '127.0.0.1'),
'port' => env('TENANCY_PORT', '3306'),
'database' => env('TENANCY_DATABASE', 'tenancy'),
'username' => env('TENANCY_USERNAME', 'tenancy'),
'password' => env('TENANCY_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
];
if it related to Livewire problem,
Open the config/livewire.php file and change this:
'middleware_group' => ['web'],
to this:
'middleware_group' => [
'web',
'universal',
InitializeTenancyByDomain::class, // or whatever tenancy middleware you use
],

Troubleshouting laravel migrations for new database

I am trying to set a separate database for tests. For some reason when I run the migrations I have always a message saying: "Nothing to migrate."
Here is how I run the migrations:
php artisan migrate --database=mysql_test
Nothing to migrate.
And here is my config/database.php:
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydb'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'dev'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql_test' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydbtest'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'dev'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
And obviously I can login to the database mydbtest with the credentials in the config.
Updating here after some troubleshooting:
After emptying mydb database and running the same command it filled the mydb database not mydbtest. This means it does not take into account what I am giving as database connection name.
Any suggestions to troubleshoot this are welcome.
Thanks
I found the problem.
After commenting out this line in my .env file it works now:
#DB_DATABASE=mydb
Thanks

Resources