如何在 mac 上安装命令行 MySQL 客户端?

How do I install command line MySQL client on mac?

我想为命令行而不是 GUI 安装 MySQL 客户端。我在网上搜索过,但只找到有关安装 MySQL 服务器的说明。

安装MySQLWorkbench,然后

export PATH=$PATH:/Applications/MySQLWorkbench.app/Contents/MacOS

如果您已经从 http://dev.mysql.com/downloads/) 的磁盘映像 (dmg) 安装了 MySQL,请打开终端,运行:

echo 'export PATH=/usr/local/mysql/bin:$PATH' >> ~/.bash_profile

然后,通过 运行ning 以下命令重新加载 .bash_profile

 . ~/.bash_profile

您现在可以使用 mysql 连接到任何 mysql 服务器:

mysql -h xxx.xxx.xxx.xxx -u username -p

来源与参考:http://www.gigoblog.com/2011/03/13/add-mysql-to-terminal-shell-in-mac-os-x/

最简单的方法是安装 mysql 服务器或 workbench,将 mysql 客户端复制到某处,更新您的路径设置,然后删除您安装的任何内容以获取可执行文件第一名。

最佳选择是:

brew install mysql

如果您从 DMG 安装到 mac,它会创建一个 mysql 客户端,但不会将其放入您的用户路径。

将此添加到您的 .bash_profile

export PATH="/usr/local/mysql/bin:$PATH

这将使您 运行 mysql 随时随地。

Mysql 有一组仅限客户端的实用程序:

Mysql 客户 shell https://dev.mysql.com/downloads/shell/

其他命令行实用程序 https://dev.mysql.com/downloads/utilities/

Mac OSX 版本可用。

打开 "MySQL Workbench" DMG 文件并

# Adjust the path to the version of MySQL Workbench you downloaded
cp "/Volumes/MySQL Workbench 6.3.9.CE/MySQLWorkbench.app/Contents/MacOS/mysql" /usr/local/bin
# Make sure it's executable
chmod +x /usr/local/bin/mysql

弹出 DMG 磁盘

要使用自制程序安装 mysql-shell,运行

brew cask install mysql-shell

然后您可以使用

启动 mysql shell
mysqlsh

如果想直接进入SQL模式,运行

mysqlsh --sql

如之前的回答所述,您可以通过 运行ning

获得 mysql 服务器和客户端库

brew install mysql.

还有仅客户端安装。仅安装客户端库 运行

brew install mysql-connector-c

为了 运行 这些命令,您需要 mac 中的自制程序包管理器。您可以通过 运行ning

安装它

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

使用 MacPorts 您可以安装客户端:

sudo port install mysql57

您还需要select安装的版本作为您的mysql

sudo port select mysql mysql57

仅当您将 -server 附加到软件包名称(例如 mysql57-server)时才会安装服务器

现在有一个 mysql-client 公式。

brew install mysql-client

这严格安装了命令行客户端,没有其他开销:

安装 Homebrew(如果没有):

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

然后,安装mysql-client:

brew install mysql-client

然后,将 mysql-client 二进制目录添加到您的 PATH:

echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile

最后,重新加载您的 bash 个人资料:

source ~/.bash_profile

那么你应该可以在一个终端中运行 mysql,如果不能尝试打开一个新的终端

来自 brew 的安装命令:

$ brew cask install mysql-shell

看看你能做什么:

$ mysqlsh --help

运行 来自 mysqlsh 已安装客户端的查询:

$ mysqlsh --host=192.x.x.x --port=3306 --user=user --password=xxxxx

MySQL Shell 8.0.18

Copyright (c) 2016, 2019, 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 '\?' for help; '\quit' to exit.
WARNING: Using a password on the command line interface can be insecure.
Creating a session to 'user@192.x.x.x:3306'
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 16
Server version: 8.0.18 MySQL Community Server - GPL
No default schema selected; 
type \use <schema> to set one.

 MySQL  192.x.x.x:3306 ssl  JS >

 MySQL  192.x.x.x:3306 ssl  JS > `\use rafdb`

Default schema set to `rafdb`.

如果您需要更轻便的解决方案,我推荐 mysql-shell,使用以下命令安装。

brew cask 安装 mysql-shell

安装后启动 mysqlsh.

mysql 客户端在 macOS 端口中可用。如果您还没有安装这个优秀的第三方包管理器,可以从这里获得:https://www.macports.org/

安装 macports 后,打开终端并确保一切都是最新的:

sudo port selfupdate

ports 仓库中有多个不同版本的 MySQL 和 mariadb(MySQL 的社区分支)。使用以下命令列出可用版本:

port search 'mariadb*'

我建议选择 mariadb 而不是 mysql,因为它主要是替代品 (https://mariadb.com/kb/en/mariadb-vs-mysql-compatibility/) 的一个下降,并且具有出色的社区支持。

如果适用,请选择所需的 mariadb 版本(此处提供了 mariadb 版本列表:https://downloads.mariadb.org/mariadb/+releases/)。如果您不介意,请安装默认版本:

sudo port install mariadb

Mariadb(包括 mysql 兼容的命令行客户端)现在可以在您的系统上使用。在我的系统上,CLI 客户端位于以下位置:

$ /opt/local/bin/mysql --version
/opt/local/bin/mysql  Ver 15.1 Distrib 5.5.68-MariaDB, for osx10.15 (x86_64) using readline 5.1

每次要用客户端的时候都要输入完整路径,明显有点不方便,/opt/local/bin/mysql。 Ports早就想到了这个问题。要查看系统上 mysql 的可用版本,运行:

$ port select mysql

Available versions for mysql:
mariadb (active)
none

从列表中选择一个。例如,要使用 mariadb 作为默认 mysql 客户端:

sudo port select mysql mariadb

现在打开一个新的终端window,你应该可以启动 mariadb mysql CLI 客户端:

mysql -h <hostname> -u <username> -p