How to run CREATE DATABASE command on LIVE Laravel site through get request

55 Views Asked by At

Background:

  • I've inherited a Laravel PHP codebase and needed to make some minor changes.
  • I added a page with a button that invokes this method below to create a database on the fly.
  • This works on my machine in dev, fails with 500 error on live.
  • If I comment out this line, the redirect works fine, so I know this is the issue.

I know this isn't ideal, but it's what I want to do. How can I make this command work please?

Method I added:

public function store()
{
    $datestr = date("Y_H_i_s");
    DB::statement("CREATE DATABASE xxxx_". $datestr);
    return redirect()->to('/setupfinished');
}

Edit: Here's the error message - so how do I enable permissions for this user?

[previous exception] [object] (PDOException(code: 42000): SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user 'xxxx'@'localhost' to database 'xxxx_2024_19_14_10' at /var/www/summer/vendor/laravel/framework/src/Illuminate/Database/Connection.php:580)
[stacktrace]

Treat me as if I know nothing about Laravel but I do know some PHP.

Thanks!

2

There are 2 best solutions below

0
Dave On BEST ANSWER

Thanks for help - I fixed with mysql command:

grant all privileges on *.* to '<user>'@'localhost';
1
Raj Kumar On

If it works on local environment, then the issue might be with the database permissions on the server. Make sure the DB_USERNAME you have in .env file on server has permission to create database.