Connect to mysql server without sudo

后端 未结 9 1169
暖寄归人
暖寄归人 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 08:44

    You need to change algorithm. Following work for me,

    mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
    mysql > FLUSH PRIVILEGES;
    
    0 讨论(0)
  • 2020-12-02 08:52

    You can use the below query:

    GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';
    

    This query is enough.

    0 讨论(0)
  • 2020-12-02 08:54

    This answer needs to be slightly adapted for mariaDB instead of mysql.

    First login as root using sudo:

    $ sudo mysql -uroot
    

    Then alter the mariadb root user:

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password USING PASSWORD('mypassword');
    FLUSH PRIVILEGES;
    

    From now on sudo is not longer needed:

    $ mysql -uroot -p
    

    Version used: mysql Ver 15.1 Distrib 10.4.13-MariaDB, for osx10.15 (x86_64) using readline 5.1

    0 讨论(0)
  • 2020-12-02 08:58

    The error Message:

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

    means that the Server not allow the connect for this user and not that mysql cant access the socket.

    try this to solve the problem:

    Login in your DB

    sudo mysql -u root -p
    

    then make these modifications:

    MariaDB []>use mysql;
    MariaDB [mysql]>update user set plugin=' ' where User='root';
    MariaDB [mysql]>flush privileges;
    MariaDB [mysql]>exit
    

    try login again without sudo

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-02 09:01

    first login to your mysql with sudo.

    then use this code to change "plugin" coloumn value from "unix_socket" or "auth_socket" to "mysql_native_password" for root user.

    UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE user = 'root' AND plugin IN ('unix_socket', 'auth_socket');
    
    FLUSH PRIVILEGES;
    

    finally restart mysql service. that's it.

    if you want more info, check this link

    UPDATE:

    In new versions of mysql or mariadb you can use :

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password USING PASSWORD('your-password');
    FLUSH PRIVILEGES;
    
    0 讨论(0)
提交回复
热议问题