首页 > 解决方案 > 在 Mac 上使用 MySQL 安装 MariaDB

问题描述

我正在尝试使用 brew 在我的 Mac 上安装 MariaDB。但是,由于它与 MySQL 冲突,我很难安装它。我想知道是否有人可以建议如何设置它,所以我同时拥有 MariaDB 和 MySQL,因为我在我的机器上都需要它们,因为我处理需要使用其中一个或另一个的多个项目。

3x-iMac:~ admin$ mysql.server start
Starting MariaDB
 SUCCESS! 

3x-iMac:~ admin$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

3x-iMac:~ admin$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MySQL [(none)]> \s
--------------
mysql  Ver 15.1 Distrib 10.3.8-MariaDB, for osx10.13 (x86_64) using readline 5.1

Connection id:      24
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server:         MySQL
Server version:     8.0.11 MySQL Community Server - GPL
Protocol version:   10
Connection:     Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:        /tmp/mysql.sock
Uptime:         2 hours 47 min 30 sec

Threads: 6  Questions: 1257  Slow queries: 0  Opens: 154  Flush tables: 2  Open tables: 130  Queries per second avg: 0.125
--------------

标签: mysqlmacosmariadbdatabase-server

解决方案


如果你运行 brew info,它甚至会警告你不要并排安装它们,因为它们会发生冲突:

brew info mysql
mysql: stable 8.0.13 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/8.0/en/
Conflicts with:
  mariadb (because mysql, mariadb, and percona install the same binaries.)
  mariadb-connector-c (because both install plugins)
  mysql-cluster (because mysql, mariadb, and percona install the same binaries.)
  mysql-connector-c (because both install MySQL client libraries)
  percona-server (because mysql, mariadb, and percona install the same binaries.)
Not installed

事实上,运行brew info mariadb它会说它是一个Drop-in Replacement

brew info mariadb
mariadb: stable 10.3.12 (bottled)
Drop-in replacement for MySQL
https://mariadb.org/
Conflicts with:
  mariadb-connector-c (because both install plugins)
  mysql (because mariadb, mysql, and percona install the same binaries.)
  mysql-cluster (because mariadb, mysql, and percona install the same binaries.)
  mysql-connector-c (because both install MySQL client libraries)
  mytop (because both install `mytop` binaries)
  percona-server (because mariadb, mysql, and percona install the same binaries.)
/usr/local/Cellar/mariadb/10.3.12 (658 files, 174.4MB) *
  Poured from bottle on 2019-01-25 at 09:50:26
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mariadb.rb
==> Dependencies

什么是直接替换?它指的是用另一个软件组件替换一个软件组件而不需要任何其他代码或配置更改并且不会产生负面影响的能力。

因此,mysql 和 mariadb 都使用 运行mysql.server start,都使用 登录 mysql mysql -h localhost -u root -p,都引用相同的数据目录/usr/local/var/mysql,都使用相同的命令,例如mysqldump,所有这些都表明两者可以互换操作。它们不能重合,除非您将它们安装在 vmware 等不同的虚拟机上或在案卷容器中运行它们(在另一个答案中建议)。

但是如果您不能在单独的虚拟机或文档容器中运行它们,那么我强烈建议您删除 MySQL 并使用 MariaDB,因为 MariaDB 保持与 MySQL 的兼容性,但还包含其他功能,例如CHECK CONSTRAINTS.

这就是您将删除 MySQL 并改为安装 MariaDB 的方式。请注意,在我的系统中,我通过 HomeBrew 使用 mysql@5.7,所以我指定它而不是 mysql:

brew remove mysql@5.7
brew cleanup

就是这样。一些指南建议删除单个目录,例如:

sudo rm /usr/local/mysql
sudo rm -rf /usr/local/var/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*

但是在我的系统上,我的首选项窗格中或启动中没有 MySQL,甚至没有自动启动。所以我唯一拥有的其他地方是 /usr/local/var 中的实际数据库数据:

/usr/local/var/mysql

但是由于 MariaDB 是一个 Drop-In Replacement,因此您无需删除这些数据,MariaDB 将在安装后使用它。

所以要安装 MariaDB:

brew install mariadb
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/services).
==> New Formulae
...
==> Updated Formulae
...
==> Deleted Formulae
...
==> Downloading https://homebrew.bintray.com/bottles/mariadb-10.3.12.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mariadb-10.3.12.mojave.bottle.tar.gz
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

MySQL is configured to only allow connections from localhost by default

To connect:
    mysql -uroot

To have launchd start mariadb now and restart at login:
  brew services start mariadb
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
  /usr/local/Cellar/mariadb/10.3.12: 658 files, 174.4MB

从安装和运行中可以看出brew info mariadb,mariadb 已安装在

/usr/local/Cellar/mariadb/10.3.12

并且 $PATH 中的 mysql 可执行文件指向该 MariaDB 二进制文件:

$ which mysql
/usr/local/bin/mysql
$ ls -l /usr/local/bin/mysql
lrwxr-xr-x  1 viggy  admin  35 Jan 25 09:50 /usr/local/bin/mysql -> ../Cellar/mariadb/10.3.12/bin/mysql

对我来说,因为我已经有一个带有 mysql@5.7 的数据目录,MariaDB 能够使用它并且我仍然可以访问我的数据(尽管仍然鼓励在删除 mysql 之前使用 mysqldump 备份数据库):

mysql -h localhost -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.12-MariaDB Homebrew

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

USE my_old_database;
Database changed
MariaDB [my_old_database]>

如您所见,现在它使用的是 MariaDB。


推荐阅读