Can't connect to local MySQL server through socket homebrew

后端 未结 21 1894
萌比男神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:23

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

    For me, I had installed mariadb long time ago, then installed mysql@5.7.

    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

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

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

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

    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.

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

    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

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

    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 mysql@5.6
    

    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 mysql@5.6
    

    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.

    0 讨论(0)
提交回复
热议问题