Mac通过brew安装的MySQL各种无法运行问题总结

Author Avatar
Reborn 7月 27, 2018
  • 在其它设备中阅读本文章

一、解决 ERROR! The server quit without updating PID file (/usr/local/var/mysql/xxx.local.pid).

参考:https://stackoverflow.com/a/36156848

问题描述

之前用的好好的,经过一次更新(5.6->8.0)后某一天突然炸了。
通过brew services start mysql启动 MySQL 后,使用 Navicat 连接提示:

2003 - Can't connect to MySQL server on '127.0.0.1' (61 "Connection refused")

经过一番查询,据说是 MySQL 服务没启动的问题(其实我觉得这说法有点不对),然后我就尝试启动MySQL服务,命令mysql.server start
接着我收到以下错误:

ERROR! The server quit without updating PID file (/usr/local/var/mysql/xxx.local.pid).

xxx为我的机器名字。

然后执行mysql_secure_installation又提示:

Securing the MySQL server deployment.

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

后来经过各种尝试如:

rm /usr/local/var/mysql/*.err
rm /usr/local/var/mysql/xxx.local.err
find / -name mysql.sock
sudo chmod -R 755 /usr/local/var/mysql/
sudo chown -R _mysql:mysql /usr/local/var/mysql
ls -laF /usr/local/var/mysql/
sudo rm -f /tmp/mysql.sock.lock
ls -laF /tmp

还有通过ps aux | grep mysql找出 MySQL 的 PID,再用kill -9 [PID]杀掉进程都没用。

最终解决方法:重装大法

最后尝试完全重新安装 MySQL 终于解决了。
警告:这将删除所有数据库,因此请确保先保存转储。

brew remove mysql
brew cleanup --force
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
sudo rm -rf /usr/local/var/mysql

brew install mysql
mysqld --initialize --explicit_defaults_for_timestamp
mysql.server start # no sudo!

重装了MySQL后建议重新执行一次配置脚本:mysql_secure_installation

注意:
安装后必须执行

mysqld --initialize --explicit_defaults_for_timestamp

否则仍无法解决以下问题:

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

附录

卸载过程如下(保留以便日后需要):

$ brew remove mysql
Uninstalling /usr/local/Cellar/mysql/8.0.11... (254 files, 232.9MB)
mysql 5.7.21, 5.7.22 2 are still installed.
Remove all versions with `brew uninstall --force mysql`.
$ brew uninstall --force mysql
Uninstalling mysql... (640 files, 467.8MB)
$ brew cleanup --force
Removing: /usr/local/Cellar/dash/0.5.10... (6 files, 196.2KB)
Removing: /usr/local/Cellar/dash/0.5.9.1... (5 files, 199KB)
Removing: /usr/local/Cellar/freetds/1.00.89... (2,094 files, 11.9MB)
Removing: /usr/local/Cellar/freetds/1.00.91... (2,094 files, 11.9MB)
Removing: /usr/local/Cellar/freetype/2.9... (60 files, 2.7MB)
Removing: /usr/local/Cellar/gdbm/1.14.1_1... (20 files, 555.8KB)
Removing: /usr/local/Cellar/icu4c/61.1... (249 files, 67.2MB)
Removing: /usr/local/Cellar/libidn2/2.0.4... (46 files, 580.8KB)
Removing: /usr/local/Cellar/libpq/10.3... (2,425 files, 26.8MB)
Removing: /usr/local/Cellar/libunistring/0.9.9... (54 files, 4.4MB)
Removing: /usr/local/Cellar/nginx/1.13.12... (23 files, 1.4MB)
Removing: /usr/local/Cellar/nginx/1.15.0... (23 files, 1.4MB)
Removing: /usr/local/Cellar/node/10.0.0... (5,301 files, 51.7MB)
Removing: /usr/local/Cellar/node/10.1.0... (7,283 files, 55.4MB)
Removing: /usr/local/Cellar/node/10.2.1... (5,301 files, 51.9MB)
Removing: /usr/local/Cellar/node/9.11.1... (5,125 files, 49.7MB)
Removing: /usr/local/Cellar/openssl/1.0.2n... (1,792 files, 12.3MB)
Removing: /usr/local/Cellar/openssl/1.0.2o_1... (1,791 files, 12.3MB)
Removing: /usr/local/Cellar/php/7.2.5... (515 files, 78.9MB)
Removing: /usr/local/Cellar/php/7.2.6... (515 files, 78.9MB)
Removing: /usr/local/Cellar/[email protected]/5.6.35_1... (498 files, 63.8MB)
Removing: /usr/local/Cellar/[email protected]/5.6.36... (498 files, 63.8MB)
Removing: /usr/local/Cellar/[email protected]/7.0.29_1... (502 files, 65.5MB)
Removing: /usr/local/Cellar/[email protected]/7.0.30... (502 files, 65.5MB)
Removing: /usr/local/Cellar/python/3.6.5... (4,794 files, 99.9MB)
Removing: /usr/local/Cellar/readline/7.0.3_1... (46 files, 1.5MB)
Removing: /usr/local/Cellar/sqlite/3.23.1... (11 files, 3MB)
Removing: /usr/local/Cellar/webp/0.6.1... (38 files, 2MB)
Removing: /usr/local/Cellar/wget/1.19.4_1... (50 files, 3.7MB)
Removing: /usr/local/Cellar/xz/5.2.3... (92 files, 1.4MB)
Removing: /usr/local/Cellar/zsh/5.4.2_3... (1,390 files, 11.8MB)
Removing: /Users/seal/Library/Caches/Homebrew/dash-0.5.10.sierra.bottle.tar.gz... (83.6KB)
Removing: /Users/seal/Library/Caches/Homebrew/dash-0.5.9.1.sierra.bottle.tar.gz... (83.2KB)
Removing: /Users/seal/Library/Caches/Homebrew/freetds-1.00.89.sierra.bottle.tar.gz... (2.3MB)
Removing: /Users/seal/Library/Caches/Homebrew/freetds-1.00.91.sierra.bottle.tar.gz... (2.3MB)
Removing: /Users/seal/Library/Caches/Homebrew/freetype-2.9.sierra.bottle.1.tar.gz... (896.8KB)
Removing: /Users/seal/Library/Caches/Homebrew/gdbm-1.14.1_1.sierra.bottle.tar.gz... (182.5KB)
Removing: /Users/seal/Library/Caches/Homebrew/icu4c-61.1.sierra.bottle.tar.gz... (25.4MB)
Removing: /Users/seal/Library/Caches/Homebrew/libidn2-2.0.4.sierra.bottle.tar.gz... (190.4KB)
Removing: /Users/seal/Library/Caches/Homebrew/libpq-10.3.sierra.bottle.tar.gz... (6MB)
Removing: /Users/seal/Library/Caches/Homebrew/libunistring-0.9.9.sierra.bottle.tar.gz... (1.4MB)
Removing: /Users/seal/Library/Caches/Homebrew/mysql-5.7.21.sierra.bottle.tar.gz... (72.2MB)
Removing: /Users/seal/Library/Caches/Homebrew/mysql-5.7.22.sierra.bottle.tar.gz... (72.2MB)
Removing: /Users/seal/Library/Caches/Homebrew/nginx-1.13.12.sierra.bottle.tar.gz... (570.5KB)
Removing: /Users/seal/Library/Caches/Homebrew/nginx-1.15.0.sierra.bottle.tar.gz... (572.6KB)
Removing: /Users/seal/Library/Caches/Homebrew/node-10.0.0.sierra.bottle.tar.gz... (13.7MB)
Removing: /Users/seal/Library/Caches/Homebrew/node-10.1.0.sierra.bottle.tar.gz... (13.7MB)
Removing: /Users/seal/Library/Caches/Homebrew/node-10.2.1.sierra.bottle.tar.gz... (13.8MB)
Removing: /Users/seal/Library/Caches/Homebrew/node-9.11.1.sierra.bottle.tar.gz... (13.3MB)
Removing: /Users/seal/Library/Caches/Homebrew/openssl-1.0.2n.sierra.bottle.tar.gz... (3.7MB)
Removing: /Users/seal/Library/Caches/Homebrew/openssl-1.0.2o_1.sierra.bottle.tar.gz... (3.7MB)
Removing: /Users/seal/Library/Caches/Homebrew/php-7.2.4_1.sierra.bottle.tar.gz... (21.6MB)
Removing: /Users/seal/Library/Caches/Homebrew/php-7.2.5.sierra.bottle.tar.gz... (21.6MB)
Removing: /Users/seal/Library/Caches/Homebrew/php-7.2.6.sierra.bottle.tar.gz... (21.6MB)
Removing: /Users/seal/Library/Caches/Homebrew/[email protected]_1.sierra.bottle.tar.gz... (19.4MB)
Removing: /Users/seal/Library/Caches/Homebrew/[email protected] (19.4MB)
Removing: /Users/seal/Library/Caches/Homebrew/[email protected]_1.sierra.bottle.tar.gz... (20MB)
Removing: /Users/seal/Library/Caches/Homebrew/[email protected] (20MB)
Removing: /Users/seal/Library/Caches/Homebrew/[email protected]_1.sierra.bottle.tar.gz... (20.5MB)
Removing: /Users/seal/Library/Caches/Homebrew/python-3.6.5.sierra.bottle.1.tar.gz... (21.9MB)
Removing: /Users/seal/Library/Caches/Homebrew/readline-7.0.3_1.sierra.bottle.tar.gz... (497.3KB)
Removing: /Users/seal/Library/Caches/Homebrew/sqlite-3.23.1.sierra.bottle.tar.gz... (1.4MB)
Removing: /Users/seal/Library/Caches/Homebrew/tomcat-9.0.6.tar.gz.incomplete... (572KB)
Removing: /Users/seal/Library/Caches/Homebrew/webp-0.6.1.sierra.bottle.1.tar.gz... (809.1KB)
Removing: /Users/seal/Library/Caches/Homebrew/wget-1.19.4_1.sierra.bottle.tar.gz... (1.3MB)
Removing: /Users/seal/Library/Caches/Homebrew/xz-5.2.3.sierra.bottle.tar.gz... (462.1KB)
Removing: /Users/seal/Library/Caches/Homebrew/zsh-5.4.2_3.sierra.bottle.tar.gz... (3.3MB)
Removing: /Users/seal/Library/Logs/Homebrew/apr... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/apr-util... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/argon2... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/aspell... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/autoconf... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/brew-php-switcher... (119B)
Removing: /Users/seal/Library/Logs/Homebrew/dash... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/freetds... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/freetype... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/gdbm... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/gettext... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/glib... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/gmp... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/icu4c... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/jpeg... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/libffi... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/libidn2... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/libpng... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/libpq... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/libsodium... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/libtool... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/libunistring... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/libzip... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/mcrypt... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/mhash... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/mysql... (1.3KB)
Removing: /Users/seal/Library/Logs/Homebrew/nginx... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/node... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/openssl... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/pcre... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/php... (12 files, 2.3KB)
Removing: /Users/seal/Library/Logs/Homebrew/[email protected] (12 files, 2.4KB)
Removing: /Users/seal/Library/Logs/Homebrew/[email protected] (12 files, 2.9KB)
Removing: /Users/seal/Library/Logs/Homebrew/[email protected] (12 files, 2.4KB)
Removing: /Users/seal/Library/Logs/Homebrew/python... (3 files, 127.8KB)
Removing: /Users/seal/Library/Logs/Homebrew/readline... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/sqlite... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/tomcat... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/[email protected] (110B)
Removing: /Users/seal/Library/Logs/Homebrew/unixodbc... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/webp... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/wget... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/xz... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/zsh... (68B)
Removing: /Users/seal/Library/Logs/Homebrew/zsh-completions... (117B)
==> This operation has freed approximately 1.3GB of disk space.