MacOS 经验集锦
码不停提
Mac 日常使用遇到的问题及解决方法记录;开发工具,环境配置,常用工具。
码不停提
Mac 日常使用遇到的问题及解决方法记录;开发工具,环境配置,常用工具。
/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 ~ %
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
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
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`).
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`).
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
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。
Mac Mini 同时连接 Wi-Fi 和以太网,且两者不在同一网段时,系统默认会根据网络服务顺序决定优先使用哪个网络,但不会自动根据目标 IP 智能切换路径。要实现“访问不同 IP 时自动选择网络”,需要结合服务顺序设置与静态路由配置来实现精准分流。
首先确保你希望优先用于外网访问的网络(如 Wi-Fi)排在前面:
此时,系统会优先通过 Wi-Fi 访问互联网,但所有流量都会走 Wi-Fi,可能导致无法访问以太网内的特定网段。
若要实现“按目标 IP 走不同网络”,仅调整服务顺序是不够的,需进一步配置路由规则。
通过命令行手动添加路由规则,使访问特定内网 IP 段时强制走以太网,其余流量走 Wi-Fi。
192.168.1.1)192.168.1.0/24)sudo route -n add -net 192.168.1.0/24 192.168.1.1
路由的存放位置与生命周期,然后给出查看、修改、删除的命令示例,最后说明如何把临时路由做成开机持久(LaunchDaemon)并提供可直接用的脚本和 plist 示例。
# 列出路由表(IPv4,不做域名解析)
netstat -nr -f inet
# 或:
sudo netstat -rn
# 查询到某个目标地址会走哪条路由
route -n get 192.168.1.100
# 查看本机接口与地址(方便确定 gateway/interface)
ifconfig
# 映射接口名到硬件端口(例如 en0 对应哪个物理/虚拟端口)
networksetup -listallhardwareports
说明:
# 例:将 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
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,但为兼容起见先��再加最可靠。)
脚本:/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
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
注意事项:
如果你希望,我可以:
你想把哪个具体的路由持久化?把目标网段、网关和接口发给我,我会给你完整可用的脚本和 plist。
暂无目录