在Mac上使用Homebrew安装的MySQL 5.6和MySQL 5.7之间切换

在Mac上使用Homebrew安装的MySQL 5.6和MySQL 5.7之间切换

背景

当按项目切换MySQL版本时,我用Homebrew安装的。
我通常使用RubyMine开发它,并且我想要防止引用mysql2由gem 引用的lib(libmysqlclient.20.dylib和libmysqlclient.18.dylib)的引用错误。

切换点

  • 1、/usr/local/var/mysql/通过切换每个版本来使用数据库设置和数据()

  • 2、使用brew unlink和brew link切换活动版本的MySQL

程序

例如,假设安装了MySQL 5.7,并且希望稍后安装MySQL 5.6。

  • 1、/usr/local/var/mysql/ 重命名并撤离。

由于存储了设置和DB数据,因此应该撤离。

$ mv /usr/local/var/mysql /usr/local/var/mysql_57
  • 2、删除MySQL 5.7的符号链接。
$ brew unlink mysql

这个符号链接 指的是诸如
/usr/local/bin/mysql -> ../Cellar/mysql/5.7.10/bin/mysql

和之类的
/usr/local/lib/libmysqlclient.20.dylib -> ../Cellar/mysql/5.7.10/lib/libmysqlclient.20.dylib
东西。

brew unlink取消他们的链接。(很方便,不是吗?
如果此符号链接存在,安装另一个版本将导致以下错误。

$ brew install mysql56
==> Reinstalling homebrew/versions/mysql56
==> Downloading https://homebrew.bintray.com/bottles-versions/mysql56-5.6.27.el_capitan.bottle.tar.gz
Already downloaded: /Library/Caches/Homebrew/mysql56-5.6.27.el_capitan.bottle.tar.gz
==> Pouring mysql56-5.6.27.el_capitan.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/innochecksum
Target /usr/local/bin/innochecksum
is a symlink belonging to mysql. You can unlink it:
  brew unlink mysql

To force the link and overwrite all conflicting files:
  brew link --overwrite mysql56

To list all files that would be deleted:
  brew link --overwrite --dry-run mysql56

之后

$ brew unlink mysql && brew link mysql56

尽管可以通过执行链接来重新分配链接,但最好首先清除链接,因为发出大量日志很难发现错误。

3.安装MySQL 5.6。

较旧版本与版本名称一起打包。
(现在安装5.7版本不需要mysql版本)

$ brew search mysql

mysql
homebrew/versions/mysql51
homebrew/versions/mysql55
homebrew/versions/mysql56

你会在列表中看到一些东西。
因为这次我想安装MySQL 5.6,所以使用mysql 56。

$ brew install mysql56

4.启动MySQL 5.6并检查版本。

$ mysql.server start
Starting MySQL
.. SUCCESS!
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.27 Homebrew

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

我能够确认MySQL 5.6启动。
如果版本中显示5.7,我们检查日志等,因为有可能无法替换符号链接。

5.停止MySQL 5.6并保存数据。

$ mysql.server stop
Shutting down MySQL
. SUCCESS!
$ mv /usr/local/var/mysql /usr/local/var/mysql_56

6.确认最初安装的MySQL 5.7启动。

$ mv /usr/local/var/mysql_57 /usr/local/var/mysql
$ brew unlink mysql56 && brew link mysql
Unlinking /usr/local/Cellar/mysql56/5.6.27... 106 symlinks removed
Linking /usr/local/Cellar/mysql/5.7.10... 92 symlinks created
$ mysql.server start
Starting MySQL
. SUCCESS!
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.10 Homebrew

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

我能够确认MySQL 5.7启动。

摘要

过来

  • 停止运行MySQL
  • 在设置和数据目录(usr/local/var/mysql)之间切换
  • brew unlink并brew link与切换活动的MySQL
  • 启动MySQL
    现在你可以切换。
    由于每次手工切换都很困难,所以我想是否要做一个切换的脚本。

参考:/usr/local/var/mysql/为什么有必要切换

如果至少启动一次5.7,如果使用相同的数据运行5.6,将出现以下错误。

Starting MySQL
. ERROR! The server quit without updating PID file (/usr/local/var/mysql/ogin.local.pid).

由于mysqld等没有动,似乎是权威,但解决不了。
(由于目录结构根据版本不同而不同,可能会影响权限以外的内容)

MySQL 5.6和MySQL 5.7(/usr/local/var/mysql/)的初始数据目录结构

// MySQL 5.6

drwxr-xr-x  10 ogi  admin       340  2 26 11:27 .
drwxr-xr-x  11 ogi  admin       374  2 26 11:27 ..
-rw-rw----   1 ogi  admin        56  2 26 11:27 auto.cnf
-rw-rw----   1 ogi  admin  50331648  2 26 11:27 ib_logfile0
-rw-rw----   1 ogi  admin  50331648  2 26 11:26 ib_logfile1
-rw-rw----   1 ogi  admin  12582912  2 26 11:27 ibdata1
drwx------  81 ogi  admin      2754  2 26 11:26 mysql
-rw-r-----   1 ogi  admin      6362  2 26 11:27 ogin.local.err
drwx------  55 ogi  admin      1870  2 26 11:26 performance_schema
drwx------   2 ogi  admin        68  2 26 11:26 test

// MySQL 5.7

drwxr-xr-x   18 ogi  admin       612  2 26 11:33 .
drwxr-xr-x   11 ogi  admin       374  2 26 11:33 ..
-rw-r-----    1 ogi  admin        56  2 26 11:33 auto.cnf
-rw-------    1 ogi  admin      1680  2 26 11:33 ca-key.pem
-rw-r--r--    1 ogi  admin      1075  2 26 11:33 ca.pem
-rw-r--r--    1 ogi  admin      1079  2 26 11:33 client-cert.pem
-rw-------    1 ogi  admin      1676  2 26 11:33 client-key.pem
-rw-r-----    1 ogi  admin       408  2 26 11:33 ib_buffer_pool
-rw-r-----    1 ogi  admin  50331648  2 26 11:33 ib_logfile0
-rw-r-----    1 ogi  admin  50331648  2 26 11:33 ib_logfile1
-rw-r-----    1 ogi  admin  12582912  2 26 11:33 ibdata1
drwxr-x---   77 ogi  admin      2618  2 26 11:33 mysql
drwxr-x---   90 ogi  admin      3060  2 26 11:33 performance_schema
-rw-------    1 ogi  admin      1680  2 26 11:33 private_key.pem
-rw-r--r--    1 ogi  admin       452  2 26 11:33 public_key.pem
-rw-r--r--    1 ogi  admin      1079  2 26 11:33 server-cert.pem
-rw-------    1 ogi  admin      1680  2 26 11:33 server-key.pem
drwxr-x---  108 ogi  admin      3672  2 26 11:33 sys

你可能感兴趣的:(在Mac上使用Homebrew安装的MySQL 5.6和MySQL 5.7之间切换)