本指南适用于使用 Homebrew 安装 MySQL、PostgreSQL、MongoDB、Redis、MinIO 的命令行客户端,仅用于远程连接,不安装或运行本地服务端。


✅ MySQL 客户端(mysql)

安装

brew install mysql-client

配置环境变量(必需)

echo 'export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

验证

mysql --version

连接示例

mysql -h your_host -u your_user -p

✅ PostgreSQL 客户端(psql)

安装

brew install libpq

配置环境变量(必需)

echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

验证

psql --version

连接示例

psql -h your_host -U your_user -d your_db

✅ MongoDB 客户端(mongosh)

安装

brew install mongosh

验证

mongosh --version

连接示例

mongosh "mongodb://your_user:your_pass@your_host:27017"

✅ Redis 客户端(redis-cli)

安装

brew install redis

验证

redis-cli --version

连接示例

redis-cli -h your_host -p 6379

✅ MinIO 客户端(mc)

安装

brew install minio/stable/mc

验证

mc --version

连接示例

mc alias set myminio https://your-minio-host:9000 your-access-key your-secret-key

使用示例

mc ls myminio
mc cp ./localfile.txt myminio/mybucket/

💪 强化版交互式连接命令

为了避免每次输入冗长的数据库或服务连接参数,可以通过以下 交互式别名函数 封装常用客户端命令。

使用时,只需执行函数名(如 mysqlc、psqlc、mongoshc 等),即可在命令行中通过提示逐步输入连接信息。

每个函数都会自动提供 默认参数(如主机、端口、用户名等),只需按回车即可使用默认值。

🚀 可用交互式别名

函数名

说明

mysqlc

交互式连接 MySQL / MariaDB

psqlc

交互式连接 PostgreSQL

mongoshc

交互式连接 MongoDB

redisc

交互式连接 Redis

mcc

交互式配置 MinIO 客户端别名(mc alias set)

  • 只需要在.zshrc加入如下内容并source即可

#clent-alias
mysqlc() {
  printf "Host (default 127.0.0.1): "
  read host
  host=${host:-127.0.0.1}
​
  printf "Port (default 3306): "
  read port
  port=${port:-3306}
​
  printf "User (default root): "
  read user
  user=${user:-root}
​
  printf "Password: "
  read -s password
  echo
​
#  printf "Database (default test): "
#  read db
#  db=${db:-test}
​
  mysql -h "$host" -P "$port" -u "$user" -p"$password" "$db"
}
​
psqlc() {
  printf "Host (default 127.0.0.1): "
  read host
  host=${host:-127.0.0.1}
​
  printf "Port (default 5432): "
  read port
  port=${port:-5432}
​
  printf "User (default postgres): "
  read user
  user=${user:-postgres}
​
  printf "Password: "
  read -s password
  echo
​
 # printf "Database (default postgres): "
 # read db
 # db=${db:-postgres}
​
  PGPASSWORD="$password" psql -h "$host" -p "$port" -U "$user" "$db"
}
​
mongoshc() {
  printf "Host (default 127.0.0.1): "
  read host
  host=${host:-127.0.0.1}
​
  printf "Port (default 27017): "
  read port
  port=${port:-27017}
​
  printf "User (default root): "
  read user
  user=${user:-root}
​
  printf "Password: "
  read -s password
  echo
​
  printf "Database (default admin): "
  read db
  db=${db:-admin}
​
  mongosh "mongodb://$user:$password@$host:$port/$db?authSource=$db"
}
​
redisc() {
  printf "Host (default 127.0.0.1): "
  read host
  host=${host:-127.0.0.1}
​
  printf "Port (default 6379): "
  read port
  port=${port:-6379}
​
  printf "Password (leave empty if none): "
  read -s password
  echo
​
  if [ -z "$password" ]; then
    redis-cli -h "$host" -p "$port"
  else
    redis-cli -h "$host" -p "$port" -a "$password"
  fi
}
​
mcc() {
  printf "Alias name (default minio): "
  read alias
  alias=${alias:-minio}
​
  printf "Endpoint (default http://127.0.0.1:9000): "
  read endpoint
  endpoint=${endpoint:-http://127.0.0.1:9000}
​
  printf "Access Key (default minioadmin): "
  read access_key
  access_key=${access_key:-minioadmin}
​
  printf "Secret Key: "
  read -s secret_key
  echo
​
  mc alias set "$alias" "$endpoint" "$access_key" "$secret_key"
}