MySQL 版本回退

一般来说,本机通过 brew install mysql 安装到的是默认的最新的 MySQL,这里是 MySQL 8。

因为云端使用的老版本,比如 MySQL 5.7。考虑到环境一致性减少部署的麻烦,应当把本地环境也切到 5.7。

直接安装 brew install mysql@5.7 会发现无法登入的情况,报如下的错:

$ mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

解决办法:

  • 卸载本地 MySQL
$ brew uninstall mysql@5.7
  • 删除相应文件
$ rm -rf /usr/local/var/mysql
$ rm /usr/local/etc/my.cnf
  • 重新安装 MySQL 5.7
$ brew install mysql@5.7
  • 将 brew 的服务指向该版本
$ brew link --force mysql@5.7
  • 配置命令行,fish shell 为列:
$ echo 'set -g fish_user_paths "/usr/local/opt/mysql@5.7/bin" $fish_user_paths' >> ~/.config/fish/config.fish
  • 启动 MySQL 服务
$ brew services start mysql@5.7
  • 登入及使用
$ mysql -uroot                                                                       23:42:16
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 Homebrew

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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>

相关资源