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

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.

This question is related to mysql phpmyadmin lamp forgot-password mysql-error-2002

The answer is


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.


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,

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.


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


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.


SOLUTION 1 Purge out (or remove if you want to keep databases) any mysql packages to repeat the installation anew:

sudo apt purge "mysql*"

-Autoremove packages

sudo apt autoremove

-Stop the apparmor service

sudo systemctl stop apparmor

-Make apparmor drop its profiles (I thought with stop it was enough, but for me it wasn't. With systemctl it doesn't work)

sudo service apparmor teardown

-Reinstall mysql-server

sudo apt install mysql-server

-Install apparmor-utils, to create a profile for mysql in apparmor that allows mysql to run

sudo apt install apparmor-utils

-Check the status of mysql-server (must be Active(running))

sudo systemctl status mysql

-Generate a profile for mysql in apparmor

sudo aa-genprof mysql

-In other terminal run mysql (enter root password)

mysql -u root -p

-do things in MySQL while apparmor is generating the profile in the other terminal

mysql> CREATE DATABASE fooDB

-Swith to the other terminal and press "s" (the prompt tells you it's for "scan")

-Say yes to the policies from apparmor you see fit (I guess all of them for mysql), say yes pressing "a" for Allow

-Press "f" to Finish the apparmor profile

-Restart the apparmor service

sudo systemctl start apparmor

-Check to see if you still can use mysql in the other terminal

mysql>exit

mysql -u root -p

If all is well you can use mysql from the command line.


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.


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!


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

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


This answer is related to updating to MySQL 5.6 on machines with a small amount of RAM

I had the same problem when upgrading from MySQL 5.5 to 5.6 on my Debian 8 (Jessie). MySQL was not started (the status was showing active/exited) and simply making service mysql start did not work, because as I found from the /var/logs/mysql/error.log log file:

InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(136019968 bytes) failed; errno 12
Cannot allocate memory for the buffer pool

The memory was not enough: I had only 256 MB of RAM.

In MySQL there is a setting, performance_schema. By default, it is turned off in MySQL 5.5.

https://dev.mysql.com/doc/refman/5.5/en/performance-schema-startup-configuration.html

But in MySQL 5.6 the default is on, and simply by adding the following line in /etc/mysql/my.cnf file and restarting, it worked.

performance_schema = off

Warning: Turning this setting off you might experience performance issues, but I guess in a development environment it won't be a problem.

Also, here is an article that might be helpful configuring MySQL to use minimal memory, Configuring MySQL to use minimal memory.


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


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

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.


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


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

It may be an issue with the configuration file. I had a similar problem, and I couldn't find a solution on the web. I noticed that I had two my.cnf files, one in /etc/mysql and the other in /etc. Follow the steps below:

  1. Check for my.cnf files on your computer using locate my.cnf.

  2. If there are two entries, i.e. /etc/my.cnf and /etc/mysql/my.cnf, rename /etc/mysql/my.cnf to something else, e.g /etc/mysql/my.cnf.old

Try to run MySQL again.


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.


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)

i solved this problem with restart mysql

/etc/init.d/mysql stop

and

/etc/init.d/mysql start

that's it.


In my case I was missing mysql-server. So after installing it via sudo apt-get install mysql-server I was able to connect again.


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.


In /etc/mysql/my.cnf, check the last line be:

!includedir /etc/mysql/conf.d/

I was having this problem too and neither of these answers helped me. The issue was different, but the error was the described by the OP.

I check the logs of MySQL in /var/log/mysql, and I saw this:

150309  5:03:19 [ERROR] /usr/sbin/mysqld: unknown variable   'lower_case_tables_names=1'

I opened the /etc/mysql/my.cnf file and outcommented that line #. After doing that, I was able to connect to the database.

Honestly I don't know what the problem was. The linode server was scheduled to restart due to maintenance, and this error came out of nowhere.


Did you check if LAMPP is running?

sudo bash <path>/lampp start

For me, path is

sudo bash /opt/lampp/lampp start

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.


In Ubuntu 18:10 Linode 1GB Ram, I experienced this error. After examining /var/log/mysql/error.log , I came across this:

[Note] InnoDB: innodb_empty_free_list_algorithm has been changed to legacy because of small buffer pool size. In order to use backoff, increase buffer pool at least up to 20MB.

I upgraded my linode to 2GB and restarted mariadb with sudo mysql. Next mysql_secure_admin ran, but the root password did not set for the user-as ususl unitl changing the root user to use the mysql_native_password plugin. Not sure, but it appears that the sock was created, but the server shutdown due to the lack of memory in my VPS.


This error 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 in the second drive as D instead of the accurate D_). Instead of telling you the typo directory does not exist, it will tell you that you lack permission to access it (leading you to try to change the permissions for the typo directory, which it will let you do). So if you get this error while changing directories, double check the configuration file and make sure you don't have a typo.


Another cause of this issue, surprisingly, is the deletion of the folder /var/log/mariadb This was my case after i deleted this log folder. After the creation, the error disappeared.


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


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


This answer is likely to drown here, but maybe somebody stumbles upon it accidentally.

In my case, SELinux prevented the user/application from connecting to the MySQL (MariaDB) server socket. On RHEL, check /var/log/audit/audit.log if you have SELinux enabled.


I my case it was just because mysql was stopped because of the missing /var/log/mysql folder defined in /etc/mysql/my.cnf. After creating it I could start mysql and it ran as usual.


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.


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.


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.


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

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>

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.


I solved this problem by removing this line from my /etc/mysql/my.conf in the mysqld section ([mysqld]):

default-character-set=utf8

Restart and it works fine.


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.


You should verify the owner of the group for /var/run/mysqld. If it isn't mysql.mysql, then do:

su root
chown mysql.mysql /var/run/mysqld

For me an update solved the problem:

On Ubuntu:

sudo apt-get update
sudo apt-get upgrade

On CentOS:

sudo yum update

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

Upgrading MySQL fixed it for me. On RHEL-based servers, just run:

sudo yum upgrade mysql-server

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

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

sudo /etc/init.d/mysql start

I got this problem too, but I just did:

sudo service mysql restart 

It worked for me.


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).


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 :)


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


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

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.


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;

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

service mysql restart
service mysql reload

as root user


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

Very nice !


Here's what worked for me:

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

This creates a link.


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.


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.


I had to say:

sudo /etc/init.d/mysqld start

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.


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.


I had this on Ubuntu and as I've figured out, there was more than one instance of mysqld.

It looked like the previous one had not been completely stopped, while the new one has already started. Running '/etc/init.d/mysql stop' didn't help, it was always returning 'OK' and a new instance has been launched automatically immediately after that:

        $ sudo /etc/init.d/mysql stop
         * Stopping MySQL database server mysqld        [ OK ] 
        $ pgrep mysql
        28315
        $ sudo /etc/init.d/mysql stop
         * Stopping MySQL database server mysqld        [ OK ] 
        $ pgrep mysql
        28570
        $ sudo /etc/init.d/mysql stop
         * Stopping MySQL database server mysqld        [ OK ] 
        $ pgrep mysql
        28763
        ..... etc ...

Fortunately, the following command has fixed the problem:

    $ sudo service mysql stop
    mysql stop/waiting

    $ ps -ef | grep mysql
    29841 26858  0 10:59 pts/8    00:00:00 grep --color=auto mysql <--- IT's gone ! 

After that I was able to start mysql again and see that mysql.sock has been successfully created.


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.


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.


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.


Simple solution on my server: After migrating to a new Debian 7 server with my MySQL databases, the second local IP address, 127.0.1.1, was missing in my hosts file. Adding this resolved the warnings:

echo -e "\n127.0.1.1       $(hostname)" >> /etc/hosts

Installing mysql-server solved the issue

sudo apt-get install mysql-server

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

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

sudo service mysql start

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.


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 :).


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.


You are missing permission to create /var/run/mysqld directory.So please create and give permission as following.

  • mkdir -p /var/run/mysqld
  • chown mysql:mysql /var/run/mysqld

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.


In my case, i had files corrupted in my folder of mysql data

mv /var/lib/mysql /var/lib/mysql_old

so as i had a backup, i moved the directory to "_old", and started the docker again. it created a new folder mysql with clean data, and the socket worked.


For me it was:

Open /etc/mysql/my.cnf or /etc/my.cnf and search for 'bind-address'. It was 127.0.0.1. I converted it to localhost, so the line result should be 'bind-address = localhost'.

Otherwise, you should run the your MySQL server with an IP address that existed in the bind-address directive, i.e. mysql -h 127.0.0.1.


sudo touch /var/lib/mysql/.force_upgrade
sudo rcmysql restart

worked for me when I had this issue


mysqld stop
mysql.server start

Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to phpmyadmin

phpMyAdmin on MySQL 8.0 phpmyadmin - count(): Parameter must be an array or an object that implements Countable Error loading MySQLdb Module 'Did you install mysqlclient or MySQL-python?' phpMyAdmin ERROR: mysqli_real_connect(): (HY000/1045): Access denied for user 'pma'@'localhost' (using password: NO) phpMyAdmin access denied for user 'root'@'localhost' (using password: NO) mysqli_real_connect(): (HY000/2002): No such file or directory How to create a foreign key in phpmyadmin #1292 - Incorrect date value: '0000-00-00' MySQL error - #1932 - Table 'phpmyadmin.pma user config' doesn't exist in engine phpMyAdmin Error: The mbstring extension is missing. Please check your PHP configuration

Examples related to lamp

How to enable SOAP on CentOS What is Options +FollowSymLinks? ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) What is a LAMP stack? How do I change the root directory of an Apache server? PHP errors NOT being displayed in the browser [Ubuntu 10.10] How can I enable cURL for an installed Ubuntu LAMP stack? PDOException “could not find driver” How can one run multiple versions of PHP 5.x on a development LAMP server? How do I convert a PDF document to a preview image in PHP?

Examples related to forgot-password

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) I forgot the password I entered during postgres installation

Examples related to mysql-error-2002

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) How to Solve the XAMPP 1.7.7 - PHPMyAdmin - MySQL Error #2002 in Ubuntu Connecting to remote MySQL server using PHP