Start webrick in port 80 (mysql2 gem issue)

Howard Huang picture Howard Huang · Apr 30, 2011 · Viewed 8.5k times · Source

I am trying to get my webrick serve in port 80. I tried:

rails server -p 80

but get a access denied. I later understood that using port 1-1024 needs a root permission, so I tried this instead

sudo rails server -p 80

However, now i get a mysql2 error this time. Don't know what happened here. I tried to run:

rails server -p 1025

and it's working fine, I think it's something about root and mysql2. Can anyone help?

below is the error message in console:

/Library/Ruby/Gems/1.8/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError) Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.2.7/lib/mysql2/mysql2.bundle from /Library/Ruby/Gems/1.8/gems/mysql2-0.2.7/lib/mysql2.rb:8 from /Library/Ruby/Gems/1.8/gems/bundler-1.0.12/lib/bundler/runtime.rb:68:in require' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.12/lib/bundler/runtime.rb:68:inrequire' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.12/lib/bundler/runtime.rb:66:in each' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.12/lib/bundler/runtime.rb:66:inrequire' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.12/lib/bundler/runtime.rb:55:in each' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.12/lib/bundler/runtime.rb:55:inrequire' from /Library/Ruby/Gems/1.8/gems/bundler-1.0.12/lib/bundler.rb:120:in require' from /Users/ccool2486/Sites/fs2_test/config/application.rb:7 from /Library/Ruby/Gems/1.8/gems/railties-3.0.6/lib/rails/commands.rb:28:inrequire' from /Library/Ruby/Gems/1.8/gems/railties-3.0.6/lib/rails/commands.rb:28 from /Library/Ruby/Gems/1.8/gems/railties-3.0.6/lib/rails/commands.rb:27:in tap' from /Library/Ruby/Gems/1.8/gems/railties-3.0.6/lib/rails/commands.rb:27 from script/rails:6:inrequire' from script/rails:6

Answer

thekindofme picture thekindofme · Apr 30, 2011

It seems that the mysql2 gem is not installed in the ruby run time that is used. however as this works for you without sudo, i suspect that you have rvm installed and you are using the rvm ruby (which has mysql installed) by default when lunching rails without sudo. but when you do sudo you are most probably using the system ruby which doesn't have mysql2 installed.

so in short if you have rvm installed and is using that. just use:

rvmsudo rails server -p 80