总结mac下使用多个版本mysql的种种问题

安装问题

  1. homebrew安装
    现在官网下一个homebrew
brew install mysql #安装最新版
brew install mysql@8.0 #安装8.0版
  1. 官网下载
    由于homebrew不支持下载5.7版本,可在官网下载
    官网地址
    其中5.7.32之后下拉的操作系统里面没有mac,之前的有

配置问题

使用mysql简要来说有四个步骤:

  1. 初始化
  2. 启动
  3. 进入,巴拉巴拉操作,退出
  4. 关闭

其中关键文件是my**.cnf文件,**是版本。
为了方便管理,我们在/opt/homebrew/etc下创建my57.cnf,my80.cnf,my93.cnf
各自版本的我是这么写的,端口设置也在下面

[mysqld]
basedir    = /opt/homebrew/opt/mysql@8.0
datadir    = /opt/homebrew/var/mysql80
socket     = /opt/homebrew/var/mysql80/mysql.sock
port       = 3308      # 或者你为 8.0 单独指定的端口
log-error  = /opt/homebrew/var/mysql80/mysqld.err
pid-file   = /opt/homebrew/var/mysql80/mysqld.pid
[mysqld]
basedir    = /usr/local/mysql-5.7.18-macos10.12-x86_64
datadir    = /opt/homebrew/var/mysql57
socket     = /opt/homebrew/var/mysql57/mysql.sock
port       = 3310
default_authentication_plugin = mysql_native_password
log-error  = /opt/homebrew/var/mysql57/mysqld.err
pid-file   = /opt/homebrew/var/mysql57/mysqld.pid
[mysqld]
port=3309
socket=/opt/homebrew/var/mysql93/mysql.sock
datadir=/opt/homebrew/var/mysql93

初始化操作如下,具体命令你需要找到你对应版本的mysqld就行

# 对 5.7 来说,用 5.7 的 mysqld
/usr/local/mysql-5.7.18-macos10.12-x86_64/bin/mysqld \
  --defaults-file=/opt/homebrew/etc/my57.cnf \
  --initialize-insecure

# 对 8.0 来说,用 8.0 的 mysqld
/opt/homebrew/opt/mysql@8.0/bin/mysqld \
  --defaults-file=/opt/homebrew/etc/my80.cnf \
  --initialize-insecure

# 对 9.3 来说,用 9.x 的 mysqld
/opt/homebrew/opt/mysql/bin/mysqld \
  --defaults-file=/opt/homebrew/etc/my93.cnf \
  --initialize-insecure

启动为了方便我们进行shell环境配置

# 👉 启动 MySQL 8.0
alias start_mysql80="/opt/homebrew/opt/mysql@8.0/bin/mysqld_safe --defaults-file=/opt/homebrew/etc/my80.cnf &"

# 👉 连接 MySQL 8.0
alias mysql80="/opt/homebrew/opt/mysql@8.0/bin/mysql -u root -S /opt/homebrew/var/mysql80/mysql.sock"

# 👉 关闭 MySQL 8.0
alias stop_mysql80="/opt/homebrew/opt/mysql@8.0/bin/mysqladmin -u root -S /opt/homebrew/var/mysql80/mysql.sock shutdown"

# 👉 启动 MySQL 9.3
alias start_mysql93="/opt/homebrew/opt/mysql/bin/mysqld_safe --defaults-file=/opt/homebrew/etc/my93.cnf &"

# 👉 连接 MySQL 9.3
alias mysql93="/opt/homebrew/opt/mysql/bin/mysql -u root -S /opt/homebrew/var/mysql93/mysql.sock"

# 👉 关闭 MySQL 9.3
alias stop_mysql93="/opt/homebrew/opt/mysql/bin/mysqladmin -u root -S /opt/homebrew/var/mysql93/mysql.sock shutdown"
# 定义 MySQL 5.7 安装目录
export MYSQL57_HOME=/usr/local/mysql-5.7.18-macos10.12-x86_64

# 把 5.7 的 bin 放到 PATH 前面,优先调用
export PATH=$MYSQL57_HOME/bin:$PATH

# 启动/停止 5.7 服务
alias start_mysql57="mysqld --defaults-file=/opt/homebrew/etc/my57.cnf --daemonize"
alias stop_mysql57="$MYSQL57_HOME/bin/mysqladmin --defaults-file=/opt/homebrew/etc/my57.cnf --socket=/opt/homebrew/var/mysql57/mysql.sock -u root shutdown"
# 进入 5.7 的 mysql 客户端
alias mysql57="sudo $MYSQL57_HOME/bin/mysql -u root -S /opt/homebrew/var/mysql57/mysql.sock"
### === END ===

然后我们执行

start_mysql80
mysql80

就能进入了

stop_mysql80

就可以退出了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值