465

My problem started off with me not being able to log in as root any more on my mysql install. I was attempting to run mysql without passwords turned on... but whenever I ran the command

# mysqld_safe --skip-grant-tables &

I would never get the prompt back. I was trying to follow these instructions to recover the password.

The screen just looks like this:

root@jj-SFF-PC:/usr/bin# mysqld_safe --skip-grant-tables
120816 11:40:53 mysqld_safe Logging to syslog.
120816 11:40:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

and I don't get a prompt to start typing the SQL commands to reset the password.

When I kill it by pressing CTRL + C, I get the following message:

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

Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

If I retry the command and leave it long enough, I do get the following series of messages:

root@jj-SFF-PC:/run/mysqld# 120816 13:15:02 mysqld_safe Logging to syslog.
120816 13:15:02 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
120816 13:16:42 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

[1]+  Done                    mysqld_safe --skip-grant-tables
root@jj-SFF-PC:/run/mysqld#

But then if I try to log in as root by doing:

# mysql -u root

I get the following error message:

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

I checked and /var/run/mysqld/mysqld.sock file doesn't not exist. The folder does, but not the file.

Also, I don't know if this helps or not, but I ran find / -name mysqld and it came up with:

/var/run/mysqld - folder
/usr/sbin/mysqld - file
/run/mysqld - folder

I don't know if this is normal or not. But I'm including this info just in case it helps.

I finally decided to uninstall and reinstall mysql.

apt-get remove mysql-server
apt-get remove mysql-client
apt-get remove mysql-common
apt-get remove phpmyadmin

After reinstalling all packages again in the same order as above, during the phpmyadmin install, I got the same error:

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

So I tried again to uninstall/reinstall. This time, after I uninstalled the packages, I also manually renamed all mysql files and directories to mysql.bad in their respective locations.

/var/lib/mysql 
/var/lib/mysql/mysql
/var/log/mysql
/usr/lib/perl5/DBD/mysql
/usr/lib/perl5/auto/DBD/mysql
/usr/lib/mysql
/usr/bin/mysql
/usr/share/mysql
/usr/share/dbconfig-common/internal/mysql
/etc/init.d/mysql
/etc/apparmor.d/abstractions/mysql
/etc/mysql

Then I tried to reinstall mysql-server and mysql-client again. But I've noticed that it doesn't prompt me for a password. Isn't it supposed to ask for an admin password?

starball
  • 20,030
  • 7
  • 43
  • 238
dot
  • 14,928
  • 41
  • 110
  • 218
  • solved by just reboot the server, then restart the mysql service – bowpunya Jan 19 '17 at 08:09
  • 2
    @Anto This is not a duplicate. This one has `/var/run/mysqld/mysqld.sock` missing. The problem you linked has that file. – motorbaby Sep 12 '18 at 14:16
  • None of the answers here or anywhere else I could find solved my problem. As it was a new project I switched to PostgreSQL and it just works! :-) – James Bradbury Apr 08 '20 at 20:55

53 Answers53

423

Try this command,

sudo service mysql start
Joe Cheng
  • 8,804
  • 3
  • 26
  • 25
244

To find all socket files on your system run:

sudo find / -type s

My Mysql server system had the socket open at /var/lib/mysql/mysql.sock

Once you find where the socket is being opened, add or edit the line to your /etc/my.cnf file with the path to the socket file:

socket=/var/lib/mysql/mysql.sock

Sometimes the system startup script that launched the command line executable specifies a flag --socket=path. This flag could override the my.cnf location, and that would result in a socket not being found where the my.cnf file indicates it should be. Then when you try to run the mysql command line client, it will read my.cnf to find the socket, but it will not find it since it deviates from where the server created one. So, Unless you care where the socket resides, just changing the my.cnf to match should work.

Then, stop the mysqld process. How you do this will vary by system.

If you're super user in the linux system, try one of the following if you don't know the specific method your Mysql setup uses:

  • service mysqld stop
  • /etc/init.d/mysqld stop
  • mysqladmin -u root -p shutdown
  • Some systems aren't setup to have an elegant way to stop mysql (or for some reason mysql doesn't respond) and you can force terminate mysql with either:
    • One step: pkill -9 mysqld
    • Two step (least preferred):
      • Find the process id of mysql with either pgrep mysql or ps aux | grep mysql | grep -v grep
      • Assuming the process id is 4969 terminate with kill -9 4969

After you do this you might want to look for a pid file in /var/run/mysqld/ and delete it

Make sure the permissions on your socket is such that whatever user mysqld is running as can read/write to it. An easy test is to open it up to full read/write and see if it still works:

chmod 777 /var/run/mysqld/mysqld.sock

If that fixes the issue, you can tailor the permissions and ownership of the socket as needed based on your security settings.

Also, the directory the socket resides in has to be reachable by the user running the mysqld process.

Ray
  • 40,256
  • 21
  • 101
  • 138
  • 20
    Please don't recommend using `kill -9`; there are several ways that should be tried before it has to come to that. – Ignacio Vazquez-Abrams Aug 16 '12 at 16:17
  • @dot run this in file root 'find . -name mysqld.sock'. I have seen the socket file in other places than /var/run. Once you find it you might need to edit your my.cnf file to point to where it is found. – Ray Aug 16 '12 at 17:57
  • @ray, i tried to run the original command in a different way (i found a ubuntu specific post on another site..). Here's the commands I ran: "sudo service mysql stop" and "sudo mysqld --skip-grant-tables". After I do the second command, i get the following error: "InnoDB: Unable to lock ./ibdata1, error: 11". I can't find this ./ibdata1 file... InnoDB: Check that you do not already have another mysqld process – dot Aug 16 '12 at 18:24
  • 2
    @dot run `ps -aux | grep mysqld` and see if any other mysql processes are running – Ray Aug 16 '12 at 19:05
  • @ray. i'm rippng my hair out man. i decided to blow mysql away and retry... note my updated post. – dot Aug 16 '12 at 19:36
  • @dot I know this may sound repetitive, but can you see if this exists by any chance: /var/lib/mysql/mysql.sock ? Just cd into the directory, don't use find... if mysql is starting, it has to be creating a socket somewhere. – Ray Aug 16 '12 at 20:21
  • @dot I think find doesn't normally list sockets. I ran `sudo find / -type s` and found a list of socket files, and ended up finding `/var/lib/mysql/mysql.sock` amoung them. Then I changed my my.cnf file to point to that. – Ray Aug 16 '12 at 20:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15435/discussion-between-dot-and-ray) – dot Aug 16 '12 at 20:38
  • Even thou the mysql service was running, the search for socket("sudo find / -type s") didn't returned mysql socket. I stopped the mysql service and started it in safe mode 'sudo mysqld_safe'...now the socket search("sudo find / -type s") returned the mysql socket – latvian Sep 07 '13 at 16:31
  • Do NOT run `kill -9 4969` blindly! That will force-kill some process on your system that happens to have ID 4969 at the time, and it will most likely not be mysql. – Supernormal Jan 25 '17 at 07:41
  • @Supernormal The assumption is you know process id of mysql (4969 in this case). I'll clarify in the answer. – Ray Jan 25 '17 at 15:05
  • If you don't want (or can't) edit `/etc/my.cnf`, the `socket = ` instruction can also be written in your local directory `~/.my.cnf`. – Paul Rougieux Jul 18 '17 at 14:51
  • If nothing works, reinstall mysql server https://serverfault.com/questions/617098/how-to-get-mysql-sock-file-mistakenly-deleted-from-mysql-tmp – User123456 Feb 26 '20 at 08:57
  • When you get "[ERROR] Found option without preceding group in config file", make sure the socket line in your my.cnf is under the group heading: [mysqld] – James Bradbury Apr 08 '20 at 14:11
209

This error occurs due to multiple installations of mysql. Run the command:

ps -A|grep mysql

Kill the process by using:

sudo pkill mysql

and then run command:

ps -A|grep mysqld

Also Kill this process by running:

sudo pkill mysqld

Now you are fully set just run the following commands:

service mysql restart
mysql -u root -p

Have very well working mysql again

arghtype
  • 4,376
  • 11
  • 45
  • 60
Hammad Raza
  • 2,313
  • 1
  • 10
  • 11
  • Hey folks, I was running all over the web trying to fix this. My problem was that I was trying to setup docker. I wanted it to use the mysql data dir that was previously used by mysql outside of docker... that ps command was the best! – Michael Nov 13 '14 at 16:14
  • 7
    After running all over the web for nearly 4+ hours, this finally worked. Should be the top answer :+1: – Shivam Gaur Jan 17 '17 at 10:44
  • 11
    Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details. – John Joe Oct 09 '17 at 15:42
  • 3
    Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details. – Adib Aroui Aug 18 '18 at 12:13
  • Tnx, you save my time. – Aleksandur Atanasov May 28 '19 at 18:24
  • 2
    Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details. – Imad Oulhou Aug 28 '20 at 23:23
  • Short, well-explained and accurate. Should be the top answer. THANKS! – araujophillips Jan 30 '21 at 10:33
  • This definitely is the answer – Clarke Feb 07 '21 at 09:00
  • I recently went from Ubuntu 18.04.6 to 22.04 and believe something went wrong with the mysql installation during the upgrade. This was very helpful as it basically halted everything running related to it and allowed me to successfully reboot it. – Jem Aug 11 '22 at 23:34
45

The solution is way easier.

  1. First, you have to locate(in Terminal with "sudo find / -type s") where your mysql.sock file is located. In my case it was in /opt/lampp/var/mysql/mysql.sock
  2. Fire up Terminal and issue sudo Nautilus
    This starts your Files manager with super user privileges
  3. From Nautilus navigate to where your mysql.sock file is located
  4. Right click on the file and select Make Link
  5. Rename the Link File to mysqld.sock then Right click on the file and Cut it
  6. Go to /var/run and create a folder called mysqld and enter it
  7. Now right click and Paste the Link File
  8. Voila! You will now have a mysqld.sock file at /var/run/mysqld/mysqld.sock :)
Ioan Stef
  • 451
  • 4
  • 3
  • 6
    How do you locate your mysql.sock file? I tried find / -name 'mysql.sock' but it returns nothing. – Matthew Lock Mar 26 '13 at 03:11
  • 2
    @MatthewLock First off, make sure that mysql is running (by "ps aux | grep mysql"), otherwise, no mysql.sock would appear. Second, use "sudo find / -type s", which gives you all sockets on your system to locate mysql.sock – narengi Jun 29 '13 at 11:34
  • 1
    Report: This worked for me (although with different directories, since I'm using a different distribution): here's what I did: 1- checked at what location mysql tries to access the socket file (by reading the log file). For me, it was `/usr/var/mysqld/mysqld.sock` 2- made sure mysqld is running (by running `/where/your/mysql/is/mssqld_safe`) 3- searched for the location where mysql socket now sits (by `sudo find / -type s` let's just say it's on `/tmp/mysql.sock`) 4- made a soft link from where-it-is to where-it's-to-be (by `sudo ln -s /usr/var/mysql/mysql.sock /tmp/mysql.sock`) – narengi Jun 30 '13 at 08:26
  • 52
    What if the output of `sudo find / -type s` does not contain any `mysql.sock`? – jeff Feb 25 '15 at 15:16
  • 1
    It works. But I have no idea why these developers never do this job in past years? – Yi Jiang Aug 24 '15 at 01:12
  • 3
    If it's pain to get of where is mysql.sock through "find / -type s" - try to use "mysql_config --socket" command. You can install it with sudo apt-get install libmysqlclient-dev – alexche8 Jun 24 '16 at 13:38
  • 1
    @alexche8 well that very command tells me the sock file is located @ /var/run/mysqld. but that folder does not exist – phil294 Aug 03 '16 at 19:55
33

Just Need to Start MySQL Service after installation:

For Ubuntu:

sudo service mysql start;

For CentOS or RHEL:

sudo service mysqld start;
Pratik Patil
  • 3,662
  • 3
  • 31
  • 31
19

There is a bug on Ubuntu with MySQL 5.6 and 5.7 where var/run/mysqld/ would disappear whenever MySQL service stopped or is rebooted. This prevents MySQL from running at all. Found this workaround, which isn't perfect, but at least it gets it running after stopping/reboot:

mkdir /var/run/mysqld/
chown mysqld /var/run/mysqld/
motorbaby
  • 634
  • 7
  • 18
  • thank you - fixed it for me, I have to remake the directory and reset ownership on every restart of service mysql - have not been able to find the error reported anywhere officially or I would have posted link here :) – djoe Mar 22 '17 at 05:57
  • 1
    Haven't tried this, but maybe it will help you: Create a newfile /etc/tmpfiles.d/mysql.conf: `# systemd tmpfile settings for mysql # See tmpfiles.d(5) for details` and then, `d /var/run/mysqld 0755 mysql mysql -` After reboot, mysql should start normally. This is from https://bugs.launchpad.net/ubuntu/+source/mysql-5.6/+bug/1435823, comment 3. If you had this problem with MySQL 5.7, please comment on Launchpad. The more users that address the problem, the more attention it will get. – motorbaby Mar 30 '17 at 18:50
  • 1
    Worked for me... had to use mysql as username and not mysqld though – Evan Parsons Apr 17 '17 at 00:36
  • Adding line to `/etc/tmpfiles.d/mysql.conf` per motorbaby's comment fixed this issue for me. On a Centos 7 OS and using `mysql Ver 15.1 Distrib 10.3.11-MariaDB,`. – turrican_34 Dec 10 '18 at 11:42
  • This fixed the issue for me. – shikata Feb 17 '20 at 15:00
19

Make sure your inaccessible socket file path is same as '/var/run/mysqld/mysqld.sock', otherwise change the path as yours. Stop the mysqld

$ sudo /etc/init.d/mysqld stop

If the process still runing;

$ sudo pkill -9 mysqld

Remove the mysql directory where socket going to create. For me it did not allowed to remove, so I had to forcefully remove.

$ sudo mkdir -p /var/run/mysqld

Set the ownership to the dirctory

$ sudo chown mysql:mysql /var/run/mysqld

Start mysql

$ sudo /etc/init.d/mysql start

Trying to connect mysql

$ sudo mysql -u dbuser -p
Sadee
  • 3,010
  • 35
  • 36
17

Okay just copy and paste these codes: This should be done in the terminal, inside a server, when your mysql database is not properly installed, and when you are getting this error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'.

Stop MySql

sudo /etc/init.d/mysqld stop

Restart it or start it

sudo /etc/init.d/mysqld restart or sudo /etc/init.d/mysqld start

Make a link like this and give it to the system

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

Run a secure installation which guides all the process you need to do to configure mysql

/usr/bin/mysql_secure_installation
Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
coolcool1994
  • 3,704
  • 4
  • 39
  • 43
  • i get same `Can't connect` error when i try running above command. – cjmling Jan 10 '18 at 16:59
  • Just something I discovered, for those running ubuntu. If you start mysqld using /etc/init.d/mysqld start you will run into this issue anyway. Use service mysql start instead and it will be fine. Infuriating, but I figured it out in the end. – Adam Short Dec 02 '19 at 14:18
15

I faced same error and found that it was due to upgradation of packages, So after restarting my system I resolved error.

I think due to sql libraries/ packages update that error occured, So try this if you are doing some upgrading :)

Taimoor Changaiz
  • 10,250
  • 4
  • 49
  • 53
9

The answer of the user load step worked for me. Sometimes is need edit the file in /etc/mysql/my.cnf add line to client

[client]
password = your_mysql_root_password
port  = 3306
host  = 127.0.0.1
socket  = /var/lib/mysql/mysql.sock
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
user3002884
  • 91
  • 1
  • 1
9

There is a lots of reason for this issue, but sometimes just restart the mysql server, it will fix the issue.

sudo service mysql restart
Leopathu
  • 624
  • 6
  • 11
9

Why getting this error

I received new updates of mysql libraries so i updated my Kubuntu OS after that getting these errors.


Commands i tried and how i fixed it.

MySql-server is running correctly but when i tried to connect its giving

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'.

I checked /var/run/mysqld/mysqld.sock'. this directory. My files did not existed.

I also tried these commands to connect but did not worked for me.

 mysql -h 127.0.0.1 -P 3306 -u root -p

 sudo service mysql start

After wasting round about 2 hours i found the solution

sudo apt-get clean
sudo apt-get update
sudo apt-get upgrade -f

After that everything fixed for me.

  • This helped me to fix the problem. I dont understand how is it related to socked problem i had... – Čamo May 21 '20 at 17:38
  • I am getting the same error and follow the same steps to fix the above problem, but still getting the 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. , using ubuntu 22.04 latest version. – Rahul Gupta Nov 10 '22 at 03:57
  • I wish people would stop asking people to upgrade their operating system. – Owl May 12 '23 at 10:56
8

Temporary Solution

Maybe someone facing this problem. I am using Mysql Workbench on Ubuntu 14 and got this error.

mysqldump: Got error: 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) when trying to connect

Find your socket file by running sudo find / -type s, in my case it was /run/mysqld/mysqld.sock

So, I just created a link to this file in tmp directory.

sudo ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

Please note that this is a temporary solution since the file created will be under /tmp. See other answers for a permanent solution.

Bilal
  • 2,645
  • 3
  • 28
  • 40
  • As you can see the location of file is under `/tmp` folder so it'll be removed for sure. You can change `.sock` file location. See @user3002884 answer. – Bilal Aug 07 '16 at 10:26
  • I know, I was just completing your answer so other users know before that the solution is just temporary, as the folder – Alex Burdusel Aug 08 '16 at 07:44
8

Using XAMPP on ubuntu:

  1. Create a folder called mysqld inside /var/run directory. You can accomplish that using the command sudo mkdir /var/run/mysqld.

  2. Create a symbolic link to mysql.sock file that is created by the XAMPP server when it is started. You can use the command sudo ln -s /opt/lampp/var/mysql/mysql.sock /var/run/mysqld/mysqld.sock.

Note: The mysql.sock file is created when the server is started and removed when the server is stopped, so sometimes the link you created might appear to be broken but it should work as long as you have started the server using either sudo /opt/lampp/lampp start or any other means.

  1. Start the server if it's not already running and try executing your program again.

Good luck! I hope you'll get away with it this time.

  • @MohsinKhan go to localhost/phpmyadmin and import the database from there –  Mar 16 '19 at 10:53
8

I think your MySQL server has not started. So start the server using one of the following commands.

#services mysql start

or

#/etc/init.d/mysql start
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Susampath
  • 706
  • 10
  • 13
7

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

solutions

finally uninstall and reinstall mysql. **

  • sudo apt-get remove mysql-server
  • sudo apt-get remove mysql-client
  • sudo apt-get remove mysql-common
  • sudo apt-get remove phpmyadmin

then install again by

  • sudo apt-get install mysql-server-5.6

After this operation, 164 MB of additional disk space will be used.

  • Do you want to continue? [Y/n] Y press YES for complete installations

...... .......

  • At last you will get these lines....

    Setting up libhtml-template-perl (2.95-1) ...

    Setting up mysql-common-5.6 (5.6.16-1~exp1) ... Processing triggers for libc-bin (2.19-0ubuntu6) Processing triggers for ureadahead (0.100.0-16) ...

  • And then

    root@ubuntu1404:~# mysql -u root -p (for every password first u should use )

  • Enter password:

  • Note :Entered password should be same as the installation time password of mysql(like .root,system,admin,rahul etc...)

    Then type

  • USE rahul_db(database name);

Thanks.**

5

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

in /etc/my.cnf add this lines:

[client]
socket=/var/lib/mysql/mysql.sock <= this path should be also same as is[mysqld]

And restart the service with: service mysql restart

this worked for me

S.Spieker
  • 7,005
  • 8
  • 44
  • 50
karan
  • 89
  • 1
  • 1
5

This was mentioned a couple of times already, but this worked immediately for me:

service mysql restart

Jack BeNimble
  • 35,733
  • 41
  • 130
  • 213
5

I had the exactly same issue. After struggling for an hour, I found a way of correcting it without reinstalling mysql-common, mysql-client, mysql-server.

First of all, go to "/var/run/mysqld". You will find that the mysql.sock does not exist. Simply remove the entire mysqld directory and recreate it and provide it necessary privileges.

# rm -rf /var/run/mysqld && mkdir /var/run/mysqld && chown mysql /var/run/mysqld/

Now, Kill the mysql process in it's entirety. It might be possible that it will show you "waiting for page cleaner" on running "/etc/init.d/mysql status" command even after shutting down the service.

To completely close the service, use

# pkill -9 mysqld

Once the process is killed, try starting it again using

# /etc/init.d/mysql start

And you will see that it works good! And also there will be no issue in stopping it too.

xscorp7
  • 303
  • 4
  • 11
4

you can find mysqld.sock in /var/run/mysqld if you have already installed mysql-server by sudo apt-get install mysql-server

spencer
  • 434
  • 4
  • 8
4

I just had this problem on Ubuntu 14.10

Turns that mysql-server was no longer installed (somehow it had been removed) but I couldn't just install it because there were some broken packages and dependency issues/conflicts.

In the end I had to reinstall mysql

sudo apt-get remove mysql-client
sudo apt-get install mysql-server
Dew
  • 97
  • 9
4

My solution;

Ubuntu 18.04 (WSL)

/etc/mysql/my.cnf

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

/etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306

I changed the port. It's worked for me. You can write another port. Example 3355

omfaer
  • 184
  • 3
  • 16
3

In My case two mysqld processes were running.. killed the optional processs by using pkill -9 mysqld

amindri
  • 1,137
  • 8
  • 12
3

If you have a lot of databases and tables on your system, and if you have innodb_file_per_table set in my.cnf, then your mysql server might have run out of opened objects / files (or rather the descriptors for these objects) Set a new max number with

open-files-limit = 2048

and restart mysql. This approach might help when the socket is not created at all, but really this might not not be the real problem, there is an underlying problem.

knb
  • 9,138
  • 4
  • 58
  • 85
3

I am using XAMPP on Ubuntu. I found this error when connecting database through terminal. I solve it without any configuration because default socket file path in XAMPP is written in "/opt/lampp/etc/my.cnf" as following:

[client]
#password   = your_password
port        = 3306
socket      = /opt/lampp/var/mysql/mysql.sock

now you can connect just by giving this socket path parameter with mysql command on terminal like:

mysql -u root --socket /opt/lampp/var/mysql/mysql.sock

and it's done without any configuration.

If you don't want to type socket path everytime, then go for changing default path in my.cnf by "/var/run/mysqld/mysqld.sock". Provide permissions and restart mysql server.

Edit: Recently I've installed Ubuntu 20.04 and trying to install MySQL server but my system were crashing and not working at all. So I've just completely removed MySQL and installed MariaDB. Its working like a charm without any problems.

Ani
  • 198
  • 1
  • 9
3

You must install mysql-server

apt install mysql-server
Darwin
  • 1,695
  • 1
  • 19
  • 29
2

Changing the host to 127.0.0.1 worked for me.

Edit the file in /etc/mysql/my.cnf and add the below mentioned line to the section: client

[client]
port  = 3306
host  = 127.0.0.1
socket  = /var/lib/mysql/mysql.sock

After you are done with it. Execute the following command.

sudo service mysql start
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
2

I had similar problem on a CentOS VPS. If MySQL won't start or keeps crashing right after it starts, try these steps:

1) Find my.cnf file (mine was located in /etc/my.cnf) and add the line:

innodb_force_recovery = X

replacing X with a number from 1 to 6, starting from 1 and then incrementing if MySQL won't start. Setting to 4, 5 or 6 can delete your data so be carefull and read http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html before.

2) Restart MySQL service. Only SELECT will run and that's normal at this point.

3) Dump all your databases/schemas with mysqldump one by one, do not compress the dumps because you'd have to uncompress them later anyway.

4) Move (or delete!) only the bd's directories inside /var/lib/mysql, preserving the individual files in the root.

5) Stop MySQL and then uncomment the line added in 1). Start MySQL.

6) Recover all bd's dumped in 3).

Good luck!

Heitor
  • 683
  • 2
  • 12
  • 26
2

I uninstalled mysql in Ubuntu 16.04 https://askubuntu.com/questions/172514/how-do-i-uninstall-mysql

I reinstalled mysql https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-16-04

This seemed to work.

Pranay Aryal
  • 5,208
  • 4
  • 30
  • 41
2

First create dir /var/run/mysqld

with command:

mkdir -p /var/run/mysqld

then add rigths to the dir

chown mysql:mysql /var/run/mysqld

after this try

mysql -u root
Ingo
  • 5,239
  • 1
  • 30
  • 24
1

I would also check the mysql configuration. I was running into this problem with my own server but I was a bit too hasty and misconfigured the innodb_buffer_pool_size for my machine.

innodb_buffer_pool_size = 4096M

It usually runs fine up to 2048 but I guess I don't have the memory necessary to support 4 gigs.

I imagine this could also happen with other mysql configuration settings.

Greg
  • 5,422
  • 1
  • 27
  • 32
1

Try the following at a terminal prompt:

sudo mysql

Once that lets you in, you can create a new user and grant privileges that you want on the specific database they need access to.

Mysql 5.7 changed some things and by default uses the auth_socket plugin (as opposed to mysql_native_password) for root to protect the account from getting hacked. You can override this by setting the plugin field for root, but unless you have a very good reason you probably shouldn't circumvent the protection. Especially when sudo mysql is easier than mysql -u root -p anyway.

I found out this info - of all places - from a Raspberry Pi help site. Worked like a charm after Lubuntu 18.04 annoyed the heck out of me for a couple hours.

JohanTux
  • 382
  • 4
  • 11
1

Sometimes it's just that you don't have enough space on server, just free up some space (like clearing log files), and start mysql:

/etc/init.d/mysql start

Prashant
  • 5,331
  • 10
  • 44
  • 59
1

my situation is that the mysql in the /etc/ directory is renamed mysql_bk, just copy mysql_bk

sudo cp -r /etc/mysql_bk/ /etc/mysql/

then

service mysql restart
xiaopo
  • 627
  • 5
  • 4
  • As with every other answer in this thread --- this can be attributed to a socket to MySQL being unavailable. This answer is unlikely to be of more use or help to others, because if you don't have mysql available at all, as in this poster's answer, then of course the socket will not be available. Others are not going to have that same mysql_bk issue which was clearly a user-initiated thing. – Sean Halls Jul 08 '20 at 08:21
0

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

Sometimes it is due to when mysql server goes down , in live server i think you should have to restart server and also in localhost you need to restart mysql server .

0

I run my MySQL on a virtual machine in Ubuntu, So what had happened was when I restarted my host and the VM, The IP had changed. I had configured mysql to run on IP 192.168.0.5 and now due to dynamic allocation of IP, my new IP was 192.168.0.8

If you have the same problem just check your ip with the command ifconfig.

Check your MySQL binding with the command cat /etc/mysql/my.cnf | grep bind-address

If both IP are the Same, then reinstall your mysql server

If not, then change your IP in /etc/network/interfaces using nano, vi, vim or anything of your preference.

I prefer sudo nano /etc/network/interfaces

and enter the following

auto eth0
iface eth0 inet static
address 192.168.0.5
netmask 255.255.255.0

Save the interfaces file, restart your interface sudo ifdown eth0 && sudo ifup eth0 replace "eth0" with your network interface

Restart MySQL sudo service mysql stop followed by sudo service mysql start

If you have the same issue as mine, You are good to go!

0

This solution can help others, sometimes the fix is very simple.

In your live environment restart mysql server and apache

sudo service mysql restart
sudo apache services restart

this is a common problem when you install a plugin or execute any database commands.

tbraun89
  • 2,246
  • 3
  • 25
  • 44
NadZ
  • 1,024
  • 9
  • 10
0

on archlinux I faced this error and the problem was that the mysqld-service wasn't running.

However I couldn't enable the mysql-service with systemctl start mysqld as instructed in the Archlinux documentation. I think the error was something like

mysqld.service not found

I first needed to restart the system.

Then I entered the command again (and entered systemctl enable mysqld) and now mysql was able to find the created mysql.sock.

ArchLinuxTux
  • 840
  • 1
  • 11
  • 28
0

I had this type of problem when i resize the droplet on digitalocean. I had upgrade the mysql version and works for me. Here is the tutorial what i applied - https://serverpilot.io/community/articles/how-to-upgrade-mysql-5.5-to-5.6-on-ubuntu-14.04.html

Sam
  • 9
  • 1
  • 3
0

I tried almost all the listed solutions, none worked for me until I restarted the machine and then mysql server restarted when I issued the command "service mysql restart".

SyCode
  • 1,077
  • 4
  • 22
  • 33
0

In my case the problem was the bind addresses in /etc/mysql/my.cnf, then:

nano /etc/mysql/my.cnf

search bind addresses and remove the specific with the host ip (not the 127.0.0.1)

barresoft
  • 11
  • 4
0

The mysql.sock file is created when MariaDB starts and is removed when MariaDB is shutdown. It won't exist if MariaDB is not running. maybe you didn't install MariaDB. YOU COULD FOLLOW THE INSTRUCTION BELOW: https://www.linode.com/docs/databases/mariadb/how-to-install-mariadb-on-centos-7 BEST

jsina
  • 4,433
  • 1
  • 30
  • 28
0

Just share with you guys, I faced same issue and got resolved using below command

sudo ln -s /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock

I hard link both the locations from my my.cnf and error

I got this answer from enter link description here

Arvind Pant
  • 401
  • 5
  • 9
0

For those who are using MySql via Vitess.

I received this error at the vttablet component due to a syntax error (forgot to add ; to end of SQL command) passed to init_db.sql that is given as example in get started docs:

apiVersion: v1
kind: Secret
metadata:
  name: my-cluster-config
type: Opaque
stringData:
  users.json: |
    {
      "user": [{
        "UserData": "user",
        "Password": "pass"
      }]
    }
  init_db.sql: |
    # This file is executed immediately after mysql_install_db,
    # to initialize a fresh data directory.
    CREATE DATABASE IF NOT EXISTS my_db <--- No ';' at the end

Full error log:

TabletManager.ReplicationStatus()(on zone1from ) error: net.Dial(/vt/socket/mysql.sock) to local server failed: dial unix /vt/socket/mysql.sock: connect: no such file or directory (errno 2002)

Rot-man
  • 18,045
  • 12
  • 118
  • 124
0

I had same problem. after removing and reinstalling mysql this issue did happen.

CAUTION: you're DATA will be DELTED!!! unfortunately i didn't find a way to get backup of mysql files when mysql is broken (if you know a way to do this please note in comments).
Just use this method if your data in mysql does not matter to you (like me when i was trying to install mysql in new installed OS).

Problem:

What did cause the problem? first i used apt remove to reinstall the mysql but this job caused error in mysql:

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' -- Missing /var/run/mysqld/mysqld.sock

Fix:

Then i used command purge to reinstall the mysql and it worked:

  1. sudo apt purge mysql* (this will clean all of your data!);
  2. sudo apt autoremove
  3. sudo apt install mysql-server (add what application you need!)

After doing these steps just run sudo mysql -u root.

Amin.Qarabaqi
  • 661
  • 7
  • 19
  • Will this solution work for MySQL 5.7.x version? I'm asking this because when you install `mysql-server` package it always defaults to MySQL 8.x version. – NikolaS Feb 09 '23 at 11:23
0

if you have "Unit mysql.service not found" error, you should install "sudo apt-get install mysql-server"

Umut Can Arda
  • 81
  • 1
  • 8
0

'/var/run/mysqld/mysqld.sock' (2)' on pop os

If you are on pop-os, do this

Remove and purge any prior installed mysql server. Then install it and configure as below.

sudo apt update
sudo apt install mysql-server
sudo mysql_secure_installation
David Kariuki
  • 1,522
  • 1
  • 15
  • 30
0

I had the same issue and after hours I found this: I added this to path /etc/mysql/my.cnf

[mysql]
innodb_force_recovery = 1

If you use mysqld use this:

[mysqld]
innodb_force_recovery = 1
Kamuran Sönecek
  • 3,293
  • 2
  • 30
  • 57
0

In my case the exact same error occurred because I installed mysql-server by mistake, while mariadb-server was already installed.

Tried many things, and after 2 hours my solution was to uninstall mysql-server and install mariadb-server again.

dimitris tseggenes
  • 3,031
  • 2
  • 16
  • 23
0

I was running mysql in a docking container (Ubuntu 22.04). The solution for me to was to stop and start mysql as follows:

root@9178d9792221:/# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

root@9178d9792221:/# /etc/init.d/mysql stop
 * Stopping MySQL database server mysqld                                                                                                                                                                                                [ OK ] 
root@9178d9792221:/# /etc/init.d/mysql start
 * Starting MySQL database server mysqld                                                                                                                                                                                                       su: warning: cannot change directory to /nonexistent: No such file or directory
                                                                                                                                                                                                                                        [ OK ]
root@9178d9792221:/# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.33-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye

And just like that, it's fixed.

Owl
  • 1,446
  • 14
  • 20
-1

locate the my.cnf file with the mysql config mine was under /etc/mysql/my.cnf (change the location where it says socket accordingly)

port            = 3306
socket          = /tmp/mysql.sock

Here is entries for some specific programs The following values assume you have at least 32M ram

This was formally known as [safe_mysqld]. Both versions are currently parsed.

[mysqld_safe]
socket          = /tmp/mysql.sock
nice            = 0
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
-1

In lampp as i recognize it does not identify mysql commands. try with

sudo /opt/lampp/bin/mysql

it will open the mysql terminal where you can communicate with databases..

Hemantha
  • 353
  • 2
  • 7
-1

The only thing that worked for me was uninstalling MYSQL completely from my system. So i did the following:

sudo apt-get remove mysql-server -y
sudo rm -r /var/lib/mysql
sudo rm -r /etc/mysql
sudo apt-get remove mysql-client -y
sudo apt-get remove mysql-common -y
sudo apt-get remove phpmyadmin -y
sudo apt-get install mysql-server -y
ToastedGuy2
  • 113
  • 3
  • 10