hea*_*y12 4 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
--------------
Run Code Online (Sandbox Code Playgroud)
如果您运行 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
Run Code Online (Sandbox Code Playgroud)
事实上,运行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
Run Code Online (Sandbox Code Playgroud)
什么是直接更换?它指的是在不需要任何其他代码或配置更改且不会产生负面影响的情况下,用另一个软件组件替换一个软件组件的能力。
因此,mysql 和 mariadbmysql.server start都使用运行,都使用 登录到 mysql mysql -h localhost -u root -p,都引用相同的数据目录 at /usr/local/var/mysql,都使用相同的命令,例如mysqldump,所有这些都表明两者可以互换操作。它们不能重合,除非您将它们安装在不同的虚拟机上,例如 vmware 或在 docket 容器中运行它们(这是在另一个答案中建议的)。
但是,如果您不能在单独的虚拟机或文档容器中运行它们,那么我强烈建议删除 MySQL 并使用 MariaDB,因为 MariaDB 保持与 MySQL 的兼容性,但还包含其他功能,例如CHECK CONSTRAINTS.
这是删除 MySQL 并安装 MariaDB 的方式。请注意,在我的系统中,我通过 HomeBrew 使用了 mysql@5.7,因此我指定了它而不是 mysql:
brew remove mysql@5.7
brew cleanup
Run Code Online (Sandbox Code Playgroud)
就是这样。一些指南建议删除单个目录,例如:
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*
Run Code Online (Sandbox Code Playgroud)
但是在我的系统上,我的首选项窗格或启动中甚至没有自动启动的 MySQL。所以我唯一拥有的其他地方是 /usr/local/var 中的实际数据库数据:
/usr/local/var/mysql
Run Code Online (Sandbox Code Playgroud)
但由于 MariaDB 是直接替代品,您无需删除这些数据,一旦安装,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
Run Code Online (Sandbox Code Playgroud)
因此,从安装和运行中可以看出brew info mariadb,mariadb 已安装在
/usr/local/Cellar/mariadb/10.3.12
Run Code Online (Sandbox Code Playgroud)
并且 $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
Run Code Online (Sandbox Code Playgroud)
对我来说,由于我已经有一个带有 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]>
Run Code Online (Sandbox Code Playgroud)
如您所见,现在它使用的是 MariaDB。
同时安装 MySQL 和 MariaDB 的问题不在于端口冲突(默认情况下,两台服务器都绑定到端口 3306),因为这可以在服务器配置中更改。问题在于 MariaDB 是 MySQL 的替代品,因此对二进制文件使用相同的路径和名称(例如,mysqld 用于服务器,mysql 用于客户端)。因此,它们的设计方式是安装一个或另一个,但不能同时安装。
更好的方法是为两个数据库服务器设置一个 Docker 容器并使用它。这种方法还有一个魅力,如果需要,还可以运行两个数据库服务器的多个不同版本。不过,您仍然必须将每个容器映射到不同的端口。
一个简单的docker-compose.ymlMySQL 5 和 MariaDB 10 设置可能如下所示:
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
Run Code Online (Sandbox Code Playgroud)
假设您的系统上安装了 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
Run Code Online (Sandbox Code Playgroud)
(注意提供正确的端口参数。在这种情况下,端口 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
Run Code Online (Sandbox Code Playgroud)
玩得开心!
| 归档时间: |
|
| 查看次数: |
10694 次 |
| 最近记录: |