Can't connect to local MySQL server through socket homebrew

后端 未结 21 1893
萌比男神i
萌比男神i 2020-12-04 05:54

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

ERROR 2002 (HY00

相关标签:
21条回答
  • 2020-12-04 06:19

    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.

    0 讨论(0)
  • 2020-12-04 06:19

    This works for me:

    brew upgrade mysql
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 06:21

    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
    
    0 讨论(0)
  • 2020-12-04 06:21

    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.

    0 讨论(0)
  • 2020-12-04 06:22

    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>
    
    0 讨论(0)
提交回复
热议问题