633

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 Commented Aug 13, 2015 at 9:34
  • Reset the password by stopping MySQL and starting it in safe mode skipping the --grant tables Commented May 29, 2016 at 21:58
  • type this in your terminal " sudo apt-get install mysql-server" Commented 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 Commented Jul 14, 2017 at 6:39
  • 1
    systemctl start mariadb.service Commented Jan 4, 2019 at 15:20

94 Answers 94

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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.

Comments

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.

Comments

6

This issue happen because MySQL server was not started. check active status using below command.

service mysql status 

If the active status is stop, then ry starting the MySQL server,

service mysql start 

Comments

5

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.

Comments

5
mysqld stop mysql.server start 

Comments

5

The same on Ubuntu 14.04 (Trusty Tahr).

If you have installed XAMPP, installing mysql-server isn't the solution, because you'll access another MySQL!

You have to use the right socket to access. Usually it is this:

/opt/lampp/var/mysql/mysql.sock 

Instead, change it to:

/var/run/mysqld/mysqld.sock 

1 Comment

This is the only solution that worked for XAMPP
5

For CentOS Linux release 7.3 The mysql.sock file path is /var/lib/mysql/mysql.sock

Edit /etc/my.cnf file and put below entry This will solve your problem.

[client] user=root password=Passw0rd port=3306 socket=/var/lib/mysql/mysql.sock [mysqld] bind-address=0.0.0.0 

After this restart the service

service mysql restart 

2 Comments

This answer actually explains what is the root cause for the error
This will not "solve" the problem, it's a workaround, and binding 0.0.0.0 exposes your local MySQL to, well, everyone
4

I had the same problem. After much searching I didn't find any answer.

At last, I checked the /tmp directory, and its permissions were 755. I changed its permissions to 777 and mysqld started well without any problem.

Comments

4

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 

Comments

4

For me an update solved the problem:

On Ubuntu:

sudo apt-get update sudo apt-get upgrade 

On CentOS:

sudo yum update 

Comments

4

It works now...

I have followed the tutorial Installing MariaDB 10.1.16 on Mac OS X with Homebrew to overcome this issue.

But don't forget to kill or uninstall the old installation of MariaDB.

Comments

4

I got exactely the same error when I have stopped mysql service, and here how to solve it: by (re)starting mysql using these commands:

sudo systemctl start mysql 

or

sudo systemctl restart mysql 

1 Comment

sudo systemctl start mysql worked for me as well on Lubuntu 20.04.
3

Try restarting the server with

sudo /usr/local/mysql/support-files/mysql.server start 

If there is any error then follow the below steps

  1. mysqld

You will see the the below log. Notice the highlighted portion of the MySQL directory here

mysqld: Can't change dir to '/usr/local/mysql-5.7.14-osx10.11-x86_64/data/' (Errcode: 13 - Permission denied) 2016-10-04T14:09:19.392581Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-10-04T14:09:19.392847Z 0 [Warning] Insecure configuration for --secure-file-priv: Current value does not restrict location of generated files. Consider setting it to a valid, non-empty path. 2016-10-04T14:09:19.392921Z 0 [Note] mysqld (mysqld 5.7.14) starting as process 1402 ... 2016-10-04T14:09:19.397569Z 0 [Warning] Can't create test file
/usr/local/mysql-5.7.14-osx10.11-x86_64/data/Sudharshan.lower-test
2016-10-04T14:09:19.397597Z 0 [Warning] Can't create test file /usr/local/mysql-5.7.14-osx10.11-x86_64/data/Sudharshan.lower-test
2016-10-04T14:09:19.397712Z 0 [ERROR] failed to set datadir to /usr/local/mysql-5.7.14-osx10.11-x86_64/data/
2016-10-04T14:09:19.397776Z 0 [ERROR] Aborting
2016-10-04T14:09:19.397795Z 0 [Note] Binlog end
2016-10-04T14:09:19.397925Z 0 [Note] mysqld: Shutdown complete

  1. sudo chown -R _mysql:_mysql /usr/local/mysql-5.7.14-osx10.11-x86_64

Note the MySQL folder path /usr/local on the the previous log, and in my case it was mysql-5.7.14-osx10.11-x86_64, and you have to update it based on the log you get on your machine to provide read access to the MySQL directory

  1. sudo /usr/local/mysql/support-files/mysql.server start

Starting MySQL
SUCCESS!

Comments

3

Installing mysql-server solved the issue

sudo apt-get install mysql-server 

Comments

3

For me mysql wasn't running so I fixed it with

sudo /etc/init.d/mysql start 

Comments

2

In my case, the problem was page corruption in all my databases (check the mysql error log).

I solved it with Forcing InnoDB Recovery. The trick is editing /etc/mysql/my.cnf and adding

innodb_force_recovery = 4 

just below

[mysqld] 

And then restart mysql. After checking everything works correctly now, remove the line back again.

Comments

2

You could first check whether the service is running, with:

ps ax | grep mysql 

I got this response:

6104 pts/0 S 0:00 /bin/sh /usr/bin/mysqld_safe 6431 pts/0 Sl 0:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --pid-file=/var/run/mysqld/m 

No response means the service isn't running, so do:

service mysql start 

1 Comment

Proper way to check that would be to do sudo /etc/init.d/mysqld status or sudo service mysqld status
2

Just copy your /opt/lampp/etc/my.cnf file to /etc/mysql/my.cnf.

And in the terminal type:

mysql -u root 

You will be getting the mysql> prompt:

mysql> Update mysql.user set Password=PASSWORD('your_password') where user='root'; mysql> FLUSH PRIVILEGES; 

1 Comment

Welcome to StackOverflow Sudarsan. It is a Professional Q & A site. Please visit how to write a good answer. Although it is not wrong to do so but you are giving an answer to a question which was asked 2 years ago.
2

Install mysql-server:

sudo apt-get install mysql-server enter password as root 

Login:

mysql -u root -p root 

Here -u user name and -p password was given while installing MySQL server. It will work as it has worked for me.

1 Comment

Its about connecting mysql server using socket ,being used in PHP and languages like Ruby On Rails.
2

My server storage was full, this was preventing Mysql from starting. Got the idea from here. Increasing the HD and rebooting fixed the issue.

Comments

2

In my case, I'm not able to access mysql and after 3 days research, I have got a solution. This is perfect solution because I have searched /var/run/mysqld/mysqld.sock and I did not find the folder. You can run on putty the commands listed below.

sudo mkdir /var/run/mysqld/ sudo chown -R mysql:mysql /var/log/mysql sudo mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --socket=/var/run/mysqld/mysqld.sock sudo /etc/init.d/mysql restart 

You will save your valuable time.

Comments

1

I can't explain it, but in kubuntu 12.04.2 after

sudo apt-get autoremove linux-headers-3.2.0-37 linux-headers-3.2.0-37-generic

it started to work

Comments

1

To prevent the problem from occurring, you must perform a graceful shutdown of the server from the command line rather than powering off the server.

shutdown -h now 

This will stop the running services before powering down the machine.

Based on Centos, an additional method for getting it back up again when you run into this problem is to move mysql.sock:

mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak service mysqld start 

Restarting the service creates a new entry called mqsql.sock

Comments

1

I just had this problem now and solved it.

Although you installed mysql-server, the daemon needs to be running in order for the client to connect to it.

First check to see if mysql server is running:

netstat -tap | grep mysql 

You should see something like this:

$ sudo netstat -tap | grep mysql tcp 0 0 localhost:mysql *:* LISTEN 6639/mysqld 

If you don't have the server running, start the daemon by the following command:

/etc/init.d/mysql restart 

This should solve your problem if it's installed.

Comments

1

If you're using Ubuntu, it could be a matter of privileges.

Check your directory privileges. It's not enough to be in the root group, also use a chmod on directories which MySQL writes (for example, /var/run/mysqld/ for the creation of the mysqld.pid file).

This was helpful for me.

Comments

1

You are running locally, meaning that your client runs on the same machine as your server.

Make sure that your Unix user can actually reach/read /var/run/mysqld/mysqld.sock:

ls -als /var ls -als /var/run ls -als /var/run/mysqld ls -als /var/run/mysqld/mysqld.sock 

If not, check with your system administrator or database administrator to provide adequate read/execute access to those directories, or move the socket file elsewhere.

Comments

1

Also check your my.conf (/etc/mysql/my.cnf) and see if bind-address is set to 127.0.0.1.

If not, this might cause this issue.

Comments

1

I had the same issue. I found this.

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

This is because you are not running the mysqld daemon before launching the MySQL client. The file /var/lib/mysql/mysql.sock will be automatically created upon running the first instance of MySQL.

To fix:

First start the MySQL daemon, then type mysql:

/etc/init.d/mysqld start mysql 

Changing MySQL Root Password

By default, the root password is empty for the MySQL database. It is a good idea to change the MySQL root password to a new one from a security point of view.

mysql> USE mysql; mysql> UPDATE user SET Password=PASSWORD('newpassword') WHERE user='root'; mysql> FLUSH PRIVILEGES; 

Once done, check by logging in:

mysql -u root -p Enter Password: <your new password> 

1 Comment

Starting mysqld: [ OK ] but still its when i run mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
1

An advice: Always ask MySQL what the problem is. In my case, less /var/log/mysql/error.log and see this:

2015-07-28 12:01:48 23224 [ERROR] /usr/sbin/mysqld: unknown variable 'log_slow_queries=/var/log/mysql/mysql-slow.log' 2015-07-28 12:01:48 23224 [ERROR] Aborting 

It is complaining, because I uncommented this option in my.cnf, but after commenting this option, it started without any problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.