前言
自從macOS升級至Mojave後小問題不斷,現在使用Homebrew安裝MySQL預設也是8.0版本,和Python類似我們依賴的元件可能還不支援高版本,新版本的某些更新和調整導致比如Sequel Pro和mysqldb不可用。當然我們也可以使用官方的MySQLWorkbench,之前介紹瞭如何在macOS下安裝多版本Python,現在繼續分享macOS如何降級MySQL。
macOS Mojave 降級安裝 MySQL 5.7
更新歷史
2018年12月17日 – 初稿
閱讀原文 – https://wsgzao.github.io/post…
擴充套件閱讀
Install MySQL 5.7 on macOS Mojave – https://medium.com/@at0dd/ins…
正常安裝
預設新版本是MySQL 8.0
brew update
brew install mysql
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
To have launchd start mysql now and restart at login:
brew services start mysql
Or, if you don`t want/need a background service you can just run:
mysql.server start
解除安裝現有版本
無論是官方dmg還是brew都記得先備份重要資料後再清理
# 正常關閉並刪除MySQL
mysql.server stop
brew services stop mysql
brew remove mysql
# 無法正常刪除MySQL
ps -ax | grep mysql
stop and kill any MySQL processes
brew remove mysql
brew cleanup
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/var/mysql
sudo rm -rf /usr/local/mysql*
安裝MySQL指定版本
# 安裝MySQL 5.7
brew install mysql@5.7
brew link --force mysql@5.7
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
mysql@5.7 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 mysql@5.7 first in your PATH run:
echo `export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"` >> ~/.zshrc
For compilers to find mysql@5.7 you may need to set:
export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib"
export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include"
For pkg-config to find mysql@5.7 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/mysql@5.7/lib/pkgconfig"
To have launchd start mysql@5.7 now and restart at login:
brew services start mysql@5.7
Or, if you don`t want/need a background service you can just run:
/usr/local/opt/mysql@5.7/bin/mysql.server start
# 開機自啟動
brew services restart mysql@5.7
# 設定環境變數
echo `export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"` >> ~/.zshrc
# 手動啟動和關閉
/usr/local/opt/mysql@5.7/bin/mysql.server start
/usr/local/opt/mysql@5.7/bin/mysql.server stop
安裝mysqldb
Python中最連線Mysql常用的驅動是:
- mysql-python :mysql的C語言的驅動
- mysql-connector:msql官方的驅動
- pymysql:python語言的驅動
# 使用MySQLdb ,但是提示importerror no module named mysqldb
brew install mysql-connector-c
pip install mysql-python
# 可能會出現以下錯誤,按照提示做即可
Error: Cannot install mysql because conflicting formulae are installed.
mysql-connector-c: because both install MySQL client libraries
Please `brew unlink mysql-connector-c` before continuing.
Unlinking removes a formula`s symlinks from /usr/local. You can
link the formula again after the install finishes. You can --force this
install, but the build may fail or cause obscure side-effects in the
resulting software.