618

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.

9
  • Probably reinstall it is easier: stackoverflow.com/a/31984482/763744
    – Zernel
    Aug 13, 2015 at 9:34
  • Reset the password by stopping MySQL and starting it in safe mode skipping the --grant tables
    – davejal
    May 29, 2016 at 21:58
  • type this in your terminal " sudo apt-get install mysql-server"
    – Humphrey
    Feb 23, 2017 at 19:51
  • Start or restart mysql service and check. sudo /etc/init.d/mysql start or sudo /etc/init.d/mysql restart Jul 14, 2017 at 6:39
  • systemctl start mariadb.service
    – Thewebus
    Jan 4, 2019 at 15:20

92 Answers 92

371

I once had this problem and solved it by installing mysql-server, so make sure that you have installed the mysql-server, not the mysql-client or something else.

That error means the file /var/run/mysqld/mysqld.sock doesn't exists, if you didn't install mysql-server, then the file would not exist. So in that case, install it with

sudo apt-get install mysql-server

But if the mysql-server is already installed and is running, then you need to check the config files.

The config files are:

/etc/my.cnf
/etc/mysql/my.cnf
/var/lib/mysql/my.cnf

In /etc/my.cnf, the socket file config may be /tmp/mysql.sock and in /etc/mysql/my.cnf the socket file config may be /var/run/mysqld/mysqld.sock. So, remove or rename /etc/mysql/my.cnf, let mysql use /etc/my.cnf, then the problem may solved.

10
  • 123
    sudo apt-get install mysql-server to install mysql on ubuntu
    – towry
    Feb 4, 2014 at 12:36
  • 13
    If I had to explain technically , I would say 1)If you are running "mysql -u <user_name> -h <MySQL_Server_address> -p", you are actually running mysql client to access the mysql server. If you do not have mysql server installed on the address/IP address you mentioned with '-h', the above error will pop up. This is because it is not able to connect to MySQL server through the socket mysql.sock 2) If Mysql-server is already installed, it should be running.If it's not, you will observe the same error. So get it running on the server you specified. Feb 23, 2014 at 22:30
  • 3
    This can also occur if you try to change the directory where the database is stored, but imput the wrong directory in the configuration file (like a typo). Instead of telling you the typo directory does not exist, it will tell you that you lack permission to access it.
    – Michael
    May 15, 2015 at 19:15
  • 6
    As a first step I would recommend an attempt to restart the server process (see other's distinctions of server v.s client). Try sudo /etc/init.d/mysql restart. Usually, re-installation isn't necessary. I would recommend you to first check the log files. Not just mysql's log file but also /var/log/syslog since you may have had a server crash causing mysql to break down as a side effect.
    – Rein
    May 4, 2017 at 8:46
  • 7
    for me it was in etc/mysql/mysql.conf.d/mysqld.cnf . It was the only file where /var/run/mysqld/mysqld.sock was present
    – user13786942
    Oct 25, 2020 at 18:30
319

Try this:

mysql -h 127.0.0.1 -P 3306 -u root -p <database>

Also (to see if it's running):

telnet 127.0.0.1 3306 

Probably it is just a misconfiguration in the my.cnf file, in /etc/somewhere (depending on the Linux distribution).

8
  • 2
    I'm getting the same error. And I've checked and the default bind in both the main and my.cnf is set to 127.0.0.1 already. However using your command above allowed me to connect. Will have to keep hunting to see why just doing "mysql" is giving the error. Dec 27, 2013 at 20:24
  • 2
    I get telnet: Unable to connect to remote host: Connection refused as a result of telnet command. However apache still runs as I'm able to open localhost
    – Rishi Dua
    Mar 30, 2014 at 1:58
  • 1
    Note: mysql -h localhost does not work. Make sure to use mysql -h 127.0.0.1 as specified in the answer.
    – vadasambar
    Dec 5, 2019 at 9:50
  • 1
    How can I solve this if I get to login with the command in this answer? Apr 22, 2020 at 0:03
  • 3
    I am using mysql server on docker. localhost doesn't work, but 127.0.0.1 does. Oct 7, 2021 at 9:15
192

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 -u root
    
  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
  1. Login with a new password

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

This was taken from Reset a MySQL root password.

11
  • Updating the users password did not work for me however once logged into the MySQL monitor and having used the mysql database, repair table user use_frm worked for me to solve this problem. - stackoverflow.com/questions/4297592/… Feb 15, 2016 at 5:29
  • 23
    In MySQL 5.7, the password field in mysql.user table field was removed, now the field name is 'authentication_string', so (5) should be update user set authentication_string=password('mynewpassword') where user='root';
    – Daniel
    Jun 29, 2016 at 11:07
  • 5
    This worked great for me, but I first had to implement this solution: stackoverflow.com/questions/42153059/…
    – Andrew Fox
    Nov 27, 2017 at 4:13
  • 4
    For MySQL 8, use the following for Step 5: UPDATE mysql.user SET authentication_string=null WHERE User='root';
    – Joseph
    Oct 24, 2020 at 1:14
  • 2
    @JoeKreydt, in v.8 it is also possible to use the original example for v.5.7+, however with the minor adaptation of providing the new password value without the surrounding passowrd() argument.
    – gnzg
    Nov 10, 2020 at 21:45
76

In my case it was that the disk was full and mysqld couldn't start anymore.

Try to restart mysql service.

> service mysql restart

or

> service mysql stop

> service mysql start

If it doesn't recognize stop command then it's definitely the disk space. You should make some space in the partition mysql is allocated or make the disk larger.

Check the disk space with

> df -h
5
  • 3
    The same thing happened to me - full disk. Cleaned up some space, rebooted and problem went away.
    – Darren
    Jan 15, 2014 at 2:05
  • 3
    If that is your problem executing: "/etc/init.d/mysqld start" will tell you your disk partition is full
    – Tickon
    Apr 10, 2015 at 14:57
  • Same for me, disk 100%. Cleaned space and start service ok
    – Hugo
    May 29, 2018 at 18:46
  • Restarting mysql service worked for me, but I do not have disk space issues Jun 8 at 12:04
  • When I try to restart I get this error: Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xeu mysql.service" for details.
    – Ali Samie
    Oct 24 at 8:38
74

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
  • 6
    From this, I just ran sudo service mysql restart and it worked.
    – Orane
    Jun 24, 2015 at 16:37
  • In my case the error was the IP changed, so I set bind-address=localhost Aug 28, 2015 at 19:11
  • 3
    @rshahriar there is nothing called as bind-address in my /etc/my.cnf is it a good idea to add the field
    – RCBian
    Sep 12, 2015 at 12:32
  • @SilvioDelgado Not if you ever want to connect through ssh tunneling. Explanation here
    – Pathros
    Nov 17, 2016 at 15:42
  • restarting the mysql service worked for me as well, but still not sure where to find the error log in order to figure out the cause and try to prevent it from happening again...
    – Maleka
    Mar 9, 2019 at 10:40
44

I fixed this problem by executing the following command:

mysql.server start

And if you are on a mac and used brew to install mysql, simply use:

brew services start mysql
0
43

I had a similar problem. mysql wouldn't start:

sudo service mysql start
start: Job failed to start

If I disabled apparmor:

sudo aa-complain /etc/apparmor.d/*

the problem went away. The issue was that mysqld was trying to access /run/mysqld/mysqld.sock but the apparmor profile only gave permission to /var/run/mysqld/mysqld.sock (/var/run is symlinked to /run, so these are actually the same). Not sure why mysqld isn't using the var path since that's what's set in all the configuration files, but you can fix the problem by adding the following to /etc/apparmor.d/usr.sbin.mysqld

/run/mysqld/mysqld.pid rw,
/run/mysqld/mysqld.sock rw,
1
  • I didn't know that I had to call "sudo service mysql start". Last time I installed MySQL on Ubuntu, I think it autostarted upon installation. MariaDB on Manjaro requires the service to be explicitly started after installation.
    – dmiller309
    Aug 29, 2014 at 18:50
31

I solved this by killing the mysql process:

ps -ef | grep mysql
kill [the id]

And then I started the server again with:

sudo /etc/init.d/mysql restart

But start works as well:

sudo /etc/init.d/mysql start

Then I logged in as admin, and I was done.

3
  • well, I actually was aborting mysql before it crashes. Then I get " * Please take a look at the syslog." and "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)". Finally, I realize it's trying to recover itself, so I kill and restart it.
    – cwhsu
    Jun 16, 2015 at 6:09
  • There are (rare) situations where "mysql start" will fail, so you need to use the "restart" argument. If you need to kill a daemon process, its better to use the "stop" argument instead of the kill command. However, the killall command is more convenient than the kill command (killall mysqld).
    – Rein
    May 4, 2017 at 9:02
  • Killing MySQL process and then restarting MySQL worked for me, nice Aug 7, 2017 at 4:05
23

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.

0
22

I just experienced the same issue after I had to restart my production server. I am running Debian 8.1 (Jessie) on a DigitalOcean droplet.

This is what I did to resolve my issue:

  1. Check if the file /var/run/mysqld/mysqld.sock exists. If it doesn't, manually create it by entering touch /var/run/mysqld/mysqld.sock (which is what I had to do).

  2. So the MySQL process can use this file. Change ownership of said file by entering chown mysql /var/run/mysqld/mysqld.sock.

  3. Once '2' has been done, restart the MySQL service by entering service mysql restart or /etc/init.d/mysql restart.

After going through the above steps, my issue was solved. I rarely have this issue, and there is probably a better way, so by all means provide constructive feedback if need be :).

1
  • 1
    I noticed the file was not there and just restarted the mysql server. It automatically created the file and started up just fine. Thanks for the lead.
    – Sojurn
    Feb 16, 2016 at 6:18
21

Check the "bind-adress" parameter in my.cnf.

Else try with the command:

mysql -h 127.0.0.1 -P 3306 -u root -p
  • -h for host 127.0.0.1, that is, localhost

  • -P (notice -P as uppercase) for port 3306, that is, the default port for MySQL

15

Your mysql-server might not be running. Ensure it runs by typing mysql.server start into the terminal.

0
13

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.

1
  • Failed to restart mysqld.service: Unit mysqld.service not found. In Ubuntu 16.04
    – Ketav
    Sep 19, 2016 at 15:08
12

I think whenever you get the error

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

I will recommend first to check whether your mysql daemon is running... Most of the time it will not running by default. You can check it by /etc/init.d/mysqld status.

If it's not running then start it first:

.../etc/init.d/mysqld start.

I bet it will 110% work.

2
  • sudo /etc/init.d/mysql start is the correct solution also if you are using WSL since the upstart service is not supported: github.com/Microsoft/WSL/issues/745 Nov 12, 2020 at 2:46
  • Same idea, but alternative syntax: service mysqld start
    – Sinc
    Apr 20, 2021 at 18:12
12

Instead of using localhost:

mysql -u myuser -pmypassword -h localhost mydatabase

Use 127.0.0.1

mysql -u myuser -pmypassword -h 127.0.0.1 mydatabase

(also note, no space between -p and mypassword)

Enjoy :)

2
  • You can also not include your password from the command line for security by using this line: mysql -u myuser -p -h 127.0.0.1 mydatabase MySQL will ask you your password and not echo it.
    – DDay
    Jan 6, 2016 at 16:56
  • 1
    This solved it for me. However, I still wonder why 127.0.0.1 works while localhost does not?!
    – not2savvy
    Mar 16, 2017 at 8:59
12

Fist,try to restart it with

service mysql stop

service mysql start

If above not resolve the issue, now let's go...

Uninstall completely MySQL

sudo apt-get remove --purge mysql\*

reinstall it

sudo apt install mysql-server mysql-client

test if it run

sudo mysql

Install php drivers

sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-cgi php7.4-cli php7.4-common

You can replace php7.4 by php7.x or php8.0.12 or later Very nice !

❗️Be careful, you lose your data if they are not saved. Please backup your data if possible yet.

4
  • 4
    beware of data loss Mar 3, 2021 at 15:19
  • 2
    this doesnt work . you even lose data
    – NMukama
    Oct 1, 2021 at 6:47
  • 1
    This solutions looks like a Windows issue fix, not a Linux.
    – MFedatto
    Mar 22, 2022 at 22:45
  • Exactly, except at this time it worked perfectly for me on Ubuntu 16 and 18. I haven't tested it on later versions. Mar 26, 2022 at 19:18
11

Here's what worked for me:

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
service mysql restart

This creates a link.

1
11

Make sure you have backups of important databases and then try uninstall MySQL related stuff:

apt-get remove --purge mysql\*

Then install it again:

apt-get install mysql-server mysql-client

This worked for me and data was kept.

If PHP MySQL shows errors you might have to reinstall PHP MySQL:

apt-get install php5-fpm php5-mysql
1
  • I think that's the fatal weapon. I tried everything but, only your proposal is what helped me. Thank you ! Oct 26, 2019 at 19:46
9

I got this problem too, but I just did:

sudo service mysql restart 

It worked for me.

9

I also facing same problem it will be occur if your mysql server is not running by default it will again stop after some sec so you again run ($ sudo service mysql start ) command you can change if know.

for that use command

$ sudo service mysql start   

(put user password if required because we use sudo ) and then run

$ sudo mysql -u root -p          (put user password if required )

now you got your database

9

It seems your MYSQL is stopped. use below command to start MySQL again

sudo service mysql start
8

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
8

I FOUND THE SOLUTION

Before firing the command : mysql_secure_installation

  • Step 1: sudo systemctl stop mariadb
  • Step 2: sudo systemctl start mariadb
  • Step 3: mysql_secure_installation

Then it will ask root password and you can simply press Enter and set your new root password.

0
7

If your installation was recent, you should to confirm if your installation is the installation SERVER... as mysql-server-5.5.. Maybe you installed only "mysql" .. this is only client instead of the server.

7

On Debian server Jessie, my working solution was to simply do

service mysql restart
service mysql reload

as root user

0
7

i solved this problem with restart mysql

/etc/init.d/mysql stop

and

/etc/init.d/mysql start

that's it.

6

In my case, the default port 3306 was being used by some other process and thus it was not starting. After I stopped the other service and did sudo service mysql start, it worked fine. BTW, you can use something like sudo lsof -Pn -iTCP:3306 to see who may be using the port.

6

Check if you have the correct rights:

sudo chmod 755 /var/lib/mysql/mysql

I had the same problems and this worked for me. After doing this I was able to start MySQL.

6

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)
0
6

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

3
  • thanks for the answer and I able to log in to mysql, but there are no user table in my databases.
    – John Joe
    Oct 9, 2017 at 16:08
  • This solution works. But before you execute the sql statement to change the password for root, you need to select a database. Execute the following statement <br> use mysql; <br> and the you can now use <br> UPDATE user SET authentication_string=password('YOURPASSWORDHERE') WHERE user='root'; FLUSH PRIVILEGES;
    – Charle K.
    Aug 9, 2018 at 7:57
  • To update root password for mysql8 use: UPDATE mysql.user SET authentication_string=null WHERE User='root'; FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password_here'; FLUSH PRIVILEGES; exit;
    – Loctarogar
    Mar 28 at 9:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.