MacOS 经验集锦

码不停提

2026-02-20
37 次浏览
0 条评论

Mac 日常使用遇到的问题及解决方法记录;开发工具,环境配置,常用工具。

脚本工具
MacOS
分享:

新 Mac OS 安装 Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

直到安装完成

==> Next steps:

- Run these commands in your terminal to add Homebrew to your PATH:
  echo >> /Users/wenqidong/.zprofile
  echo 'eval "$(/opt/homebrew/bin/brew shellenv zsh)"' >> /Users/wenqidong/.zprofile
  eval "$(/opt/homebrew/bin/brew shellenv zsh)"
- Run brew help to get started
- Further documentation:
  https://docs.brew.sh

添加环境变量,测试 brew 命令

wenqidong@wenqidongdeMac-mini ~ %  echo >> /Users/wenqidong/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv zsh)"' >> /Users/wenqidong/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv zsh)"
wenqidong@wenqidongdeMac-mini ~ % brew help
Example usage:
brew search TEXT|/REGEX/
brew info [FORMULA|CASK...]
brew install FORMULA|CASK...
brew update
brew upgrade [FORMULA|CASK...]
brew uninstall FORMULA|CASK...
brew list [FORMULA|CASK...]

Troubleshooting:
brew config
brew doctor
brew install --verbose --debug FORMULA|CASK

Contributing:
brew create URL [--no-fetch]
brew edit [FORMULA|CASK...]

Further help:
brew commands
brew help [COMMAND]
man brew
https://docs.brew.sh
wenqidong@wenqidongdeMac-mini ~ %

安装 MySQL

wenqidong@wenqidongdeMac-mini ~ % brew search mysql
==> Formulae
automysqlbackup         mysql-client            mysql-connector-c++     mysql@8.0               perl-dbd-mysql
mysql                   mysql-client@8.0        mysql-search-replace    mysql@8.4               qt-mysql
mysql++                 mysql-client@8.4        mysql-to-sqlite3        mysqltuner              sqlite3-to-mysql

==> Casks
mysql-shell                   mysqlworkbench                navicat-for-mysql             sqlpro-for-mysql
wenqidong@wenqidongdeMac-mini ~ % brew install mysql@8.4

安装完成

We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -u root

mysql@8.4 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have mysql@8.4 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/mysql@8.4/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql@8.4 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/mysql@8.4/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/mysql@8.4/include"

To start mysql@8.4 now and restart at login:
  brew services start mysql@8.4
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql@8.4/bin/mysqld_safe --datadir\=/opt/homebrew/var/mysql
==> Summary
🍺  /opt/homebrew/Cellar/mysql@8.4/8.4.8: 321 files, 315.2MB
==> Running `brew cleanup mysql@8.4`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
==> Caveats
==> mysql@8.4
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -u root

mysql@8.4 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have mysql@8.4 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/mysql@8.4/bin:$PATH"' >> ~/.zshrc

For compilers to find mysql@8.4 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/mysql@8.4/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/mysql@8.4/include"

To start mysql@8.4 now and restart at login:
  brew services start mysql@8.4
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql@8.4/bin/mysqld_safe --datadir\=/opt/homebrew/var/mysql

安装PHP

wenqidong@wenqidongdeMac-mini ~ % brew search php
==> Formulae
brew-php-switcher       php@8.1 (deprecated)    phpbrew                 phpunit                 pyp
php                     php@8.2                 phpmd (deprecated)      pop
php-code-sniffer        php@8.3                 phpmyadmin              pup (deprecated)
php-cs-fixer            php@8.4                 phpstan                 pcp (deprecated)

==> Casks
eclipse-php                             phd2 (deprecated)                       phpstorm
wenqidong@wenqidongdeMac-mini ~ % brew install php@8.3

安装完成

To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /opt/homebrew/opt/php@8.3/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /opt/homebrew/etc/php/8.3/

php@8.3 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have php@8.3 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/php@8.3/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/opt/homebrew/opt/php@8.3/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@8.3 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/php@8.3/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/php@8.3/include"

To start php@8.3 now and restart at login:
  brew services start php@8.3
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/php@8.3/sbin/php-fpm --nodaemonize
==> Summary
🍺  /opt/homebrew/Cellar/php@8.3/8.3.30: 522 files, 93.5MB
==> Running `brew cleanup php@8.3`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
==> Caveats
==> php@8.3
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /opt/homebrew/opt/php@8.3/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /opt/homebrew/etc/php/8.3/

php@8.3 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have php@8.3 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/php@8.3/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/opt/homebrew/opt/php@8.3/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@8.3 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/php@8.3/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/php@8.3/include"

To start php@8.3 now and restart at login:
  brew services start php@8.3
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/php@8.3/sbin/php-fpm --nodaemonize

安装 Redis

wenqidong@wenqidongdeMac-mini ~ % brew search redis
==> Formulae
hiredis                       redis-leveldb                 redir                         redu
iredis                        redis@6.2 (deprecated)        redict                        redo
redis                         redis@8.2                     reddix

==> Casks
another-redis-desktop-manager           medis                                   redis-pro
jpadilla-redis (deprecated)             redis-insight                           zedis
wenqidong@wenqidongdeMac-mini ~ % brew install redis
wenqidong@wenqidongdeMac-mini ~ % brew install redis
✔︎ JSON API cask.jws.json                                                                     Downloaded   15.3MB/ 15.3MB
✔︎ JSON API formula.jws.json                                                                  Downloaded   32.0MB/ 32.0MB
==> Fetching downloads for: redis
✔︎ Bottle Manifest redis (8.6.0)                                                              Downloaded   10.9KB/ 10.9KB
✔︎ Bottle redis (8.6.0)                                                                       Downloaded    1.2MB/  1.2MB
==> Pouring redis--8.6.0.arm64_tahoe.bottle.tar.gz
==> Caveats
To start redis now and restart at login:
  brew services start redis
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/redis/bin/redis-server /opt/homebrew/etc/redis.conf
==> Summary
🍺  /opt/homebrew/Cellar/redis/8.6.0: 15 files, 3MB
==> Running `brew cleanup redis`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).

安装 nvm

wenqidong@wenqidongdeMac-mini ~ % brew search nvm 
==> Formulae
convmv      mvnvm       nvm         xnvme       nvi         nvc         nrm         nom         nim         dvm

==> Casks
nvs
wenqidong@wenqidongdeMac-mini ~ % nvm -v
zsh: command not found: nvm
wenqidong@wenqidongdeMac-mini ~ % node -v
zsh: command not found: node
wenqidong@wenqidongdeMac-mini ~ % brew install nvm

安装完成

Please note that upstream has asked us to make explicit managing
nvm via Homebrew is unsupported by them and you should check any
problems against the standard nvm install method prior to reporting.

You should create NVM's working directory if it doesn't exist:
  mkdir ~/.nvm

Add the following to your shell profile e.g. ~/.profile or ~/.zshrc:
  export NVM_DIR="$HOME/.nvm"
  [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

You can set $NVM_DIR to any location, but leaving it unchanged from
/opt/homebrew/Cellar/nvm/0.40.4 will destroy any nvm-installed Node installations
upon upgrade/reinstall.

Type `nvm help` for further information.
==> Summary
🍺  /opt/homebrew/Cellar/nvm/0.40.4: 10 files, 214.4KB
==> Running `brew cleanup nvm`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).

安装 node

wenqidong@wenqidongdeMac-mini ~ % nvm install v20
Downloading and installing node v20.20.0...
Downloading https://nodejs.org/dist/v20.20.0/node-v20.20.0-darwin-arm64.tar.xz...
################################################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.20.0 (npm v10.8.2)
Creating default alias: default -> v20 (-> v20.20.0 *)
wenqidong@wenqidongdeMac-mini ~ % node -v
v20.20.0

v2rayNG 安装问题

M1 MacOS 安装 v2rayN arm64 版,启动时报错;

“v2rayN” is damaged and can’t be opened. You should move it to the Trash.

解决方法:使用终端解除限制

xattr -cr /path/to/v2rayN.app
  • /path/to/v2rayN.app 替换为 v2rayN 程序的实际路径。
  • xattr 命令用于清除文件的扩展属性,解决 macOS 的安全限制问题。

尝试再次打开 v2rayN。

MacMini 同时连接两个网段

Mac Mini 同时连接 Wi-Fi 和以太网,且两者不在同一网段时,系统默认会根据‌网络服务顺序‌决定优先使用哪个网络,但不会自动根据目标 IP 智能切换路径。要实现“访问不同 IP 时自动选择网络”,需要结合‌服务顺序设置‌与‌静态路由配置‌来实现精准分流。

调整网络服务顺序(基础设置)

首先确保你希望优先用于外网访问的网络(如 Wi-Fi)排在前面:

  1. 打开 ‌系统设置 > 网络
  2. 点击右下角 ‌“...” > 设定服务顺序
  3. 将 Wi-Fi 拖动到以太网的上方
  4. 点击“应用”保存

此时,系统会优先通过 Wi-Fi 访问互联网,但所有流量都会走 Wi-Fi,可能导致无法访问以太网内的特定网段。

若要实现“按目标 IP 走不同网络”,仅调整服务顺序是不够的,需进一步配置路由规则。

通过命令行手动添加路由规则,使访问特定内网 IP 段时强制走以太网,其余流量走 Wi-Fi。

操作步骤:

  1. 查看以太网连接的路由器网关地址(例如:192.168.1.1
  2. 确定需要通过以太网访问的目标 IP 段(如:192.168.1.0/24
  3. 打开终端,执行以下命令(需管理员权限):
sudo route -n add -net 192.168.1.0/24 192.168.1.1

路由的存放位置与生命周期,然后给出查看、修改、删除的命令示例,最后说明如何把临时路由做成开机持久(LaunchDaemon)并提供可直接用的脚本和 plist 示例。

  1. 路由在哪里?
  • route -n add -net 添加的是内核的路由表(kernel routing table),是即时生效的但默认是非持久的 —— 重启或网络接口重置后会丢失。
  • 内核路由表不是写在某个单一配置文件里(不像 /etc/hosts),所以“查看”是读取内核的路由表条目。
  1. 查看当前路由表(常用)
# 列出路由表(IPv4,不做域名解析)
netstat -nr -f inet

# 或:
sudo netstat -rn

# 查询到某个目标地址会走哪条路由
route -n get 192.168.1.100

# 查看本机接口与地址(方便确定 gateway/interface)
ifconfig
# 映射接口名到硬件端口(例如 en0 对应哪个物理/虚拟端口)
networksetup -listallhardwareports

说明:

  • netstat -nr 显示路由表的完整条目(Destination/Gateway/Flags/Refs/Use/Mtu/Interface)。
  • route -n get <addr> 会显示到单个目标的详细路由决策(包括使用的网关、接口和标志)。
  1. 添加 / 删除 / 修改 路由(示例)
  • 添加网络路由(临时,立即生效):
# 例:将 192.168.10.0/24 的流量通过网关 10.0.0.1 发出
sudo route -n add -net 192.168.10.0/24 10.0.0.1

# 也可以直接指定接口(不指定 gateway)
sudo route -n add -net 192.168.10.0/24 -interface en0
  • 删除路由:
# 删除同样的网络路由
sudo route -n delete -net 192.168.10.0/24
  • 修改路由 macOS 上通常做法是先 delete 再 add(因为直接“修改”支持不一定一致):
sudo route -n delete -net 192.168.10.0/24
sudo route -n add -net 192.168.10.0/24 10.0.0.2

(某些 BSD/macOS 版本支持 route change,但为兼容起见先��再加最可靠。)

  1. 什么时候路由会消失
  • 重启机器会丢失(默认非持久)。
  • 如果路由是绑定到某个接口,当接口重置或断开时路由也可能被移除。
  1. 要持久化(开机自动添加)怎么办? 推荐做法:把需要的 route 命令写成脚本,放 /usr/local/sbin(或 /usr/local/bin),然后用 LaunchDaemon 在系统启动时以 root 运行该脚本。下面给出完整示例。

脚本:/usr/local/sbin/add-routes.sh

#!/bin/sh
# 等待网络稳定(必要时调整)
sleep 5

# 示例:添加两个路由
/sbin/route -n add -net 192.168.10.0/24 10.0.0.1
/sbin/route -n add -net 10.20.0.0/16 10.0.0.2

exit 0
  • 保存后:sudo chmod 755 /usr/local/sbin/add-routes.sh

LaunchDaemon plist(/Library/LaunchDaemons/com.example.routes.plist)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.example.routes</string>

    <key>ProgramArguments</key>
    <array>
      <string>/bin/sh</string>
      <string>/usr/local/sbin/add-routes.sh</string>
    </array>

    <key>RunAtLoad</key>
    <true/>

    <!-- 可选:如果想在崩溃后重试 -->
    <key>KeepAlive</key>
    <false/>
  </dict>
</plist>

安装并启用:

# 把 plist 放到 /Library/LaunchDaemons(需要 root)
sudo cp com.example.routes.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/com.example.routes.plist
sudo chmod 644 /Library/LaunchDaemons/com.example.routes.plist

# bootstrap(macOS 10.11+ 推荐)
sudo launchctl bootstrap system /Library/LaunchDaemons/com.example.routes.plist

# 如果需要立即运行一次(可选)
sudo launchctl start com.example.routes

# 卸载/停止
sudo launchctl bootout system /Library/LaunchDaemons/com.example.routes.plist

注意事项:

  • launchctl 的具体子命令在不同 macOS 版本上细节有差异(load/unload 老方式,bootstrap/bootout 新方式)。如果 bootstrap 报错,可以改用 sudo launchctl load /Library/LaunchDaemons/…(兼容旧系统)。
  • 有时需要脚本等待网络接口完全就绪(示例脚本里用了 sleep 5),更健壮的做法是循环检查目标网关或接口 IP 是否存在再添加路由。
  • plist 必须 owned by root:wheel 且权限 644,否则不会被 launchd 接受。
  1. 调试与日志
  • 用 netstat -nr 验证路由是否已添加。
  • 检查系统日志:sudo log show --predicate 'process == "launchd"' --last 1h 或者 /var/log/system.log(基于系统版本)。
  • 若脚本没有执行,检查 plist 权限与 ownership,并确认 launchctl bootstrap/load 没报错。

如果你希望,我可以:

  • 针对你要添加的具体路由(给我网络/掩码和网关/接口),帮你生成一个可用的 add-routes.sh + plist 并给出安装命令。
  • 或者给出一个更健壮的“等待网络就绪”脚本模板(循环检测接口/网关可达)。

你想把哪个具体的路由持久化?把目标网段、网关和接口发给我,我会给你完整可用的脚本和 plist。

评论区 (0)

你需要先 登录 后才能发表评论。
还没有人评论,赶快成为第一个吧。