在Mac上使用MySQL安装MariaDB

时间:2018-07-05 11:00:10

标签: mysql macos mariadb database-server

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

2 个答案:

答案 0 :(得分:2)

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

更好的方法是为两个数据库服务器设置一个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

玩得开心!

答案 1 :(得分:1)

如果您运行Brew信息,它甚至会警告您不要并排安装它们,因为它们会发生冲突:

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 -h localhost -u root -p登录mysql,都引用了/usr/local/var/mysql上的相同数据目录,都使用了相同的命令,例如{{ 1}},所有这些都表明两者可以互换操作。它们不能重合,除非您将它们安装在诸如vmware之类的不同虚拟机上,或者在文件容器中运行它们(另一个答案中建议这样做)。

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

这是删除MySQL并安装MariaDB的方式。请注意,在我的系统中,我通过HomeBrew使用mysql@5.7,因此我指定了它而不是mysql:

CHECK CONSTRAINTS

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

brew remove mysql@5.7
brew cleanup

但是在我的系统上,我的首选项窗格,启动或自动启动中都没有MySQL。因此,我仅有的其他地方是/ usr / local / var中的实际数据库数据:

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*

但是,由于MariaDB是一个即插即用的替代品,因此您无需删除此数据,并且MariaDB一旦安装就将使用它。

要安装MariaDB:

/usr/local/var/mysql

因此,从安装过程中以及运行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 可以看到,mariadb已安装在

brew info mariadb

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

/usr/local/Cellar/mariadb/10.3.12

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

$ 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

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