Connect to mysql server without sudo

后端 未结 9 1173
暖寄归人
暖寄归人 2020-12-02 08:39

The command:

mysql -u root -p

gives the error:

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


        
9条回答
  •  自闭症患者
    2020-12-02 09:00

    You can use the same ROOT user, or a NEW_USER and remove the SUDO privileges. Below example shows how to remove connect using ROOT, without SUDO.

    Connect to MY-SQL using SUDO

    sudo mysql -u root
    

    Delete the current Root User from the User Table

    DROP USER 'root'@'localhost';
    

    Create a new ROOT user (You can create a different user if needed)

    CREATE USER 'root'@'%' IDENTIFIED BY '';
    

    Grant permissions to new User (ROOT)

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
    

    Flush privileges, so that the Grant tables get reloaded immediately. (Why do we need to flush privileges?)

    FLUSH PRIVILEGES;
    

    Now it's all good. Just in case, check whether a new root user is created.

    SELECT User,Host FROM mysql.user;
    
    +------------------+-----------+
    | User             | Host      |
    +------------------+-----------+
    | root             | %         |
    | debian-sys-maint | localhost |
    | mysql.session    | localhost |
    | mysql.sys        | localhost |
    +------------------+-----------+
    4 rows in set (0.00 sec)
    

    Exit mysql. (Press CTRL + Z). Connect to MySQL without SUDO

    mysql -u root
    

    Hope this will help!

提交回复
热议问题