When using the root user to access the database locally, the mysql library is not displayed---Access denied for user ''@'localhost' to database 'mysql'

When using the root user to access the database locally, the mysql library is not displayed?

And create a database error

MariaDB [(none)]> create database mysql;
ERROR 1044 (42000): Access denied for user ‘’@‘localhost’ to database ‘mysql’

1. Problem background

  • When using the root user to access the database normally as follows

b14177396077b46b31d2dfe66c62a1b

You can see some system information of the mysql library

  • The error is as follows

755f5e46c414806277a5b0409954dd0

  • For example, I create a database permission error

image-20230215192511609

2. Solutions

Tried many methods, even reinstalling mysql found that it still failed, and encountered another problem: mysql uninstallation is not clean

How to uninstall mysql completely and cleanly, please refer to: https://blog.csdn.net/m0_49562857/article/details/129049754?spm=1001.2014.3001.5502

Later, I searched a lot of information and found that it was not the mysql library that was deleted, but all the root users and all related authorizations of the root users were deleted, which caused me to report an error when using the root user to access the database.

Solution:

  • close database
systemctl stop mariadb
  • Shield permission to start the database
mysqld_safe --skip-grant-table

image-20230215193529362

  • Start another linux window

    • 1. Enter the database
    mysql -uroot mysql
    
    • 2. Query all users and their authorization status in the current database
    select host,user,password from mysql.user; 
    

    9081d16bf328e33e57fee278d0b1882

    If you have a root user, please refer to step 4

    For non-root users, please refer to step 5

    • 3. Delete users with empty passwords
    delete from user where USER='';
    FLUSH PRIVILEGES; 
    
    • 4. After querying, it is found that there is a root user in the system to execute the following command
    UPDATE user SET Password=PASSWORD('新密码') where USER='root';
    FLUSH PRIVILEGES;
    
    • 5. After querying, it is found that there is no root user in the system to execute the following command
    grant all on *.* to 'root'@'localhost' identified by '新密码';
    flush privileges;
    

    083721fab348c295ed0e9b10eb43b52

  • kill the existing mysql process

  • Start the database query and find that the mysql library is displayed

image-20230215195500526

Guess you like

Origin blog.csdn.net/m0_49562857/article/details/129049653