ERROR 1698 (28000): Access denied for user 'root'@'localhost'

后端 未结 17 1583
执笔经年
执笔经年 2020-11-22 10:00

I\'m setting up a new server and keep running into this problem.

When I try to login to the MySQL database with the root user, I get the error:

相关标签:
17条回答
  • 2020-11-22 10:32

    You want to access MySQL with root user but you're not providing root's correct password.

    If you need to set a new password for root, MySQL's site has great documentation on how to do it: http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

    I'll not show the process in here because MySql documentation on the above link it's clear and concise.

    0 讨论(0)
  • 2020-11-22 10:32

    Basically means that: db_users using it, will be “auth” by the system user credentias. You can see if your root user is set up like this by doing the following:

    sudo mysql -u root # I had to use "sudo" since is new installation
    
    mysql> USE mysql;
    mysql> SELECT User, Host, plugin FROM mysql.user;
    
    +------------------+-----------------------+
    | User             | plugin                |
    +------------------+-----------------------+
    | root             | auth_socket           |
    | mysql.sys        | mysql_native_password |
    | debian-sys-maint | mysql_native_password |
    +------------------+-----------------------+
    

    As you can see in the query, the root user is using the auth_socket plugin

    There are 2 ways to solve this:

    1.You can set the root user to use the mysql_native_password plugin

    2.You can create a new db_user with you system_user (recommended)

    Option 1:

    sudo mysql -u root # I had to use "sudo" since is new installation
    mysql> USE mysql;
    mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
    mysql> FLUSH PRIVILEGES;
    mysql> exit;
    $ service mysql restart
    

    Some systems like Ubuntu, mysql is using by default the UNIX auth_socket plugin.

    Option 2: (replace YOUR_SYSTEM_USER with the username you have)

    $ sudo mysql -u root # I had to use "sudo" since is new installation
    
    mysql> USE mysql;
    mysql> CREATE USER 'YOUR_SYSTEM_USER'@'localhost' IDENTIFIED BY '';
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'YOUR_SYSTEM_USER'@'localhost';
    mysql> UPDATE user SET plugin='auth_socket' WHERE User='YOUR_SYSTEM_USER';
    mysql> FLUSH PRIVILEGES;
    mysql> exit;
    
    $ service mysql restart
    

    Remember that if you use option #2 you’ll have to connect to mysql as your system username (mysql -u YOUR_SYSTEM_USER)

    Note: On some systems (e.g., Debian stretch) ‘auth_socket’ plugin is called ‘unix_socket’, so the corresponding SQL command should be: UPDATE user SET plugin='unix_socket' WHERE User='YOUR_SYSTEM_USER';

    mysql 8.x.x updated/replaced the auth_socket for caching_sha2_password I don’t have a system setup with mysql 8.x.x to test this, however the steps above should help you to understand the issue.

    0 讨论(0)
  • 2020-11-22 10:33

    I found my solution after hours of research here.

    Stop MySQL

    sudo service mysql stop
    

    Make MySQL service directory.

    sudo mkdir /var/run/mysqld
    

    Give MySQL user permission to write to the service directory.

    sudo chown mysql: /var/run/mysqld
    

    Start MySQL manually, without permission checks or networking.

    sudo mysqld_safe --skip-grant-tables --skip-networking &
    

    Log in without a password.

     mysql -uroot mysql
    

    update password

    UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='%';
    EXIT;
    

    Turn off MySQL.

    sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
    

    Start the MySQL service normally.

    sudo service mysql start
    
    0 讨论(0)
  • 2020-11-22 10:34

    This has happened to me as well. The problem is with the mysql repo that comes already with the linux distro. So when you simply do: $ sudo apt install mysql-server it installs mysql from their default repo which gives this problem. So to overcome that you need to uninstall that installed mysql $ sudo apt remove mysql* --purge --auto-remove

    Then download mysql repo from official mysql website MySQL APT Repo Follow their documentation on how to add repo and install it. This gives no issue. Also as answered by @zetacu, you can verify that mysql root now indeed uses mysql_native_password plugin

    0 讨论(0)
  • 2020-11-22 10:35

    Some systems like Ubuntu, mysql is using by default the UNIX auth_socket plugin.

    Basically means that: db_users using it, will be "auth" by the system user credentias. You can see if your root user is set up like this by doing the following:

    $ sudo mysql -u root # I had to use "sudo" since is new installation
    
    mysql> USE mysql;
    mysql> SELECT User, Host, plugin FROM mysql.user;
    
    +------------------+-----------------------+
    | User             | plugin                |
    +------------------+-----------------------+
    | root             | auth_socket           |
    | mysql.sys        | mysql_native_password |
    | debian-sys-maint | mysql_native_password |
    +------------------+-----------------------+
    

    As you can see in the query, the root user is using the auth_socket plugin

    There are 2 ways to solve this:

    1. You can set the root user to use the mysql_native_password plugin
    2. You can create a new db_user with you system_user (recommended)

    Option 1:

    $ sudo mysql -u root # I had to use "sudo" since is new installation
    
    mysql> USE mysql;
    mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
    mysql> FLUSH PRIVILEGES;
    mysql> exit;
    
    $ service mysql restart
    

    Option 2: (replace YOUR_SYSTEM_USER with the username you have)

    $ sudo mysql -u root # I had to use "sudo" since is new installation
    
    mysql> USE mysql;
    mysql> CREATE USER 'YOUR_SYSTEM_USER'@'localhost' IDENTIFIED BY '';
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'YOUR_SYSTEM_USER'@'localhost';
    mysql> UPDATE user SET plugin='auth_socket' WHERE User='YOUR_SYSTEM_USER';
    mysql> FLUSH PRIVILEGES;
    mysql> exit;
    
    $ service mysql restart
    

    Remember that if you use option #2 you'll have to connect to mysql as your system username (mysql -u YOUR_SYSTEM_USER)

    Note: On some systems (e.g., Debian stretch) 'auth_socket' plugin is called 'unix_socket', so the corresponding SQL command should be: UPDATE user SET plugin='unix_socket' WHERE User='YOUR_SYSTEM_USER';

    Update: from @andy's comment seems that mysql 8.x.x updated/replaced the auth_socket for caching_sha2_password I don't have a system setup with mysql 8.x.x to test this, however the steps above should help you to understand the issue. Here's the reply:

    One change as of MySQL 8.0.4 is that the new default authentication plugin is 'caching_sha2_password'. The new 'YOUR_SYSTEM_USER' will have this auth plugin and you can login from the bash shell now with "mysql -u YOUR_SYSTEM_USER -p" and provide the password for this user on the prompt. No need for the "UPDATE user SET plugin" step. For the 8.0.4 default auth plugin update see, https://mysqlserverteam.com/mysql-8-0-4-new-default-authentication-plugin-caching_sha2_password/

    0 讨论(0)
  • 2020-11-22 10:35

    First step: go to /etc/phpmyadmin/config.inc.php then uncomment lines where you find AllowNoPassword . Second step: login to your mysql default account

    mysql -u root -p
    use mysql;
    update user set plugin="" where user='root';
    flush privilege;
    

    and that's all!

    0 讨论(0)
提交回复
热议问题