4

我正在尝试使用 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
--------------
4

2 回答 2

8

如果你运行 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。

于 2019-01-25T15:12:14.487 回答
6

同时安装 MySQL 和 MariaDB 的问题不在于端口冲突(两个服务器默认绑定到端口 3306),因为这可以在服务器配置中更改。问题在于 MariaDB 是 MySQL 的替代品,因此对二进制文件使用相同的路径和名称(例如,mysqld 用于服务器,mysql 用于客户端)。因此,它们的设计方式是一个安装了一个或另一个,但不能同时安装两者。

更好的方法是为两个数据库服务器设置一个 Docker 容器并使用它。这种方法还有一个魅力,那就是如果需要的话,还可以运行两个数据库服务器的多个不同版本。不过,您仍然必须将每个容器映射到不同的端口。

MySQL 5 和 MariaDB 10的简单docker-compose.yml设置如下所示:

version: '2'
services:
  mysql5:
    image: mysql:5
    ports:
     - "3305:3306/tcp"
    environment:
      - MYSQL_ROOT_PASSWORD=secret_password
      - MYSQL_USER=user
      - MYSQL_PASSWORD=user_password_here
      - MYSQL_DATABASE=my_db
  mariadb10:
    image: mariadb:10
    ports:
     - "3310:3306/tcp"
    environment:
      - MYSQL_ROOT_PASSWORD=secret_password
      - MYSQL_USER=user
      - MYSQL_PASSWORD=user_password_here
      - MYSQL_DATABASE=my_db

假设您的系统上安装了 docker-compose,您可以通过docker-compose up -d在终端中键入文件目录来启动这两个容器。docker-compose.yml

MySQL 5 将在端口 3305 上可用,MariaDB 在端口 3310 上可用。(这将有可能在端口 3306 上拥有本机 MySQL 或 MariaDB。)应调整数据库用户、密码和数据库名称的环境变量,课程。如果您需要不同的版本,MySQL 和 MariaDB 的版本也是如此。

容器启动后(docker-compose up -d),您可以通过以下方式连接到它们:

mysql --host=::1 --user=user --password my_db --port=3310

(注意提供正确的端口参数。在这种情况下,端口 3310 用于 MariaDB 10。)

输入用户密码后,user您可以发出 SQL 查询:

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.5-10.3.8-MariaDB-1:10.3.8+maria~jessie mariadb.org binary distribution

Copyright (c) 2000, 2018, 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> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| my_db              |
+--------------------+
2 rows in set (0,00 sec)

mysql> USE my_db;
Database changed
mysql> SHOW TABLES;
Empty set (0,00 sec)

mysql> quit
Bye

玩得开心!

于 2018-07-12T20:55:37.057 回答