rake db:用mysql2 gem创建错误(无法为{“adapter”=>“mysql2”创建数据库)

问题描述 投票:3回答:2

我正试图在我的debian 7盒子上使用ruby 2.0和rails 4一起运行,并且我在使用mysql2 gem连接到mysql数据库时遇到问题。

我使用下面的命令来设置周围的一切:

\curl -L https://get.rvm.io | bash -s stable
rvm install ruby   
rvm use ruby --default
rvm rubygems current 
gem install rails --no-ri --no-rdoc

当我尝试运行rake db:create时,我收到了以下错误:


no implicit conversion of Fixnum into String
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13/lib/mysql2/client.rb:58:in `connect'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13/lib/mysql2/client.rb:58:in `initialize'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `new'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `mysql2_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
/home/mayrun/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/home/mayrun/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:79:in `retrieve_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:53:in `connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/mysql_database_tasks.rb:8:in `connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/mysql_database_tasks.rb:16:in `create'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:72:in `create'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:86:in `block in create_current'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:182:in `block in each_current_configuration'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:181:in `each'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:181:in `each_current_configuration'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:85:in `create_current'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:20:in `block (2 levels) in '
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/home/mayrun/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/bin/rake:33:in `'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/bin/rake:23:in `load'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/bin/rake:23:in `'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `'
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "database"=>"myapp_development", "pool"=>5, "username"=>"dev", "password"=>password, "socket"=>"/var/run/mysqld/mysqld.sock"}
no implicit conversion of Fixnum into String
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13/lib/mysql2/client.rb:58:in `connect'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/mysql2-0.3.13/lib/mysql2/client.rb:58:in `initialize'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `new'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `mysql2_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
/home/mayrun/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/home/mayrun/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:79:in `retrieve_connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:53:in `connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/mysql_database_tasks.rb:8:in `connection'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/mysql_database_tasks.rb:16:in `create'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:72:in `create'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:86:in `block in create_current'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:182:in `block in each_current_configuration'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:181:in `each'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:181:in `each_current_configuration'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:85:in `create_current'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:20:in `block (2 levels) in '
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/home/mayrun/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/gems/rake-10.1.0/bin/rake:33:in `'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/bin/rake:23:in `load'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247@global/bin/rake:23:in `'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval'
/home/mayrun/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `'
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "database"=>"myapp_test", "pool"=>5, "username"=>"dev", "password"=>password, "socket"=>"/var/run/mysqld/mysqld.sock"}

深入了解我的检查:

  • mysql启动并运行
  • mysql的用户名和密码都是正确的。
  • mysql正在侦听localhost和127.0.0.1
  • /var/run/mysqld/mysqld.sock文件存在
  • 我查看了mysql日志,看起来很像mysql2甚至没有尝试连接。
  • 当用较旧的,较慢的mysql gem替换mysql2 gem时,问题就消失了。

我的mysql版本:

$ mysql --version    
mysql  Ver 14.14 Distrib 5.5.31, for debian-linux-gnu (x86_64) using readline 6.2    

这是我的database / config.yml文件:


development:
  adapter: mysql2
  encoding: utf8
  database: myapp_development
  pool: 5
  username: dev
  password: password
  host: localhost
  socket: /var/run/mysqld/mysqld.sock

test:
  adapter: mysql2
  encoding: utf8
  database: myapp_test
  pool: 5
  username: dev
  password: password
  socket: /var/run/mysqld/mysqld.sock

production:
  adapter: mysql2
  encoding: utf8
  database: myapp_production
  pool: 5
  username: dev
  password: password
  socket: /var/run/mysqld/mysqld.sock

本地宝石清单:

*** LOCAL GEMS ***

actionmailer (4.0.0)
actionpack (4.0.0)
activemodel (4.0.0)
activerecord (4.0.0)
activerecord-deprecated_finders (1.0.3)
activesupport (4.0.0)
arel (4.0.1)
atomic (1.1.14)
builder (3.1.4)
bundler (1.3.5)
bundler-unload (1.0.2)
coffee-rails (4.0.1)
coffee-script (2.2.0)
coffee-script-source (1.6.3)
erubis (2.7.0)
execjs (2.0.2)
executable-hooks (1.2.6)
hike (1.2.3)
i18n (0.6.5)
jbuilder (1.5.2)
jquery-rails (3.0.4)
json (1.8.1)
mail (2.5.4)
mime-types (1.25)
minitest (4.7.5)
multi_json (1.8.2)
mysql2 (0.3.13)
polyglot (0.3.3)
rack (1.5.2)
rack-test (0.6.2)
rails (4.0.0)
railties (4.0.0)
rake (10.1.0)
rdoc (3.12.2)
rubygems-bundler (1.4.1)
rvm (1.11.3.8)
sass (3.2.12)
sass-rails (4.0.1)
sdoc (0.3.20)
sprockets (2.10.0)
sprockets-rails (2.0.1)
thor (0.18.1)
thread_safe (0.1.3)
tilt (1.4.1)
treetop (1.4.15)
turbolinks (1.3.0)
tzinfo (0.3.38)
uglifier (2.3.0)

安装包:

$ sudo dpkg -l | grep mysql
ii  libdbd-mysql-perl                  4.021-1+b1                    amd64        Perl5 database interface to the MySQL database
ii  libmysqlclient-dev                 5.5.31+dfsg-0+wheezy1         amd64        MySQL database development files
ii  libmysqlclient18:amd64             5.5.31+dfsg-0+wheezy1         amd64        MySQL database client library
ii  mysql-client                       5.5.31+dfsg-0+wheezy1         all          MySQL database client (metapackage depending on the latest version)
ii  mysql-client-5.5                   5.5.31+dfsg-0+wheezy1         amd64        MySQL database client binaries
ii  mysql-common                       5.5.31+dfsg-0+wheezy1         all          MySQL database common files, e.g. /etc/mysql/my.cnf
ii  mysql-server                       5.5.31+dfsg-0+wheezy1         all          MySQL database server (metapackage depending on the latest version)
ii  mysql-server-5.5                   5.5.31+dfsg-0+wheezy1         amd64        MySQL database server binaries and system database setup
ii  mysql-server-core-5.5              5.5.31+dfsg-0+wheezy1         amd64        MySQL database server binaries
ii  php5-mysql                         5.4.4-14+deb7u5               amd64        MySQL module for php5
mysql ruby-on-rails ruby gem mysql2
2个回答
8
投票

对于每个人仍然挣扎,记得在一个单独的终端运行mysqld。


1
投票

请确保你的mysql.sock在ubuntu 14.04中是正确的,它位于/var/run/mysqld/mysqld.sock

© www.soinside.com 2019 - 2024. All rights reserved.