M1macos12.3修复mysql无法启动与无法连接的问题

 
Category: Debug

写在前面

接上面的一篇文章, MacOS升级到12.3之后就出现了一些奇奇怪怪的bug, 之前才修复了Python(installed with brew)打开之后被killed的问题, 据说这个问题是在macOS下特有的一个问题.

解决这个之后, mysql也就不闪退了, 但是mysql不能像之前一样正常启动了, 总是提示密码错误, 或者服务无法启动, 或者pid文件找不到, permission denied之类的…..

后来一直尝试过很多解决方案, 比如通过源码进行安装, 完全卸载之后进行安装以及修改my.cnf等, 显示的问题也开始变得奇奇怪怪起来.

今天突发奇想, 打开了xxx.err这个文件, 查看类其中的报错内容, 才最终得到了解决的办法…

报错分析与解决

❯ mysql.server start
Starting MySQL
. ERROR! The server quit without updating PID file (/opt/homebrew/var/mysql/xxx.pid).

其中xxx.err文件(部分)如下:

 cat /opt/homebrew/var/mysql/xxx.err
2022-05-01T02:48:31.6NZ mysqld_safe Logging to '/opt/homebrew/var/mysql/xxx.err'.
2022-05-01T02:48:31.6NZ mysqld_safe Starting mysqld daemon with databases from /opt/homebrew/var/mysql
2022-05-01T02:48:31.649790Z 0 [System] [MY-010116] [Server] /opt/homebrew/opt/mysql/bin/mysqld (mysqld 8.0.28) starting as process 2462
2022-05-01T02:48:31.651378Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /opt/homebrew/var/mysql/ is case insensitive
2022-05-01T02:48:31.651399Z 0 [ERROR] [MY-010123] [Server] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
2022-05-01T02:48:31.651452Z 0 [ERROR] [MY-010119] [Server] Aborting
2022-05-01T02:48:31.651527Z 0 [System] [MY-010910] [Server] /opt/homebrew/opt/mysql/bin/mysqld: Shutdown complete (mysqld 8.0.28)  Homebrew.
2022-05-01T02:48:31.6NZ mysqld_safe mysqld from pid file /opt/homebrew/var/mysql/xxx.pid ended
2022-05-01T02:48:41.6NZ mysqld_safe Logging to '/opt/homebrew/var/mysql/xxx.err'.
2022-05-01T02:48:41.6NZ mysqld_safe Starting mysqld daemon with databases from /opt/homebrew/var/mysql
2022-05-01T02:48:41.961331Z 0 [System] [MY-010116] [Server] /opt/homebrew/opt/mysql/bin/mysqld (mysqld 8.0.28) starting as process 2587
2022-05-01T02:48:41.963039Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /opt/homebrew/var/mysql/ is case insensitive
2022-05-01T02:48:41.963060Z 0 [ERROR] [MY-010123] [Server] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!

这时候就要从Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!这里寻找问题的答案了. 这里参考了Stack Overflow的一个答案1, 通过添加user的方法, 就可以了, 注意修改的my.cnf文件的位置在:

❯ vi /opt/homebrew/etc/my.cnf

添加了最后一行:

  1 # Default Homebrew MySQL server config
  2 [mysqld]
  3 # Only allow connections from localhost
  4 bind-address = 127.0.0.1
  5 mysqlx-bind-address = 127.0.0.1
  6 user=root

此时服务就能正确启动了, 但是还是显示密码不对, 这时候就通过MySQL8.0 的忘记密码解决方案来做就好了, 这里参考了2.

# 关闭服务
mysql.server stop
ps ax | grep mysql
kill -9 PID

# 不认证密码进入
# 如果提示`mysqld: File './binlog.index' not found (OS errno 13 - Permission denied)`
# 可以采用sudo
sudo $(brew --prefix mysql)/bin/mysqld_safe --skip-grant-tables --skip-networking &

新开终端然后输入mysql就能进入交互命令行了, 如果显示下面的服务未开启的话就可以输入:

❯ mysql
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (61)
❯ sudo su
sh-3.2# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.28 Homebrew

Copyright (c) 2000, 2022, 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.

mysql>

然后在MySQL命令行界面输入:

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEWPASSWORD';
Query OK, 0 rows affected (0.00 sec)

mysql> \q;
Bye

最后别忘了杀掉对应的进程:

❯ ps ax|grep mysql                                                                                                                          
50853 s001  SN     0:00.02 sudo ./mysqld --skip-grant-tables --skip-networking --user=root
50854 s001  SN     0:00.41 ./mysqld --skip-grant-tables --skip-networking --user=root
51223 s001  R+     0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox mysql

❯ sudo kill -9 50853
[1]  + 50853 killed     sudo ./mysqld --skip-grant-tables --skip-networking --user=root

❯ sudo kill -9 50854

这时候再通过sudo mysql.server start开启服务, 采用mysql -uroot -p就能愉快进入MySQL了~

后记

这次安装MySQL时候就没有显示很多的报错信息, 可能brew修复了这个问题, 因为之前每次运行brew install mysql, 虽然成功安装, 但是都会出现一些问题, 就是显示后面15行的信息, 然后提示采用brew postinstall mysql进行安装, 这次却没有出现这个问题.

参考