Mac通过Homebrew安装的MariaDB重启后无法访问求助
我通过Homebrew在本地Mac上安装了MariaDB,之前一切正常,但电脑重启后就无法访问本地数据库服务器了。/opt/homebrew/var/mysql/中的文件仍存在,但服务启动失败。目前唯一的解决办法是完全重新安装MariaDB并重新配置所有数据库,非常麻烦。
我曾考虑关机前停止MariaDB,但经常忘记。而且这种情况是近期才出现的,之前没有问题。
编辑1:连接错误信息
执行mysql -u root -p后出现以下错误:
mysql -u root -p Enter password: ERROR 2002 (HY000): Can't connect to local server through socket '/tmp/mysql.sock'
编辑2:服务启动失败情况
即使关机前停止MariaDB,重启服务(未关机)也会出问题。电脑重启后执行brew services start mariadb显示启动成功,但实际状态是stopped:
% brew services list Name Status User File httpd started myusername ~/Library/LaunchAgents/homebrew.mxcl.httpd.plist mariadb none root php started myusername ~/Library/LaunchAgents/homebrew.mxcl.php.plist php@7.4 none php@8.0 none php@8.1 none php@8.2 none % brew services start mariadb ==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb) % brew services list Name Status User File httpd started myusername ~/Library/LaunchAgents/homebrew.mxcl.httpd.plist mariadb stopped root ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist php started myusername ~/Library/LaunchAgents/homebrew.mxcl.php.plist php@7.4 none php@8.0 none php@8.1 none php@8.2 none
配置文件内容(/opt/homebrew/etc/my.cnf)
# # This group is read both by the client and the server # use it for options that affect everything # [client-server] # # include *.cnf from the config directory # !includedir /opt/homebrew/etc/my.cnf.d
编辑3:错误日志(/opt/homebrew/var/mysql/mycomputer.local.err)
240815 19:48:34 mysqld_safe Starting mariadbd daemon with databases from /opt/homebrew/var/mysql 2024-08-15 19:48:34 0 [Note] Starting MariaDB 11.4.2-MariaDB source revision 3fca5ed772fb75e3e57c507edef2985f8eba5b12 as process 62946 2024-08-15 19:48:34 0 [Warning] Setting lower_case_table_names=2 because file system for /opt/homebrew/var/mysql/ is case insensitive 2024-08-15 19:48:34 0 [Note] InnoDB: Compressed tables use zlib 1.2.12 2024-08-15 19:48:34 0 [Note] InnoDB: Number of transaction pools: 1 2024-08-15 19:48:34 0 [Note] InnoDB: Using generic crc32 instructions 2024-08-15 19:48:34 0 [Note] InnoDB: Initializing buffer pool, total size = 128.000MiB, chunk size = 2.000MiB 2024-08-15 19:48:34 0 [Note] InnoDB: Completed initialization of buffer pool 2024-08-15 19:48:34 0 [ERROR] InnoDB: Missing FILE_CHECKPOINT(234874341) at 234874341 2024-08-15 19:48:34 0 [ERROR] InnoDB: Log scan aborted at LSN 234874341 2024-08-15 19:48:34 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error 2024-08-15 19:48:34 0 [Note] InnoDB: Starting shutdown... 2024-08-15 19:48:34 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 2024-08-15 19:48:34 0 [Note] Plugin 'FEEDBACK' is disabled. 2024-08-15 19:48:34 0 [Note] Plugin 'wsrep-provider' is disabled. 2024-08-15 19:48:34 0 [ERROR] Unknown/unsupported storage engine: InnoDB 2024-08-15 19:48:34 0 [ERROR] Aborting 240815 19:48:34 mysqld_safe mysqld from pid file /opt/homebrew/var/mysql/digs-bb-mac9.local.pid ended
从错误日志看,核心问题是InnoDB日志文件损坏,导致存储引擎初始化失败。以下是修复步骤:
停止MariaDB服务
brew services stop mariadb备份InnoDB日志文件
先备份防止意外,路径在/opt/homebrew/var/mysql/下的ib_logfile0和ib_logfile1:cd /opt/homebrew/var/mysql/ mv ib_logfile0 ib_logfile0.bak mv ib_logfile1 ib_logfile1.bak添加InnoDB强制恢复参数
编辑/opt/homebrew/etc/my.cnf(或/opt/homebrew/etc/my.cnf.d/server.cnf),在[mysqld]段添加:innodb_force_recovery = 1注:
innodb_force_recovery取值1-6,先从1开始尝试,若不行逐步提高到3(更高值可能导致数据丢失)。启动MariaDB服务
brew services start mariadb如果能成功启动,立即导出所有数据库备份:
mysqldump -u root -p --all-databases > all_databases_backup.sql恢复正常启动
停止服务,移除innodb_force_recovery参数,然后删除原数据库文件(或重命名),重新初始化MariaDB:brew services stop mariadb mv /opt/homebrew/var/mysql /opt/homebrew/var/mysql_old mysql_install_db --user=root --basedir=/opt/homebrew --datadir=/opt/homebrew/var/mysql导入备份数据
启动服务后导入备份:brew services start mariadb mysql -u root -p < all_databases_backup.sql设置自动启动(可选)
确保MariaDB随系统启动:brew services restart mariadb
预防措施
- 关机前尽量手动停止MariaDB:
brew services stop mariadb - 检查Mac的电源设置,避免强制关机导致文件损坏
- 定期备份数据库,减少恢复成本
内容的提问来源于stack exchange,提问作者clive beckett

