Mac下使用homebrew管理多版本mysql同时启动

Mac下使用homebrew管理多版本mysql同时启动

思路

给每个版本分配不同的数据目录和配置文件即可

本文尝试了使用 brew 安装管理多个MySQL版本,同时运行、直接切换

安装

如果已有数据文件请自行备份以及使用

安装 mysql 5.7
shell 复制代码
brew install mysql@5.7

/opt/homebrew/var/mysql 目录为空时,安装过程会自动生成数据目录,可在安装过程中看到执行了生成数据目录的命令 (目录如果有文件则不会执行该命令)

复制代码
/opt/homebrew/Cellar/mysql@5.7/5.7.41/bin/mysqld --initialize-insecure --user=XXXX --basedir=/opt/homebrew/Cellar/mysql@5.7/5.7.41 --datadir=/opt/homebrew/var/mysql --tmpdir=/tmp

准备好5.7的文件

shell 复制代码
mv /opt/homebrew/var/mysql  /opt/homebrew/var/mysql@5.7
cp /opt/homebrew/etc/my.cnf /opt/homebrew/etc/my@5.7.cnf

修改配置文件

  • 指定配置文件--defaults-file
  • 修改datadirWorkingDirectory路径
shell 复制代码
...
ExecStart=/opt/homebrew/opt/mysql@5.7/bin/mysqld_safe --defaults-file=/opt/homebrew/etc/my@5.7.cnf  --datadir=/opt/homebrew/var/mysql@5.7
...
WorkingDirectory=/opt/homebrew/var/mysql@5.7
...

/opt/homebrew/Cellar/mysql@5.7/5.7.41/homebrew.mysql@5.7.service
...
<array>
    <string>/opt/homebrew/opt/mysql@5.7/bin/mysqld_safe</string>
    <string>--defaults-file=/opt/homebrew/etc/my@5.7.cnf</string>
    <string>--datadir=/opt/homebrew/var/mysql@5.7</string>
</array>
...
<key>WorkingDirectory</key>
<string>/opt/homebrew/var/mysql@5.7</string>
...

/opt/homebrew/Cellar/mysql@5.7/5.7.41/homebrew.mxcl.mysql@5.7.plist

更改端口和socket不需要同时运行的可以不改

plain 复制代码
[mysqld]
port = 3307
socket = /tmp/mysql@5.7.sock

/opt/homebrew/etc/my@5.7.cnf

启动 & 测试

shell 复制代码
brew services stop mysql@5.7
brew services start mysql@5.7
安装 mysql 8.0
plain 复制代码
brew install mysql@8.0
brew services start mysql

数据目录和配置文件就不改了,使用默认的

结束

多版本完美运行

分别重置密码

复制代码
mysql_secure_installation -h127.0.0.1 -P3307

连接不同服务端

复制代码
mysql -h127.0.0.1 -P3307
相关推荐
沢田纲吉28 分钟前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
Java水解20 小时前
Mysql查看执行计划、explain关键字详解(超详细)
后端·mysql
知其然亦知其所以然1 天前
MySQL 社招必考题:如何优化查询过程中的数据访问?
后端·mysql·面试
悄然林静1 天前
Mac终端执行`brew doctor`报`openssl@1.1`警告
mac·xcode·apple
DemonAvenger1 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
程序新视界1 天前
如何在MySQL中创建聚集索引?
mysql
AAA修煤气灶刘哥2 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
程序新视界2 天前
学习MySQL绕不开的两个基础概念:聚集索引与非聚集索引
mysql
RestCloud2 天前
跨境数据传输:ETL如何处理时区与日期格式差异
mysql·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql