-1

I'm trying to put a project to production. I'm getting an access error when running an Axios post request to the Laravel API. (I'm not getting this error on the local machine)

[2019-01-28 12:18:04] local.ERROR: SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' (SQL: select * from `oauth_clients` where `id` = 2 limit 1) {"exception":"[object] (Illuminate\\Database\\QueryException(code: 1698): SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' (SQL: select * from `oauth_clients` where `id` = 2 limit 1) at /var/www/html/RemApp/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, PDOException(code: 1698): SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost' at /var/www/html/RemApp/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:68)

I'm not getting an error when running migrations. Below is my .env file. (I have not changed the config/database.php)

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=AppName
DB_USERNAME=root
DB_PASSWORD=mypasswordhere
  1. Tried clearing cache with:

    php artisan cache:clear
    php artisan config:clear
    
  2. Removed quotes from DB_PASSWORD

  3. The credentials are correct. (Can login and run queries using mysql -u -p)
Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
Vova
  • 47
  • 6
  • I'm using a digital ocean ubuntu droplet if this helps. – Vova Jan 28 '19 at 12:33
  • Is your production database not password protected ? You can run this `.env` on both environments and it would still connect to 2 different databases – Mike Jan 28 '19 at 12:35
  • Don't deploy a application to a production environment on your own if you don't have system administration experience. I doubt this problem is programming related. – Devon Bessemer Jan 28 '19 at 12:49

4 Answers4

2

Turns out this was related to: SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'. Tried everything

For some reason a mysql root user cannot be used. To solve this you would need to create a new user. Below are the commands to create a new user and grant root privileges.

mysql -u root -p
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';
FLUSH PRIVILEGES;
exit
service mysql restart

Now edit your .env to match the new user credentials. To be on the safe side you can reboot the server.

Vova
  • 47
  • 6
0

If the db password is empty, don't put an empty string, leave it blank, as such, change:

DB_PASSWORD=''

To

DB_PASSWORD=

otherwise, it passes those quotes as your password.

Edit #1

If you do have a password, then unquote it if it is quoted.

Script47
  • 14,230
  • 4
  • 45
  • 66
0

Just set up correct Database credentials:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=        // Your Database Name
DB_USERNAME=       // Yout Database Username
DB_PASSWORD=      // Your Database Password

If no password is set on the database, clear it DB_PASSWORD, empty space must also be removed

After completion of .env edit, You can clear the configuration cache with the following artisan command: php artisan config:cache

you need also restart your server after changing your .env file values.

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
  • I removed the password just for stack overflow. I have it in the original .env – Vova Jan 28 '19 at 12:37
  • @Vova please clear cache: `php artisan config:cache` – Udhav Sarvaiya Jan 28 '19 at 12:40
  • @Vova If all possible solution is not working for you then please provide some more information. make sure have you a default username(**root**) in the installation of MySQL ? – Udhav Sarvaiya Jan 28 '19 at 12:48
  • What exactly is "this"? Can you explain what you've changed and how that might solve the problem? – Nico Haase Jan 28 '19 at 13:11
  • @NicoHaase I gave my answer Op on the basis of editing the previous question, OP have changed question view, yes i will explain answer but i will first out find out solution because op have tried this solution but is not work for him [check here](https://stackoverflow.com/posts/54402070/revisions) – Udhav Sarvaiya Jan 28 '19 at 13:15
  • 1
    I understand that it is difficult to adopt an answer while the question has changed, and surely one should appreciate that you try to find a new solution. But regardless of the question, "try this"-answers without an explanation are not as good as those which contain an explanation – Nico Haase Jan 28 '19 at 13:21
  • 1
    Okay, thanks for your time @NicoHaase , I will take care of it next time :) – Udhav Sarvaiya Jan 28 '19 at 13:28
0

First of all, using the root with empty password on production is dangers. You need to use a separate user with strong password or at least use strong password for root.

and clear cache on production

php artisan cache:clear
php artisan config:clear