Getting ''Access denied for user 'root'@'localhost' (using password: YES)'' when I have no password on root [duplicate]

此生再无相见时 提交于 2020-01-26 04:01:27

问题


So I am trying to send data to a database but I am getting denied access when trying.
PS: I don't have a password to root.

<?php
$userName = $_POST['registerName'];
$userPassword = $_POST['registerPassword'];
$userEmail = $_POST['registerEmail'];
$userStudie = $_POST['registerStudie'];
$userYear = $_POST['registerYear'];


if (!empty($userName) || !empty($userPassword) || !empty($userEmail) || !empty($userStudie) || 
!empty($userYear)) {
$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbname = "brukere";

$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);

if (mysqli_connect_error()) {
    die('Connect Error('.mysqli_connect_errno().')'.mysqli_connect_error());
} else {
    $SELECT = "SELECT brukerEmail FROM brukeretabell WHERE brukerEmail = ? LIMIT 1";
    $INSERT = "INSERT INTO brukeretabell (brukerNavn, brukerPassord, brukerEmail, brukerStudie, brukerAar) VALUES (?, ?, ?, ?, ?)";

    $stmt = $conn->prepare($SELECT);
    $stmt->bind_param("s",$brukerEmail);
    $stmt->execute();
    $stmt->bind_result($brukerEmail);
    $stmt->store_result();
    $rnum = $stmt->num_rows;

    if ($rnum == 0) {
        $stmt->close();

        $stmt = $conn->prepare($INSERT);
        $stmt->bind_param("sssii", $brukerNavn, $brukerPassord, $brukerEmail, $brukerStudie, $brukerAar);
        $stmt->execute();
        echo "Bruker lagt til";
    } else {
        echo "Bruker allerede registrert";
    }
    $stmt->close();
    $conn->close();
}
} else {
echo "Du må fylle ut alle feltene";
die();
}
?>

This is the message I get when trying to submit data to the database: Can't post images so nice if someone that can does

This is line 15:
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);

I have been trying the whole day now, but haven't got any result. Thanks prior!

EDIT:
These are the users: enter image description here


回答1:


The root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program, to access the user.

In order to use a password to connect to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal:

sudo mysql

Next, check which authentication method each of your MySQL user accounts use with the following command:

SELECT user,authentication_string,plugin,host FROM mysql.user;

// Output

+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25FE3CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

In this example, you can see that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:

FLUSH PRIVILEGES;

Check the authentication:

SELECT user,authentication_string,plugin,host FROM mysql.user;

Output

+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             | *3636DACC8616T8978782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

You can see in this example output that the root MySQL user now authenticates using a password.

exit



来源:https://stackoverflow.com/questions/59724519/getting-access-denied-for-user-rootlocalhost-using-password-yes-when

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!