206

When I attempted to connect to a local MySQL server during my test suite, it fails with the error:

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

However, I'm able to at all times, connect to MySQL by running the command line mysql program. A ps aux | grep mysql shows the server is running, and stat /tmp/mysql.sock confirm that the socket exists. Further, if I open a debugger in except clause of that exception, I'm able to reliably connect with the exact same parameters.

This issue reproduces fairly reliably, however it doesn't appear to be 100%, because every once in a blue moon, my test suite does in fact run without hitting this error. When I attempted to run with sudo dtruss it did not reproduce.

All the client code is in Python, though I can't figure how that'd be relevant.

Switching to use host 127.0.0.1 produces the error:

DatabaseError: Can't connect to MySQL server on '127.0.0.1' (61)
17
  • 1
    Is it possible that you're somehow hitting the database with many concurrent connections? Maybe try increasing max_connections in your MySQL conf file?
    – dgel
    Commented May 1, 2013 at 20:36
  • 2
    does mysql -h 127.0.0.1 work from the commandline? I'm not so sure your mysql server is actually listening on a TCP port.
    – Eli
    Commented May 1, 2013 at 21:08
  • 1
    Are you sure you have the right versions of the Python MySQL client libraries for your version of MySQL? Also, does mysql -h localhost work reliably?
    – Old Pro
    Commented May 7, 2013 at 6:25
  • 2
    Does MySQL log anything to the error log? Also, check file permissions on /tmp/mysql.sock and your mysql data directory. Do the errors also occur if you run the test suite as root (sudo)? Commented May 7, 2013 at 22:53
  • 2
    A lot of these suggestions are covered by the official MySQL reference manual which I reference in my response below. It's a better use of time to go through the MySQL reference manual suggestions systematically, rather than trying just one or two of those suggestions.
    – jtoberon
    Commented May 8, 2013 at 16:41

37 Answers 37

1
2
0

Using MacOS Mojave 10.14.6 for MySQL 8.0.19 installed via Homebrew

  • Ran sudo find / -name my.cnf
  • File found at /usr/local/etc/my.cnf

Worked for a time then eventually the error returned. Uninstalled the Homebrew version of MySQL and installed the .dmg file directly from here

Happily connecting since then.

0

In my case what helped was to edit the file /etc/mysql/mysql.conf.d/mysqld.cnfand replace the line:

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

with

socket      = /tmp/mysql.sock

Then I restarted the server and it worked fine. The funny thing is that if I put back the line as it was before and restarted it still worked..

0

Following steps would help:

  1. mysql.server start
  2. List item

for more details go to ref on medium

0

I'm using macOS Monterey, I fixed it by changing the file content which locate in /opt/homebrew/etc/my.cnf from "bind-address = 127.0.0.1" to "bind-address = localhost"

0

You can try $ mysql.server start then $ mysql -h localhost -u root -p in password just press enter and it will start, then u can change password

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 9, 2022 at 10:39
0

I installed MySQL 8.0 on macOS Monterey 12.5.1 using homebrew brew install mysql

When I ran mysql -uroot it gave me that error.

I fixed it like this:

sudo /usr/local/opt/mysql/support-files/mysql.server start

It showed some error pointing to a given error file so I just deleted the error file:

rm -rf /usr/local/var/mysql/*.err

and then reran the start command

sudo /usr/local/opt/mysql/support-files/mysql.server start

And then mysql -uroot worked again

-2

Mac user here running mac os mojave 10.14. In my case what helped me was to uninstall MySQL from within the system preferences pane and then reinstalling and selecting the legacy password MySQL system during the installation wizard.

I did this by going to apple menu > system preferences >> MySQL >> and then hitting uninstall.

Once I successfully uninstalled I went back to MySQL's website and into the archive downloads and downloaded mysql Ver 8.0.18 for macos10.14 on x86_64 (MySQL Community Server - GPL).

Now, when going through the install wizard you will come to a point where the installer asks you to choose the password type that you'd like to use and here you MUST SELECT THE LEGACY SYSTEM that allows you to sign into MySQL using the root user and a password that you will set at that moment.

After I finished going through the install wizard I restarted my terminal (in my case I was using vs code's terminal) and then ran "mysql -u root -p" and was able to enter the password I created during installation and got into MySQL with no errors.

1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.