ERROR 2002 (HY000): Can't connect to local MyS

2018-12-31 07:16发布

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 change password through terminal I get:

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

How can I fix this? I am unable to open LAMP, uninstall it or reinstall it.

30条回答
宁负流年不负卿
2楼-- · 2018-12-31 07:38

In my case it worked by doing some R&D:

I am able to connect to MySQL using

root-debian#mysql -h 127.0.0.1 -u root -p

But it's not working with mysql -u root -p.

I did not find any bind-address in my.cnf. So I outcommented the parameter socket=/var/lib/mysql/mysqld.sock in my.cnf which was causing me a problem with login.

After restarting the service it went fine:

root@debian:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.19 MySQL Community Server (GPL)
查看更多
路过你的时光
3楼-- · 2018-12-31 07:39

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

    update user set password=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.


Update taken from @Daniel's comment below:

In MySQL 5.7, the password field in mysql.user table field was removed, and now the field name is 'authentication_string', so step 5 should be:

 update user set authentication_string=password('mynewpassword') where user='root';
查看更多
浮光初槿花落
4楼-- · 2018-12-31 07:39

I tried the following steps:

  1. Log in as super user or use sudo
  2. Open /etc/mysql/my.cnf using gedit
  3. Find bind-address, and change its value to the database server host machine's IP address. For me, it was localhost or 127.0.0.1
  4. Save and close the file.
  5. Come back to terminal and execute sudo service mysql start

And it worked for me.

查看更多
美炸的是我
5楼-- · 2018-12-31 07:40

Somehow the MySQL server process did not create the socket, or the client is looking for the socket in the wrong place.

My first suggestion would be to check if the MySQL server is running. Second suggestion might be, is the MySQL server running on another host? If so, add the -h <hostname> flag to your MySQL client in the terminal.

If MySQL is indeed running, and running locally, check your my.cnf file. There should be a line like

socket = /var/run/mysqld/mysqld.sock

See if that matches the socket location that you mentioned in your post.

From experience, I would say the most likely scenario is your MySQL server either is not running at all or is not running on the same host as where you run your MySQL client from the terminal.

查看更多
ら面具成の殇う
6楼-- · 2018-12-31 07:42

By experience I say that you need to check if the server is running first and then try configuring MySQL. The last solution is to re-install MySQL.

查看更多
无色无味的生活
7楼-- · 2018-12-31 07:43

I had the same issue. Sometimes this happens if your MySQL service is turned down.

So you have to start it:

sudo service mysql start
查看更多
登录 后发表回答