Mac使用 brew 安装mysql5.7

1.清除之前安装的MySQL

先停止所有mysql有关进程

sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /var/db/receipts/com.mysql.*

如果brew之前安装过也要卸载

2. 安装MySQL

执行brew install [email protected],
然后出现

➜  ~ brew install [email protected]
==> Downloading https://homebrew.bintray.com/bottles/[email protected]
Already downloaded: /Users/duheyuan/Library/Caches/Homebrew/downloads/9d9f10181613ae442dfb00f24c9b74b30752d8b19c25d8d4c98889597773730d--mysql@5.7-5.7.29.catalina.bottle.tar.gz
==> Pouring [email protected]
==> /usr/local/Cellar/[email protected]/5.7.29/bin/mysqld --initialize-insecure --user=duheyuan --basedir=/u
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

[email protected] is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"


To have launchd start [email protected] now and restart at login:
  brew services start [email protected]
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/[email protected]/bin/mysql.server start
==> Summary
  /usr/local/Cellar/[email protected]/5.7.29: 319 files, 232.3MB

安装完成,接下来我们要根据提示运行
echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc, 把MySQL路径写到zshrc里,没装zsh的写入到bash_profile.

然后来一个灵魂一击,source ~/.zshrc or source ~/.bash_profile

启动MySQL

brew services start [email protected]or /usr/local/opt/[email protected]/bin/mysql.server start.
要想直接使用mysql.server start需要配置zshrc文件.
vim ~/.zshrc打开后添加alias mysql="/usr/local/opt/[email protected]/bin/mysql"
然后再来一遍source ~/.zshrc

搞定

最后记一下安装过程中出现的问题

在启动时候出现了一个错误
ERROR! The server quit without updating PID file (/usr/local/var/mysql/MacBook-Pro.local.pid).
找了很多帖子尝试发现都不行,因为里面是本地的数据库文件,平时开发用的线上数据库所以干脆放弃删除掉了,然后重新安装数据库,okay了

你可能感兴趣的:(homebrew)