Lambivist
Lambivist

Reputation: 4789

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]

Upvotes: 471

Views: 1586081

Answers (30)

Shardul Birje
Shardul Birje

Reputation: 420

I was getting this error since the latest macOS update(quite related to this issue). I followed almost every answer on Stackoverflow but nothing worked.

Finally today what helped me was removing MYSQL completely from the system and then I followed the steps provided in the terminal which told me to first install mysql@8.4 then start and stop the service (for 8.4) once and finally when I started mysql service using /opt/homebrew/opt/mysql/bin/mysqld_safe --datadir\=/opt/homebrew/var/mysql things are back to normal

on further research I found this GitHub discussion and things are sorted now

Upvotes: 0

Abhay Shiro
Abhay Shiro

Reputation: 3627

I am using MacOS on Apple Silicon chip due to which I have two brew installed. The one that is chip compatible is installed as brew86

I faced this same problem and I was not able to start local mysql instance using brew86 services start mysql

What solved my problem was I ran brew86 doctor and I found that there was permission issues. I had to run some chown command as shown in the results of brew86 doctor

Hope this helps.

Upvotes: 0

Firzen
Firzen

Reputation: 2102

If you have tried everything and nothing works, you are at the right place. Just run:

brew doctor

Fix all issues it found, and then run:

brew remove mysql
brew install mysql
mysql.server start

Upvotes: 0

Ramil Mammadov
Ramil Mammadov

Reputation: 532

Try with -h (host) and -P(port):

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

Upvotes: 13

AnupRaj
AnupRaj

Reputation: 671

I got the following error:

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

Tried several ways and finally solved it through the following way:

sudo gksu gedit /etc/mysql/my.cnf

Modified:

#bind-address       = 127.0.0.1

to:

bind-address        = localhost

and restarted:

sudo /etc/init.d/mysql restart

It worked.

Upvotes: 56

Waqas
Waqas

Reputation: 3853

Are you sure you installed MySQL as well as MySQL server?

For example to install MySQL server I'll use yum or apt to install both MySQL command line tool and the server:

yum -y install mysql mysql-server (or apt-get install mysql mysql-server)

Enable the MySQL service:

/sbin/chkconfig mysqld on

Start the MySQL server:

/sbin/service mysqld start

Afterwards set the MySQL root password:

mysqladmin -u root password 'new-password' (with the quotes)

Upvotes: 101

marimaf
marimaf

Reputation: 5410

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.

Upvotes: 242

Abhijeet Patil
Abhijeet Patil

Reputation: 418

In case if you are reinstalling mariadb you must check of old process of mysql is still running find the process and kill

ps ax | grep mysql

sudo kill -9 mysql

Since InnoDB restarts itself you can use kill -15 in case of fresh installation* remove directory

sudo rm -rf /var/lib/mysql

create a default database structure and guide mariadb to read your data directory

sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

Then reconfigure

sudo mysql_secure_installation

Upvotes: 0

leompeters
leompeters

Reputation: 1035

When you use localhost to connect to MySQL, the operating system uses the socket connector. However, if you use the 127.0.0.1 IP address, the operating system will use the TCP/IP connector. So, a possible solution when you’re having issues with the socket connector is trying to establish the connection using TCP/IP by specifying the 127.0.0.1 IP address instead of localhost.

Upvotes: 3

Cosmin
Cosmin

Reputation: 166

There are a lot of potential problems that will lead to this error. The best thing, as suggested in another answer, is to check the log file. This can be found in different locations, depending on the platform. For me, this was located at /var/log/mysqld.err and the error was "No space left on device". After doing some cleanup, I could start the service and the problem was fixed.

Upvotes: 0

lucian
lucian

Reputation: 681

You might want to chek if the hard disk is full (df on the console), that's what ultimately triggered this error for me.

Upvotes: 5

Pietro La Grotta
Pietro La Grotta

Reputation: 327

I solved by deleting tc.log into /var/lib/mysql directory because it was corrupdet after full disk. Than, when i started mysql, the log file was created as new.

Upvotes: 1

Doulat Khan
Doulat Khan

Reputation: 533

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, -P means port and -p means password.

Upvotes: 26

user9869932
user9869932

Reputation: 7377

After two days with this stuff, here's my two cents on this,

are you using hhvm?

if so, append the next line in /etc/hhvm/server.ini

hhvm.mysql.socket = /var/run/mysqld/mysqld.sock

Upvotes: 1

Zorayr
Zorayr

Reputation: 24962

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

Upvotes: 38

Coder
Coder

Reputation: 3103

In my case the solution was that I had mariadb installed, which was aliased to mysql. So all service commands relating to mysql were not recognised... I just needed:

service mariadb start

And for good measure:

chkconfig mariadb on

Upvotes: -2

Alfredo cubitos
Alfredo cubitos

Reputation: 171

I had the same issue on Freebsd, I solved it by deleting the my.cnf file. Mysql version was 5.6.

It seems that after upgrading from 5.5 the old my.cnf remains and some incompatible parameters forces to an error. The old my.cnf had a lot of parameters but the new one only one

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

But if you do so, keep in mind that could be a security risk, because this without any further entry may open your db to the rest of the world ;-) If you won't that add at least this line

bind-address = 127.0.1.1

Upvotes: 0

narasimharaosp
narasimharaosp

Reputation: 533

If anyone looking for Intialization error

Can't connect to database "/var/run/mysql/mysql.sock"

When running civicrm & drupal just make below changes in

settings.php and civicrm.settings.php

only in places where you try to establish db connection

localhost to 127.0.0.1 #local installation

Upvotes: 2

Antony
Antony

Reputation: 4364

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.

Upvotes: 4

jsina
jsina

Reputation: 4849

I've just had this problem. after a day of checking finally I've got the answer with that 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

Upvotes: 2

Tadiwanashe
Tadiwanashe

Reputation: 1304

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.

Upvotes: 6

Mike
Mike

Reputation: 24413

I had this problem too when trying to start the server, so many of the answers here that just say to start the server didn't work. The first thing you can do is execute the following to see if there are any config errors:

/usr/sbin/mysqld --verbose --help 1>/dev/null

I did have one error that showed up:

160816 19:24:33 [Note] /usr/sbin/mysqld (mysqld 5.5.50-0ubuntu0.14.04.1-log) starting as process 9461 ...
160816 19:24:33 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
160816 19:24:33 [Note] Plugin 'FEDERATED' is disabled.
160816 19:24:33 [ERROR] /usr/sbin/mysqld: unknown variable 'innodb-online-alter-log-max-size=4294967296'
160816 19:24:33 [ERROR] Aborting

A simple grep -HR "innodb-online-alter-log-max-size" /etc/mysql/ showed me exactly what file contained the offending line, so I removed that line from the file.

Then, checking my /var/log/mysql/error.log file I had:

InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes
InnoDB: than specified in the .cnf file 0 671088640 bytes!
160816 22:46:46 [ERROR] Plugin 'InnoDB' init function returned error.
160816 22:46:46 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
160816 22:46:46 [ERROR] Unknown/unsupported storage engine: InnoDB
160816 22:46:46 [ERROR] Aborting

Based on this question the accepted solution wouldn't work because I couldn't even get the server started, so I followed what some of the comments said and deleted my /var/lib/mysql/ib_logfile0 and /var/lib/mysql/ib_logfile1 files.

This allowed the server to start and I was able to connect and execute queries, however checking my error log file it was quickly getting filled up with several tens of thousands of lines like this:

160816 22:52:15  InnoDB: Error: page 1415 log sequence number 82039318708
InnoDB: is in the future! Current system log sequence number 81640793100.
InnoDB: Your database may be corrupt or you may have copied the InnoDB
InnoDB: tablespace but not the InnoDB log files. See
InnoDB: http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html
InnoDB: for more information.

Based on a suggestion from here, to fix this I did a mysqldump and restore of all databases (see the link for several other solutions).

$ mysqldump -u root -p --allow-keywords --add-drop-database --comments --hex-blob --opt --quote-names --databases db_1 db_2 db_3 db_etc > backup-all-databases.sql
$ mysql -u root -p < backup-all-databases.sql

Everything appears to be working as expected now.

Upvotes: 3

Vlad Schnakovszki
Vlad Schnakovszki

Reputation: 8601

This doesn't directly answer your question but a subset of it, namely using PythonAnywhere. I kept stumbling upon this question when looking for a fix so I'm adding it here in the hope that it will help others in my situation.


PythonAnywhere decided to change the database connection hostnames in order to improve efficiency and reliability, as detailed here:

The official host name you should use for connecting to your account's MySQL database instance has changed from mysql.server to yourusername.mysql.pythonanywhere-services.com. This bypasses a part of our infrastructure that has started showing problems in recent weeks, and it should be much more efficient and reliable than the old way.

Hence, you will need to update your hostname to the value highlighted above.

Upvotes: 2

Hamdi Charef
Hamdi Charef

Reputation: 649

Edit your config file /etc/mysql/my.cnf:

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

Upvotes: 0

ikuchris
ikuchris

Reputation: 1172

sudo service mysqld start

Worked for me, I'm using Centos

Upvotes: 3

matt2000
matt2000

Reputation: 1073

As can be seen by the many answers here, there are lots of problems that can result in this error message when you start the MySQL service. The thing is, MySQL will generally tell you exactly what's wrong, if you just look in the appropriate log file.

For example, on Ubuntu, you should check /var/log/syslog. Since lots of other things might also be logging to this file, you probably want to use grep to look at mysql messages, and tail to look at only the most recent. All together, that might look like:

grep mysql /var/log/syslog | tail -50

Don't blindly make changes to your configuration because someone else said 'This worked for my system.' Figure out what is actually wrong with your system and you'll get a better result much faster.

Upvotes: 15

Ivan Aracki
Ivan Aracki

Reputation: 5391

Try running following command:

sudo /etc/init.d/mysql start

Upvotes: 0

Sergei Khaletskiy
Sergei Khaletskiy

Reputation: 73

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

Upvotes: 2

SharpC
SharpC

Reputation: 7474

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* 

Upvotes: 4

Alex
Alex

Reputation: 4669

I've deleted mysql.sock file and it worked. Putting the rights on it didn't work at all. Neither restarting, or whatever ...

Upvotes: 0

Related Questions