You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac OS High Sierra执行rake db:create报错,重装MySQL仍未解决求指引

Hey Brandon, let's tackle this mysql2 gem issue you're facing on Mac OS High Sierra. The error clearly points to a missing libmysqlclient.20.dylib file, which usually happens when the mysql2 gem was compiled against an older version of MySQL, and your recent reinstall changed the library path or version. Here are the most effective fixes to try:

解决方向与具体步骤

1. 重新编译mysql2 gem(最优先尝试)

Your existing mysql2 gem is still linked to the old library path from before you reinstalled MySQL. Let's rebuild it to point to the current MySQL installation:

  • First, uninstall the existing mysql2 gem:
    gem uninstall mysql2
    
  • Install the gem again, explicitly specifying your MySQL's library and include paths to ensure it binds correctly (note we're using your gem version 0.4.10):
    gem install mysql2 -v 0.4.10 -- --with-mysql-lib=/usr/local/opt/mysql/lib --with-mysql-include=/usr/local/opt/mysql/include
    
  • If you're using Bundler, refresh your project's dependencies:
    bundle install
    

2. 验证MySQL与mysql2版本兼容性

The libmysqlclient.20.dylib is associated with MySQL 5.7. If you reinstalled MySQL 8.0, it uses libmysqlclient.21.dylib instead, which won't work with the older mysql2 0.4.10 gem.

  • Check your current MySQL version:
    mysql --version
    
    • If you have MySQL 8.0: Upgrade your mysql2 gem to a version that supports MySQL 8.0 (like 0.5.x or newer) by updating your Gemfile and running bundle update mysql2.
    • If you need to stick with mysql2 0.4.10: Downgrade MySQL to 5.7 to match the library version the gem expects.

3. 直接修复动态库链接

If recompiling the gem doesn't work, you can manually adjust the library path in the mysql2 bundle file using install_name_tool:

  • First, confirm the current libmysqlclient file in your MySQL installation:
    ls /usr/local/opt/mysql/lib/libmysqlclient*.dylib
    
  • Suppose you find libmysqlclient.21.dylib (from MySQL 8.0), run this command to update the bundle's linked library:
    install_name_tool -change /usr/local/opt/mysql/lib/libmysqlclient.20.dylib /usr/local/opt/mysql/lib/libmysqlclient.21.dylib /Users/brandon/Documents/GitHub/projectParent/project/.bundle/ruby/2.2.0/extensions/x86_64-darwin-17/2.2.0-static/mysql2-0.4.10/mysql2/mysql2.bundle
    

4. 清除Bundler缓存并刷新依赖

Sometimes Bundler caches old build configurations. Let's clear that and start fresh:

  • Clean the Bundler cache:
    bundle clean --force
    
  • Re-download and reinstall all dependencies:
    bundle install --redownload
    

Also, make sure your system can find MySQL tools by adding its bin path to your shell profile:

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

内容的提问来源于stack exchange,提问作者Brandon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:33:36