0

I have created a multi-tenant database and problem is that when I create a new database with tenant I receive the following error:

SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user 'ion'@'localhost' to database 'a901ce392dad4b90b1c6e175b07196a9' (SQL: GRANT ALL ONa901ce392dad4b90b1c6e175b07196a9.* TOa901ce392dad4b90b1c6e175b07196a9@'127.0.0.1')

I have the following controller to create a new tenant:

        public function store(RegisterAuthRequest $request)
    {
        $name = $request->name;
        $email = $request->email;
        $password = $request->password;
        if (Tenant::tenantExists($name)) {
            return response()->json([
                'success' => false,
                'message' => "A tenant with name " . $name . " already exists."
            ]);
        }
        $tenant = Tenant::registerTenant($name, $email, $password);
        $tenancy = app(Environment::class);


        $tenancy->hostname(); // resolves $hostname as currently active hostname


        $tenancy->website(); // resolves $website
        $tenancy->tenant(); // resolves $website
//
        $tenancy->identifyHostname(); // resets resolving $hostname by using the Request
        // invite admin
        $tenant->admin->notify(new TenantCreated($request->email, $request->password));
        return response()->json([
            'success' => true,
            'message' => 'tenant with name ' . $tenant->hostname->fqdn . " created and Admin {$email} can log in using password {$password}"
        ],200);
    }

And here is my Tenant model:

    public static function registerTenant($name, $email, $password): Tenant
{
    // Convert all to lowercase
    $connection = app(Connection::class)->systemName();
    $name = strtolower($name);
    $email = strtolower($email);
    $website = new Website;
    if($connection != 'default') $website->managed_by_database_connection = $connection;
    app(WebsiteRepository::class)->create($website);
    // associate the website with a hostname
    $hostname = new Hostname;
    $baseUrl = config('tenancy.hostname.default');
    $hostname->fqdn = "{$name}.{$baseUrl}";
    app(HostnameRepository::class)->attach($hostname, $website);
    // make hostname current
    app(Environment::class)->tenant($hostname->website);
    // Make the registered user the default Admin of the site.
    $admin = static::makeAdmin($name, $email, $password, $hostname);
    return new Tenant($website, $hostname, $admin);
}
private static function makeAdmin($name, $email, $password, $hostname): User
{
    $admin = new User;
    $admin->name = $name;
    $admin->email = $email;
    $admin->password = Hash::make($password);
    $admin->hostname()->associate($hostname);
    $admin->job_title = 'admin';
    $admin->birth_date = '1000-01-01';
    $roleAdmin = Role::where('name', 'admin')->firstOrFail();
    $admin->assignRole($roleAdmin);
    $admin->save();
    return $admin;
}
public static function tenantExists($name)
{
    $name = $name . '.' . config('tenancy.hostname.default');
    return Hostname::where('fqdn', $name)->exists();
}

I have all privileges for ion@localhost in MySQL:

+--------------------------------------------------+
| Grants for ion@localhost                         |
+--------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'ion'@'localhost' |

and there is the thing, in error, it's said SQL: GRANT ALL ONa901ce392dad4b90b1c6e175b07196a9.* TOa901ce392dad4b90b1c6e175b07196a9@'127.0.0.1' and I don't understand why is giving all Grant to this user?

I tried dd() to find out where the error occurs and here it is:

app(WebsiteRepository::class)->create($website);
Shadow
  • 33,525
  • 10
  • 51
  • 64
Mindru Ion
  • 373
  • 5
  • 19
  • can you try giving permission to ion@'127.0.0.1', not localhost? MySql must be connected here on 127.0.0.1 not on localhost. – prabhat Sep 18 '19 at 01:43
  • I tried, I found something weird, I use `GRANT ALL PRIVILEGES ON *.* TO 'ion'@'%'` and everything is ok, only one privilege is not given "grant privilege", do you know why? – Mindru Ion Sep 18 '19 at 07:14

0 Answers0