Can't connect to local MySQL server through socket homebrew
Asked Answered
V

27

144

I recently tried installing MySQL with homebrew (brew install mysql) and when I try to run it I get the following error:

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

There is no /tmp/mysql.sock nor a /var/lib/mysql.sock.

I've searched and haven't found any mysql.sock file.

How can I fix this?

Vent answered 22/2, 2013 at 3:34 Comment(4)
You can see this link GeekHades answer. #4847569Barns
I brew installed mysql and had the same issue after my laptop battery died and forced an incomplete shutdown. It's worth running mysqld to check things and ensure MySQL shut down properly last time. If it had a 'dirty' shutdown (e.g. if a laptop battery forces a system shutdown) this should clean it up. Then you can start MySQL server again: mysql.server start.Abject
This answer worked for me: https://mcmap.net/q/86160/-brew-install-mysql-on-macosKissie
This occurred for me after restoring a new Mac Mini from a Time Machine backup. I had to uninstall [email protected] and reinstall it for it to start working. Bit a hammer but was relatively painless since all my configurations were retained.Anergy
G
117

When you got the server running via

mysql.server start

you should see the socket in /tmp/mysql.sock. However, the system seems to expect it in /var/mysql/mysql.sock. To fix this, you have to create a symlink in /var/mysql:

sudo mkdir /var/mysql

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

This solved it for me. Now my phpMyAdmin works happily with localhost and 127.0.0.1.

Credit goes to Henry

Guessrope answered 6/8, 2013 at 20:47 Comment(11)
When I try mysql.server start I get ERROR! The server quit without updating PID file (/usr/local/var/mysql/lyahdav-C02R32HCG8WM.pid). I needed an older version of MySQL, installed via brew install [email protected].Aftersensation
running. mysql.server start fixed it for meStenger
@EB it's been a while since I had this issue but looking back at my comment, you tried the workaround to install an older version of MySQL? Other than that I'm out of ideas unfortunately.Aftersensation
The problem gets resolved when you re-install MySQL but then you have the so long backup-restore routine. Then, it also breaks again because my computer closes itself unexpectedly. I don't if this is an out of memory issue but everytime my computer closes, mysql gets broken with 2002Embargo
@LironYahdav What made you come to the conclusion you needed an older version of MySQL?Stairwell
@Stairwell I unfortunately don't remember. Hopefully someone else could chime in.Aftersensation
I did all that and got: ``` .. ERROR! The server quit without updating PID file (/var/lib/mysql/Saranshs-MacBook-Pro-2.local.pid). ```Dulaney
Do any of these answers apply to a homebrew-installed mysql?Bourdon
@DavidRhoden Sure, that's what the original question was about.Guessrope
@AAGD: I ask because in my current setup mysql.server start starts the pre-installed mysql instead of the homebrew-installed one. Or at least I think it does.Bourdon
@DavidRhoden Maybe you missed a step during installation? This would be off-topic here, maybe go through flaviocopes.com/mysql-how-to-install or some other tut to find a fix...Guessrope
R
105

Warning, this will wipe your databases, take a backup if you wish to keep them

I had some directories left from another mysql(8.0) installation, that were not removed.

I solved this by doing the following:

First uninstall mysql

brew uninstall [email protected]

Delete the folders/files that were not removed

rm -rf /usr/local/var/mysql
rm /usr/local/etc/my.cnf

Reinstall mysql and link it

brew install [email protected]
brew link --force [email protected]

Enable and start the service

brew services start [email protected]
Reckford answered 21/3, 2019 at 19:15 Comment(5)
Seems like the issue for me was that I still had /usr/local/var/mysql after a previous installation. Removing did the trick! Thanks for your suggestion.Bodycheck
I needed to run the "mysql_secure_installation" step mentioned by homebrew before this error disappeared.Saguache
This finally worked for me (after trying 5 other answers across various questions), though I knew beforehand that removing the mysql folder would obliterate my local databases, so I used mv /usr/local/var/mysql /usr/local/var/mysql_prev and mv /usr/local/etc/my.cnf /usr/local/etc/my.cnf_prev instead just in case. I then can compare the differences using the diff command if I want to. I'm using macOS Big Sur.Plenum
Warning: This will remove all of your databases, make a backup first and run mysql_secure_installation after all steps.Yellowlegs
On Mac Os Monterey M1 my.cnf is in /opt/homebrew/etcChromosome
S
66

Looks like your mysql server is not started. I usually run the stop command and then start it again:

mysqld stop
mysql.server start

Same error, and this works for me.

Shere answered 24/4, 2013 at 22:13 Comment(3)
. ERROR! The server quit without updating PID file (/usr/local/var/mysql/myHostName.pid). ?Fullfaced
I did a mysqld & per a comment above, but I think it probably would do the same thing as mysqld stop I'm pretty sure in my case it was because of an improper shutdown.Elater
I can confrim, in my case just 'mysqld' command was enough to restart the service, and socket file was recreated when service is started. From console output after service starts its obvious that file was created again: 'socket: '/tmp/mysql.sock' port: 3306 Homebrew.'Cybil
S
31

Try to connect using "127.0.0.1" instead "localhost".

Sillabub answered 22/2, 2013 at 3:43 Comment(2)
@Esteban MySQL will try to connect to the unix socket if you tell it to connect to "localhost". If you tell it to connect to 127.0.0.1 you are forcing it to connect to the network socket. So probably you have MySQL configured to only listen to the network socket and not to the file system socket. (Source: serverfault.com/a/295300/59101)Spar
This helped me to force the mysql client to use the TCP socket, as I'm running an SSH tunnel on localhost to my remote MySQL DB.Kenney
H
24
  1. If you are able to see "mysql stopped" when you run below command;

    brew services list
    
  2. and if you are able to start mysql with below command;

    mysql server start
    

this means; mysql is able to start manually, but it doesn't start automatically when the operating system is started. Adding mysql to services will fix this problem. To do so, you can run below command;

brew services start mysql

After that, you may restart your operating system and try connecting to mysql to see if it started automatically. I did the same and stop receiving below error;

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

I hope this helps.

Harrow answered 10/1, 2016 at 19:23 Comment(4)
@berk it helped in my case to get reid of the error but prompt another error 'ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: NO)'Juanajuanita
Hi @PratikKhadka, the error you are getting now, means mysql is running, and you have authentication problem. According to the error message you posted, you are using username as 'user' and using no password. You should use a valid username and password. I suggest you to check the following link; "forums.mysql.com/read.php?34,140320,140324"Harrow
No need to restart OS, just running "brew services start mysql" worked for me . Thanks!Leotaleotard
Hi @AKS, you are right. Restart is not required to make it work. I edited the answer to explain why restart is required. It is required to see if mysql starts automatically when OS is started. Thank you for the feedback!Harrow
N
9

Since I spent quite some time trying to solve this and always came back to this page when looking for this error, I'll leave my solution here hoping that somebody saves the time I've lost. Although in my case I am using mariadb rather than MySql, you might still be able to adapt this solution to your needs.

My problem

is the same, but my setup is a bit different (mariadb instead of mysql):

Installed mariadb with homebrew

$ brew install mariadb

Started the daemon

$ brew services start mariadb

Tried to connect and got the above mentioned error

$ mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

My solution

find out which my.cnf files are used by mysql (as suggested in this comment):

$ mysql --verbose --help | grep my.cnf
/usr/local/etc/my.cnf ~/.my.cnf
                        order of preference, my.cnf, $MYSQL_TCP_PORT,

check where the Unix socket file is running (almost as described here):

$ netstat -ln | grep mariadb
.... /usr/local/mariadb/data/mariadb.sock

(you might want to grep mysql instead of mariadb)

Add the socket file you found to ~/.my.cnf (create the file if necessary)(assuming ~/.my.cnf was listed when running the mysql --verbose ...-command from above):

[client]
socket = /usr/local/mariadb/data/mariadb.sock

Restart your mariadb:

$ brew services restart mariadb

After this I could run mysql and got:

$ mysql -uroot
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

So I run the command with superuser privileges instead and after entering my password I got:

$ sudo mysql -uroot
MariaDB [(none)]>

Notes:

  1. I'm not quite sure about the groups where you have to add the socket, first I had it [client-server] but then I figured [client] should be enough. So I changed it and it still works.

  2. When running mariadb_config | grep socket I get: --socket [/tmp/mysql.sock] which is a bit confusing since it seems that /usr/local/mariadb/data/mariadb.sock is the actual place (at least on my machine)

  3. I wonder where I can configure the /usr/local/mariadb/data/mariadb.sock to actually be /tmp/mysql.sockso I can use the default settings instead of having to edit my .my.cnf (but I'm too tired now to figure that out...)

  4. At some point I also did things mentioned in other answers before coming up with this.

Noncooperation answered 23/5, 2018 at 0:53 Comment(0)
W
8

The file /tmp/mysql.sock is probably a Named-Pipe, since it's in a temporary folder. A named pipe is a Special-File that never gets permanently stored.

If we make two programs, and we want one program to send a message to another program, we could create a text file. We have one program write something in the text file and the other program read what our other program wrote. That's what a pipe is, except it doesn't write the file to our computer hard disk, IE doesn't permanently store the file (like we do when we create a file and save it.)

A Socket is the exact same as a Pipe. The difference is that Sockets are usually used over a network -- between computers. A Socket sends information to another computer, or receives information from another computer. Both Pipes and Sockets use a temporary file to share so that they can 'communicate'.

It's difficult to discern which one MySql is using in this case. Doesn't matter though.

The command mysql.server start should get the 'server' (program) running its infinite loop that will create that special-file and wait for changes (listen for writes).

After that, a common issue might be that the MySql program doesn't have permission to create a file on your machine, so you might have to give it root privileges

sudo mysql.server start
Walkabout answered 27/10, 2018 at 22:10 Comment(1)
This worked great, thanks. I just ran mysql.server start, mysql.server stop, and then started up the service via homebrew again with brew services start [email protected] and things ran smoothly.Caddaric
M
5

After installing macos mojave, had to wipe mysql folder under /usr/local/var/mysql and then reinstall via brew install mysql otherwise permission related things would come up all over the place.

Mig answered 2/10, 2018 at 19:7 Comment(3)
I solved my issue with your answer, I had installed previously mysql and then tryied to downgrade to [email protected]Playwriting
Thanks dude. it solved my problem too. removing /usr/local/var/mysql and reinstalling was solution for me.Monck
Beware: removing /usr/local/var/mysql you will also remove all your existing databases!Pitchstone
C
5

When running mysql_secure_installation and entering the new password I got:


Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)


I noticed when trying the following from this answer:

netstat -ln | grep mysql

It didn't return anything, and I took that to mean that there wasn't a .sock file.

So, I added the following to my my.cnf file (either in /etc/my.cnf or in my case, /usr/local/etc/my.cnf).

Under:

[mysqld]
socket=/tmp/mysql.sock

Under:

[client]
socket=/tmp/mysql.sock

This was based on this post.

Then stop/start mysql again and retried mysql_secure_installation which finally let me enter my new root password and continue with other setup preferences.

Camilacamile answered 25/8, 2019 at 8:42 Comment(0)
G
4

In my case, the culprit was found in the logfiles:

$ tail /usr/local/var/mysql/<hostname>.lan.err
2019-09-19  7:32:21 0 [ERROR] InnoDB: redo log file './ib_logfile0' exists. Creating system tablespace with existing redo log files is not recommended. Please delete all redo log files before creating new system tablespace.
2019-09-19  7:32:21 0 [ERROR] InnoDB: Database creation was aborted with error Generic error. You may need to delete the ibdata1 file before trying to start up again.

So I renamed ib_logfile0 to get rid of the error (I had to do the same with ib_logfile1 afterwards).

mv /usr/local/var/mysql/ib_logfile0 /usr/local/var/mysql/ib_logfile0_bak
mv /usr/local/var/mysql/ib_logfile1 /usr/local/var/mysql/ib_logfile1_bak
brew services restart mariadb
Gauleiter answered 19/9, 2019 at 13:35 Comment(0)
P
4

If "mysqld" IS running, it's possible your data is corrupted. Try running this:

mysqld

Read through the wall of data, and check if mysqld is reporting that the database is corrupted. Corruption can present in many unintuitive ways:

  • mysql -uroot returns "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)".
  • mysql.server start returns "ERROR! The server quit without updating PID".
  • Sequel Pro and MySQL Workbench responds that they can't connect to MySQL on localhost or 127.0.0.1.

To recover your data, open my.cnf and add the following line in the [mysqld] section:

innodb_force_recovery=1

Restart mysqld:

$ brew services restart [email protected]

Now you can connect to it, but it’s in limited read-only mode.

If you're using InnoDB, run this to export all your data:

$ mysqldump -u root -p --all-databases --add-drop-database --add-drop-table > data-recovery.sql

The file is created in your ~ dir. It may take some time.

Once finished, remove innodb_force_recovery=1 from my.cnf, then restart mysql in normal mode:

$ brew services restart [email protected]

Drop all the databases. I did this using Sequel Pro. This deletes all your original data. Make sure your data-recovery.sql looks good before doing this. Also consider backing up /usr/local/var/mysql to be extra careful.

Then restore the databases, tables, and data with this:

$ mysql -uroot < ~/data-recovery.sql

This can be a long import/restoration process. Once complete, you’re good to go!

Thanks go to https://severalnines.com/database-blog/my-mysql-database-corrupted-what-do-i-do-now for the recovery instructions. The link has further instructions on MyISAM recovery.

Projector answered 2/12, 2020 at 4:58 Comment(0)
A
3

I got the same error and this is what helped me:

$ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
$launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
$mysql -uroot
mysql>
Assassin answered 8/10, 2013 at 9:18 Comment(0)
M
3

I faced the same problem on my mac and solved it, by following the following tutorials

https://mariadb.com/resources/blog/installing-mariadb-10116-mac-os-x-homebrew

But don't forget to kill or uninstall the old version before continuing.

Commands:

brew uninstall mariadb

xcode-select --install

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" - See more at: https://mariadb.com/resources/blog/installing-mariadb-10116-mac-os-x-homebrew#sthash.XQoxRoJp.dpuf

brew doctor

brew update

brew info mariadb

brew install mariadb

mysql_install_db

mysql.server start
Metaphor answered 27/12, 2016 at 11:38 Comment(1)
Please explain with some brief examples how the link you provided, helped you out. Links can disappear, and therefore this can be a reason that your answer might be deleted.Sear
C
3

Just to add to these answers, In my case I had no local mySQL server, it was running inside a docker container. So the socket file does not exist and will not be accessible for the "mysql" client.

The sock file gets created by mysqld and mysql uses this to communicate with it. However if your mySql server is not running local, it does not require the sock file.

By specifying a host name/ip the sock file is not required e.g.

mysql --host=127.0.0.1 --port=3306 --user=xyz --password=xyz
Cachexia answered 4/9, 2017 at 11:21 Comment(1)
my issue was I tried to use localhost or http://localhostColdiron
B
2

You'll need to run mysql_install_db - easiest way is if you're in the install directory:

$ cd /usr/local/Cellar/mysql/<version>/ 
$ mysql_install_db

Alternatively, you can feed mysql_install_db a basedir parameter like the following:

$ mysql_install_db --basedir="$(brew --prefix mysql)"
Brabant answered 1/3, 2016 at 19:51 Comment(0)
C
2

After a restart I could not connect with the local mariadb, a search also brought me to this page and I wanted to share my solution with you.

I noticed that the directory my.cnf.d in /usr/local/etc/ is missing.

This is a known bug with homebrew that is described and solved there. https://github.com/Homebrew/homebrew-core/issues/36801

fast way to fix: mkdir /usr/local/etc/my.cnf.d

Calcification answered 5/8, 2019 at 10:51 Comment(1)
This worked for me. Could not connect to local mariadb after brew update and restart. brew services list result was showing mariadb status started in yellow. Thanks for the tip.Hogfish
P
1

just to complete this thread. therefore MAMP (PRO) is used pretty often

the path here is

/Applications/MAMP/tmp/mysql/mysql.sock
Pianist answered 19/10, 2017 at 7:32 Comment(0)
I
1

This works for me:

brew upgrade mysql
Impression answered 6/7, 2020 at 23:14 Comment(0)
S
1

If brew does not complete "postinstall" I only have to use:

sudo chown -R $(whoami) /usr/local/*

then completed postinstall by:

brew postinstall [email protected]

next step just start [email protected] service.

It worked for me on MacOS Monterey.

Sisyphus answered 12/1, 2022 at 2:8 Comment(0)
H
1

I also ran into this... it seemingly resulted from some leftover artifacts of multiple mysql installs on my dev machine. Every time I attempted to start or restart the mysql service it would crash. Ultimately, working through the err file helped me solve my issue.

Setup:

At various points during troubleshooting I tried uninstalling everything mysql in my Homebrew list, deleting /opt/homebrew/var/mysql, and reinstalling [email protected] to no avail. I also tried restarting my machine.

I was able to validate what was happening when the service failed to start by reviewing the err (e.g., MyComputerName.local.err) log that it was spitting out into /opt/homebrew/var/mysql/ when I tried to start/restart the service via Homebrew. In the beginning, I was seeing things like this that pointed to issues with the config:

mysqld: Table 'mysql.plugin' doesn't exist
TIMESTAMPZ 0 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
TIMESTAMPZ 0 [ERROR] unknown variable 'mysqlx-bind-address=127.0.0.1'
TIMESTAMPZ 0 [ERROR] Aborting

I deleted my.cnf and my.cnf.default in /opt/homebrew/etc and attempted to start the service again. Then, the failure changed to this:

TIMESTAMPZ 0 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist
TIMESTAMPZ 0 [ERROR] Fatal error: Failed to initialize ACL/grant/time zones structures or failed to remove temporary table files.
TIMESTAMPZ 0 [ERROR] Aborting

At this point I noticed several mysql lock files in /opt/homebrew/var/homebrew/locks/ (mysql.formula.lock, [email protected], [email protected]); I deleted those files, as well as anything mysql in /opt/homebrew/var/homebrew/linked.

After that, I was able to start mysql without any issues.

Hymie answered 4/5, 2022 at 18:25 Comment(0)
S
0

I manually started mysql in the system preferences pane by initialising the database and then starting it. This solved my problem.

Shown answered 18/4, 2019 at 10:57 Comment(0)
S
0

I had same problem. After trying all these methods without success I did the following:

tail -f the-mysql-or-maria-db-error-file.err

in another console:

brew services restart mariadb

I saw the following error:

"MAC HOMEBREW Crash recovery failed. Either correct the problem (if it's, for example, out of memory error) and restart, or delete tc log and start mysqld with"

So I changed the tc.log extesion to tc.log.txt and restart mariadb

brew services restart mariadb

and done!

Subplot answered 29/11, 2019 at 11:33 Comment(0)
D
0

[LINUX]

Though answer is expected to be for MacOS only but in Linux we may face the same Error.

I was facing the same issue in Linux. I ran this command:

sudo /etc/init.d/mysql.server start

and I was able to run the MySQL server

Ref. https://gist.github.com/vinodpandey/1a4b5b8228f9a000ca236820185fc3bc

Dysfunction answered 12/4, 2021 at 9:58 Comment(0)
E
0

I just ran into this problem, I did some tricks but for me it didn't work. If you used macOS, here is what I did. We start by uninstalling mysql and then reinstalling it.

brew uninstall mysql
brew install mysql

Note: you must have homebrew installed

Epicarp answered 21/11, 2022 at 10:22 Comment(0)
S
0

This happened to me today 2023-02-23 after a homebrew update.

Homebrew had created a new my.cnf.default file, I backed up my old one and replaced it with this new default and restarted Mariadb and everything worked including the old root password.

Stacystadholder answered 23/2, 2023 at 17:46 Comment(0)
P
0

If you install mysql via brew in Apple Silicon( M1/M2/M3/M4...) maybe you can try this steps

brew install [email protected]
brew postinstall [email protected] 
brew services start [email protected]
mysql -uroot -p

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.35 Homebrew

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.

if it still can't work

maybe check the log there

/opt/homebrew/var/mysql/

like /opt/homebrew/var/mysql/{yourName}.local.err


To delete and reinitialize the MySQL database (which will lead to the loss of all unbacked data), you can consider the following:

  1. Stop the MySQL service (if it is running)

brew services stop [email protected]

  1. Back up the current MySQL data directory (in case you need to roll back)

cp -R /opt/homebrew/var/mysql /path/to/backup_mysql_data/

  1. Delete InnoDB-related system files

    rm -f /opt/homebrew/var/mysql/ibdata1

    rm -f /opt/homebrew/var/mysql/ib_logfile0

    rm -f /opt/homebrew/var/mysql/ib_logfile1

    find /opt/homebrew/var/mysql/ -name "*.ibd" -exec rm -f {} \;

  2. After cleaning, re-initialize the MySQL data directory.

    brew postinstall [email protected]

  3. Start the MySQL service

    brew services start [email protected]

Palmate answered 18/12, 2023 at 15:29 Comment(0)
P
-1

For me, I had installed mariadb long time ago, then installed [email protected].

When I executed mysql -uroot, I get the error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Reading the answers:

  • I uninstalled mariadb
  • Deleted the folder /usr/local/var/mysql
  • Ran the command mysqld --initialize

Then I was able to mysql -uroot -p

Politian answered 23/7, 2020 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.