brew 安装mysql和遇到的问题

如果之前没有安装过MySQL 5.7

brew install mysql@5.7  // 安装
brew link --force mysql@5.7 // 链接
brew services start mysql@5.7 // 启动服务

//这个加入到系统环境的命令,brew中会显示
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc // 输出到环境变量

如果之前安装了 MySQL 5.7

brew uninstall mysql@5.7
rm -rf /usr/local/var/mysql
rm /usr/local/etc/my.cnf

卸载命令
1) 卸载 mysql

brew uninstall mysql
2) 清理缓存

brew cleanup --prune=all mysql
3)删除数据目录

rm -rf /usr/local/var/mysql

ps -ax | grep mysql
#stop and kill any MySQL processes
brew remove mysql
sudo rm /etc/my.cnf
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
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 /var/db/receipts/com.mysql.*
sudo rm -rf /private/var/db/receipts/*mysql*

#如果配置过系统环境记得在.bash_profile和.zhsc中把mysql的删除

重点:

brew安装mysql过程中是会有错误提示的,不是说brew install完就成功了;

安装完给/usr/local/var/mysql权限(这个是brew 安装的数据库储存目录路径)

sudo chown -R mysql:mysql /usr/local/var/mysql

然后把/usr/local/etc/my.cnf,拷贝到/etc/目录下:

sudo cp /usr/local/etc/my.cnf /etc/my.cnf


并把下面的配置文件,加入到/etc/my.cnf中

# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 127.0.0.1
#mysqlx-bind-address = 127.0.0.1
user = mysql

​​​​​​​

具体看下面:

安装mysql的步骤参考的https://blog.csdn.net/stupid56862/article/details/86476136博客
执行brew install mysql@5.7命令的时候输出如下

$ brew install mysql@5.7
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/5.7/manifests/5.7.34
Already downloaded: /Users/bytedance/Library/Caches/Homebrew/downloads/bede69c91ac2034b7482dc9c89c4bfc2c11780da50ac1c9c136dc9b7d45bd96d--mysql@5.7-5.7.34.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/5.7/blobs/sha256:baf50315c60aef1b4598999f95f2
Already downloaded: /Users/bytedance/Library/Caches/Homebrew/downloads/25f0fa24a48854bebec5fbc6420b9dbcf6566bbc3c820f9f95165f07ed695997--mysql@5.7--5.7.34.big_sur.bottle.tar.gz
==> Reinstalling mysql@5.7
==> Pouring mysql@5.7--5.7.34.big_sur.bottle.tar.gz
==> /usr/local/Cellar/mysql@5.7/5.7.34/bin/mysqld --initialize-insecure --user=bytedance --basedir=/
Last 15 lines from /Users/bytedance/Library/Logs/Homebrew/mysql@5.7/post_install.01.mysqld:

/usr/local/Cellar/mysql@5.7/5.7.34/bin/mysqld
--initialize-insecure
--user=bytedance
--basedir=/usr/local/Cellar/mysql@5.7/5.7.34
--datadir=/usr/local/var/mysql
--tmpdir=/tmp

2021-05-07T17:20:17.406513Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-07T17:20:17.553153Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2021-05-07T17:20:17.553203Z 0 [ERROR] Aborting

Warning: The post-install step did not complete successfully
You can try again using:
  brew postinstall mysql@5.7
==> 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

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
==> Summary
🍺  /usr/local/Cellar/mysql@5.7/5.7.34: 319 files, 234.7MB

仔细看错误,[ERROR] --initialize specified but the data directory has files in it. Aborting,意思是mysql存放data的已经有文件了,而我们的data地址是–datadir=/usr/local/var/mysql,所以我就去/usr/local/var/下,把mysql这个文件夹全删了(如果只删除mysql文件夹内的东西,它会一直反复出现,我就直接rm 这个文件夹了)
 

cd /usr/local/var/
rm -rf mysql

然后再次执行brew reinstall mysql@5.7,发现又遇见了错误,打印如下

$ brew install mysql@5.7
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/5.7/manifests/5.7.34
Already downloaded: /Users/bytedance/Library/Caches/Homebrew/downloads/bede69c91ac2034b7482dc9c89c4bfc2c11780da50ac1c9c136dc9b7d45bd96d--mysql@5.7-5.7.34.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/mysql/5.7/blobs/sha256:baf50315c60aef1b4598999f95f2
Already downloaded: /Users/bytedance/Library/Caches/Homebrew/downloads/25f0fa24a48854bebec5fbc6420b9dbcf6566bbc3c820f9f95165f07ed695997--mysql@5.7--5.7.34.big_sur.bottle.tar.gz
==> Reinstalling mysql@5.7
==> Pouring mysql@5.7--5.7.34.big_sur.bottle.tar.gz
==> /usr/local/Cellar/mysql@5.7/5.7.34/bin/mysqld --initialize-insecure --user=bytedance --basedir=/
Last 15 lines from /Users/bytedance/Library/Logs/Homebrew/mysql@5.7/post_install.01.mysqld:

/usr/local/Cellar/mysql@5.7/5.7.34/bin/mysqld
--initialize-insecure
--user=bytedance
--basedir=/usr/local/Cellar/mysql@5.7/5.7.34
--datadir=/usr/local/var/mysql
--tmpdir=/tmp

2021-05-07T17:30:37.406513Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-07T17:30:37.409017Z 0 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/var/mysql/ is case insensitive
2021-05-07T17:30:37.482846Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-07T17:30:37.497524Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-07T17:30:37.553153Z 0 [ERROR] unknown variable 'mysqlx-bind-address=127.0.0.1'
2021-05-07T17:30:37.553203Z 0 [ERROR] Aborting

Warning: The post-install step did not complete successfully
You can try again using:
  brew postinstall mysql@5.7
==> 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

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
==> Summary
🍺  /usr/local/Cellar/mysql@5.7/5.7.34: 319 files, 234.7MB

失败原因是由于[ERROR] unknown variable ‘mysqlx-bind-address=127.0.0.1’,需要去my.cnf文件中去掉这个设置

cd /usr/local/etc
vim vim my.cnf
//删除mysqlx-bind-address = 127.0.0.1这一行后保存

再次执行

brew reinstall mysql@5.7

成功,不再有error
然后启动mysql服务

brew link mysql@5.7  --force
mysql.server start
mysql -uroot    // 进入 mysql ,此时默认没设置密码
select version();  // 在mysql 中查询 mysql 版本
exit     //退出mysql

再进行密码设置

mysql_secure_installation //一路y或n即可,有一个是否禁用远程连接,记得选n

如果遇到其它错误没有头绪的,先给数据库目录权限

brew 安装的mysql数据库目录一般都在/usr/local/var/mysql

sudo chown -R mysql:mysql /usr/local/var/mysql

无论是homebrew等方式,在Mac下都是不会生成my.cnf文件,因为已经使用了最优默认值,如果需要也可以自行新建或配置/etc/my.cnf

加载my.cnf位置顺序查看:

mysql --verbose --help | grep my.cnf

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值