Ruby on Rails Rake Error

2019-06-20 23:32发布

问题:

EDIT: Solved the problem, thanks to this forum post: http://forums.aptana.com/viewtopic.php?f=20&t=7563&p=27407&hilit=libmysql.dll#p27407. Thanks everyone!

I've started learning RoR and have been trying to use rake db:migrate but I keep getting the same error. I can connect to the MySQL database using C:\dev\railslist>mysql -u root railslist_development -p. rake db:migrate --trace produces the following:

C:\dev\railslist>rake db:migrate --trace
(in C:/dev/railslist)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
rake aborted!
Mysql::Error: query: not connected: CREATE TABLE 'schema_migrations' ('version'
varchar(255) NOT NULL) ENGINE=InnoDB
C:/Ruby19/lib/ruby/gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connecti
on_adapters/abstract_adapter.rb:219:in 'rescue in log'
C:/Ruby19/lib/ruby/gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connecti
on_adapters/abstract_adapter.rb:202:in 'log'
C:/Ruby19/lib/ruby/gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connecti
on_adapters/mysql_adapter.rb:323:in 'execute'
C:/Ruby19/lib/ruby/gems/1.9.1/gems/activerecord-2.3.5/lib/active_record/connecti
on_adapters/abstract/schema_statements.rb:114:in 'create_table'
...

My database.yml file is as follows:

development:
  adapter: mysql
  database: railslist_development
  username: root
  password: **********
  host: localhost

  ...

EDIT: Sorry, I got mixed up there... I can connect to the MySQL database using mysql connect localhost - it produces a long list of commands and variables. Also if I enter mysql -h localhost -u root -p I can log into the MySQL prompt. So to clarify: I can connect to the MySQL database via the command line, however in RoR Rake produces an error.

回答1:

This is almost definitely because your mysql instance is not running or you haven't configured config/database.yml to be pointed at the right database for your environment (usually Development). Here are a couple of things to try -

  • Check your config/database.yml - where is your host (if no host listed, it'll connect to localhost)
  • Try running mysql -h localhost and see if mysql is running.

EDIT: If you're not able to connect to your localhost database, then the problem is there, not with Rails. Make sure you have it running, and that your permissions are set correctly to allow connection from your machine. Also, try connecting as root from your local machine, to see if it's a more granular issue (such as you have local connections enabled, but not for the user you're using in Rails).

EDIT 2: In this case, your problem likely is that your database has not been created. Simply go to a command line and type the following:

mysql -u root -p -e 'create database railslist_development;'

This should create the database and allow you to run your migration.



回答2:

To clarify here - the problem is that the client MySQL library does not work currently with Rails 2.3.

To fix this:

  1. Download an older version of the client library here
  2. Drop this file in your ruby bin directory
  3. Restart your MySQL service

    [1]: [1]: http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll "here"



回答3:

I'm not answering your question per se, but since you're just learning, why not stick to the simpler sqlite for now?



回答4:

The DB is not created.

When using MySql, before running rake db:migrate you should create the DB doing:

rake db:create

Or create the database manually via SQL commands.

mysql -h localhost -u root -p
> CREATE DATABASE railslist_development;

I hope this helps you.