Tuesday 6 November 2018

Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)

I am having a big problem trying to connect to mysql. When I run:
/usr/local/mysql/bin/mysql start
I have the following error :
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
I do have mysql.sock under the /var/mysql directory.
In /etc/my.cnf I have:
[client]
port=3306
socket=/var/mysql/mysql.sock

[mysqld]
port=3306
socket=/var/mysql/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M
and in /etc/php.ini I have :
; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket = /var/mysql/mysql.sock
I have restarted apache using sudo /opt/local/apache2/bin/apachectl restart
But I still have the error.
Otherwise, I don't know if that's relevant but when I do mysql_config --sockets I get
--socket         [/tmp/mysql.sock]

 Answers


If your file my.cnf (usually in the /etc/mysql/ folder) is correctly configured with
socket=/var/lib/mysql/mysql.sock
you can check if mysql is running with the following command:
mysqladmin -u root -p status
try changing your permission to mysql folder. If you are working locally, you can try:
sudo chmod -R 755 /var/lib/mysql/
that solved it for me



A quick workaround that worked for me: try using the local ip address (127.0.0.1) instead of 'localhost' in mysql_connect(). This "forces" php to connect through TCP/IP instead of a unix socket.



Make sure you are running mysqld : /etc/init.d/mysqld start



Another workaround is to edit /etc/my.cnf and include host in the section [client]
 [client]
 #password       = your_password
 host            = 127.0.0.1
 port            = 3306
 socket          = /var/run/mysql/mysql.sock
And then restarting the mysql service.
This workaround was tested in: Server version: 5.5.25a-log Source distribution



In my case, I was using Centos 5.5. I found that the problem was because the mysql service was stopped some how. So I started mysql service with the command:
 /etc/init.d/mysqld start
So.. silly mistake.



If everything worked just fine and you just started seeing this error, before you do anything else, make sure you're not out of disk space:
df -h
If the volume where the mysql.sock is being created is at 100% use, MySql won't be able to create it and this will be the cause of this error. All you need to do is delete something that's not needed, like old log files.



I was getting the error because I was running MAMP and my .sock file was in a different location. I just added a symbolic link where the app thought it should be that pointed to where it actually was and it worked like a charm.



I got this error when I set cron job for my file. I changed the permissions of file to 777 but it still not worked for me. Finally I got the solution. May be it will be helpful for others.
Try with this command:
mysql -h 127.0.0.1 -P 3306 -u root -p
Remember that -h means host and -p means port.



you can always start mysql server by specifying the location of the mysql.sock file using the --socket option like
mysql --socket=/var/mysql/mysql.sock 
This will work even if the location of socket file in specified in a different location in the my.cnf file.



I also found that this was a permissions problem. I compared the MySQL files to a working install (both on Debian 6 squeeze) and had to make the following ownership changes (where mydatabase is any database(s) you have).
Ownership mysql:mysql:
chown mysql:mysql /var/lib/mysql
chown mysql:mysql /var/lib/mysql/ib*
chown mysql:mysql /var/lib/mysql/mydatabase
chown mysql:mysql /var/lib/mysql/mydatabase/*
chown mysql:mysql /var/lib/mysql/mysql/* 
Ownership mysql:root:
chown mysql:root /var/lib/mysql/mysql
chown mysql:root /var/run/mysqld 
Ownership mysql:adm:
chown mysql:adm /var/log/mysql
chown mysql:adm /var/log/mysql.err
chown mysql:adm /var/log/mysql.log* 



There are many solutions to this problem but for my situation, I just needed to correct the DATE on the machine/server (Ubuntu 16.04 Server).
i) Check the date of your server and correct it.
ii) Run sudo /etc/init.d/mysql restart
That should get it started.



This was good enough for me
sudo /etc/init.d/mysql restart



CentOS 7, 64 bit. Fresh installation.
In my case, the error was because I didn't have the right MySQL server and MySQL client installed.
Using yum, I removed mariadb and mysql-community edition. I downloaded the rpm's for the client and server from the official MySQL website and installed the server and client.
On installing the server, I was shown a message that the password to the root account for MySQL was stored in a file which I could view with sudo cat /root/.mysql_secret.
So after installing the client and server, I checked if MySQL was working (I think I rebooted before doing so) with the command sudo service mysql status and I got the result.
MySQL running (2601) [ OK ]
I logged into MySQL using the password from the .mysql_secret file:
mysql -uroot -pdxM01Xfg3DXEPabpf. Note that dxM01Xfg3DXEPabpf is the password mentioned in the .mysql_secret file.
and then typed entered the following command at the mysql prompt to change the password of root:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('somePassword');
Everything worked fine from then on.



I used 127.0.0.1 for -h instead localhost and everything was OK. In other case had what had - error that above.



For me - this was simply a case of MySQL taking a long time to load. I have over 100,000 tables in one of my databases and it did eventually start but obviously has to take a long time in this instance.

0 comments:

Post a Comment