0

Trying to create a login-system, but the browser just shows the error message (Access denied for user 'root'@'localhost'). I have confirmed the password for mysql, but realized that the password to get into the server is needed, but not provided.

Part of my php-file looks like this:

    <?php
session_start(); //Nevermind this indent
    $DATABASE_HOST = 'localhost';
    $DATABASE_USER = 'root';
    $DATABASE_PASS = ''; //redacted, but provided
    $DATABASE_NAME = 'users';
    $con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
    if ( mysqli_connect_errno() ) {
        exit('Failed to connect to MySQL: ' . mysqli_connect_error()); // it always catches here
    }

It contains the mysqli_connect() with all the variables to the database, but not to get into the mysql server. I'm using php 8.0 with mariaDB on Ubuntu server 20.04. How can I provide the password, grant the access to or else to get into the mariaDB-server? I would like to keep the password if possible. And yeah, I'm a beginner.

Ric
  • 21
  • 1
  • Does this answer your question? [MySQL Error: : 'Access denied for user 'root'@'localhost'](https://stackoverflow.com/questions/41645309/mysql-error-access-denied-for-user-rootlocalhost) OR this link: https://www.google.com/search?q=Access+denied+for+user+%27root%27%40%27localhost%27+site%3Astackoverflow.com – Luuk Sep 04 '21 at 09:14
  • @Luuk The link seems to solve getting into the databases, but I can't enter the MariaDB server without the sudo password. If I try (mysql -u root -p) without the sudo, I'll get the same error message, meaning that the Mysql password is correct. – Ric Sep 05 '21 at 08:59
  • If you are unable to logon with `mysql -u root -p`, then you could try resetting your password, see: https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html – Luuk Sep 05 '21 at 09:05
  • @Luuk I can logon with the system password and the mysql password. But that requires me to use (sudo mysql -u root -p(password)) which works perfectly fine in the terminal, but not through the php-code as the system password isn't included. – Ric Sep 05 '21 at 11:03
  • You should be able to log on (with: mysql -u root -p) without using `sudo`. [This](https://askubuntu.com/questions/766334/cant-login-as-mysql-user-root-from-normal-user-account-in-ubuntu-16-04) shows how to solve it. When trying out stuff with MySQL, you should create another account, and use that for testing. Leave `root` for local access, doing admin stuff. – Luuk Sep 05 '21 at 14:16
  • BTW: There is a difference between `root`@`localhost` and `root`@`%`. You can check which ones exist using: `select user,host from mysql.user;`. You should make sure `root`@`%` cannot be used from the internet. – Luuk Sep 05 '21 at 14:18
  • Using root access for application use strongly not recommended. You should to create new user with relevant permissions for application use. – Slava Rozhnev Sep 09 '21 at 19:48

0 Answers0