ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

前端 未结 30 1030
离开以前
离开以前 2020-11-22 02:25

I installed LAMP on Ubuntu 12.04 LTS (Precise Pangolin) and then set root password on phpMyAdmin. I forgot the password and now I am unable to login. When I try to chan

相关标签:
30条回答
  • 2020-11-22 03:15

    I am seeing all these answers, but none offer the option to reset the password and no accepted answer. The actual question being he forgot his password, so he needs to reset, not see if it's running or not (installed or not) as most of these answers imply.


    To reset the password

    Follow these steps (can be helpful if you really forget your password and you can try it anytime, even if you're not in the situation at the moment):

    1. Stop mysql

      sudo /etc/init.d/mysql stop
      

      Or for other distribution versions:

      sudo /etc/init.d/mysqld stop
      
    2. Start MySQL in safe mode

      sudo mysqld_safe --skip-grant-tables &
      
    3. Log into MySQL using root

      mysql -uroot
      
    4. Select the MySQL database to use

      use mysql;
      
    5. Reset the password

      -- MySQL version < 5.7
      update user set password=PASSWORD("mynewpassword") where User='root';
      
      -- MySQL 5.7, mysql.user table "password" field -> "authentication_string"
      
      update user set authentication_string=password('mynewpassword') where user='root';
      
    6. Flush the privileges

      flush privileges;
      
    7. Restart the server

      quit
      
    8. Stop and start the server again

      Ubuntu and Debian:

      sudo /etc/init.d/mysql stop
      ...
      sudo /etc/init.d/mysql start
      

      On CentOS, Fedora, and RHEL:

      sudo /etc/init.d/mysqld stop
      ...
      sudo /etc/init.d/mysqld start
      
    9. Login with a new password

      mysql -u root -p
      
    10. Type the new password and enjoy your server again like nothing happened

    This was taken from Reset a MySQL root password.

    0 讨论(0)
  • 2020-11-22 03:16

    Open the terminal and type:

    sudo apt-get purge mysql-client-core-5.6
    
    sudo apt-get autoremove
    
    sudo apt-get autoclean
    
    sudo apt-get install mysql-client-core-5.5
    
    sudo apt-get install mysql-server  
    

    Both MySQL database core client and MySQL Server packages will be the same version 5.5. MySQL Client 5.5 and MySQL Server 5.5 are the current "best" versions of these packages in Ubuntu 14.04 as determined by the package maintainers.

    If you would rather install MySQL Client 5.6 and MySQL Server 5.6 you can also find the mysql-client-core-5.6 and mysql-server-5.6 packages in the Ubuntu Software Center. The important thing is that the client and server version numbers match in either case.

    This worked for me.

    0 讨论(0)
  • 2020-11-22 03:17

    If you're using Amazon EC2, and you're having this problem on the instance, then you only need to do:

    sudo yum install mysql-server
    sudo service mysqld restart
    

    Amazon EC2 doesn't have a server installed (only the client is installed), so in case of that you need to install that on your instance, and after that try

     mysql -u root -p
    

    to check if that worked.

    0 讨论(0)
  • 2020-11-22 03:17

    In my case, It seems like I wasnt really able to kill the mysql process, when I run

    sudo service mysql stop
    ps -ef | grep mysql
    

    The mysql process was always there, it looks like it was blocking the socket file and new mysql process wasnt able to create it itself.

    so this helped

    cd /var/run
    sudo cp mysqld/ mysqld.bc -rf
    sudo chown mysql:mysql mysqld.bc/
    sudo service mysql stop
    sudo cp mysqld.bc/ mysqld -rf
    sudo chown mysql:mysql mysqld -R
    sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
    

    Now Im able to log in database using

    mysql -u root
    

    Then to update root password:

    UPDATE user SET authentication_string=password('YOURPASSWORDHERE') WHERE user='root';
    FLUSH PRIVILEGES;
    

    PS: I had trouble updating root passwod, seems like problem with "auth_socket" plugin, so I had to create new user with full privileges

    insert into user set `Host` = "localhost", `User` = "super", `plugin` = "mysql_native_password", `authentication_string` = NULL, `password_expired` = "N", `password_lifetime` = NULL, `account_locked` = "N", `Select_priv` = "Y",
    `Insert_priv` = "Y", `Update_priv` = "Y", `Delete_priv` = "Y", `Create_priv` = "Y", `Drop_priv` = "Y", `Reload_priv` = "Y", `Shutdown_priv` = "Y", `Process_priv` = "Y", `File_priv` = "Y",
    `Grant_priv` = "Y",  `References_priv` = "Y", `Index_priv` = "Y", `Alter_priv` = "Y", `Show_db_priv` = "Y", `Super_priv` = "Y", `Create_tmp_table_priv` = "Y", `Lock_tables_priv` = "Y",
    `Execute_priv` = "Y", `Repl_slave_priv` = "Y",  `Repl_client_priv` = "Y",  `Create_view_priv` = "Y", `Show_view_priv` = "Y", `Create_routine_priv` = "Y", `Alter_routine_priv` = "Y",
    `Create_user_priv` = "Y",  `Event_priv` = "Y", `Trigger_priv` = "Y", `Create_tablespace_priv` = "Y";
    

    This creates user "super" with no password and then you can connect with mysql -u super

    0 讨论(0)
  • 2020-11-22 03:18

    If you have XAMPP installed on your Linux machine, try to copy your my.cnf file from /opt/lampp/etc/my.cnf to /etc/my.cnf.

    Then, run the mysql -u root again... You should now have the correct socket and be able to run the MySQL client.

    0 讨论(0)
  • 2020-11-22 03:18

    i solved this problem with restart mysql

    /etc/init.d/mysql stop

    and

    /etc/init.d/mysql start

    that's it.

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