I am using Rails v2.3. and MySQL v5.1
I created a new Rails environment named "special" by copy the config/environments/development.rb to config/environments/special.rb
Then, I defined the following thing in config/database.yml :
special:
adapter: mysql2
host: localhost
username: My_user_name
password: My_pwd
database: special_db
encoding: latin1
Then, I go to the command line to run the command:
$ RAILS_ENV=special rake db:create
also tried $rake db:create RAILS_ENV=special
I expect a new database named special_db should be created, but it isn't .
Why? Why I have created a new environment and run db:create in that environment, but database is not created? Am I missing something?
Have you created the database in MySQL?
mysqladmin create special_db
Are you sure that the username you are using have privileges to the database?
GRANT ALL PRIVILEGES ON special_db.* TO 'user'#'host'; FLUSH PRIVILEGES;
What is the output from the rake task? Have you tried running with the -t option? If so can you post the output?
Related
I've spend several hours figuring out how to get my database up and running. I created a new rails app and wanted to deploy it to heroku. I followed the instructions from heroku (to switch from sqlite3 -> postgresql) but it just doesn't work.
This is in my database.yml file:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
production:
<<: *default
database: myapp_production
url: <%= ENV['DATABASE_URL'] %>
I can't create or seed any data in the database. Sometimes it executes the db:migrate, but even then it doesn't create anything. This is what I get when running:
heroku run rake db:create
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
Does anyone has an idea on how to solve this? I don't have a clue anymore ...
Thanks!
By default you don't need to create a db on heroku.
Just run heroku run rails db:migrate and rest of the stuff will be handled by heroku itself.
Also your database.yml should be changed to following for Production env.
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
Also your rails app is by default assigned with a Postgres addon.
You can check this by running command heroku addons in the console.
Source -
Heroku Getting Started Guide
Heroku Postgres Addon Guide
You cannot create a database on Heroku using db:create (you cannot drop it neither). Your database is created when you add an add-on (such as Heroku Postgres). You can only migrate and seed. And if you want to start over, you can use pg:reset (instead of drop and create)
So the correct sequence should be:
add the Heroku add-on (such as Heroku Postgres). Add-ons are located here: https://elements.heroku.com/addons.
rake db:migrate
rake db:seed
if you want to start over
rake pg:reset
rake db:migrate
rake db:seed
From Heroku documentation: https://devcenter.heroku.com/articles/heroku-postgresql
The PostgreSQL user your database is assigned doesn’t have permission to create or drop databases. To drop and recreate your database use pg:reset.
As per the given stacktrace, it seems like you are trying to create a database on heroku which in turn is giving you Permission Denied Error.
Firstly, you do not need to run
heroku run rake db:create
Instead run
heroku run rake db:migrate
and it should migrate the migrations which are down.
For checking the current status of migrations run the following command:
heroku run rake db:migrate:status
Other Point you mentioned:
-> I can't create or seed any data in the database
As already mentioned above you can't create a database as heroku does it for you .
For seeding data in the database run the following command:
heroku run rake db:seed
I got a Ruby on Rails v5 application and I'm trying to run the following Rake task in development environment:
rake db:reset
Sadly, Rake runs the task in both development and test environment. Since I don't use the test environment, I don't wanna double my database. I have read somewhere that Rake run task in both environment when there is no RAILS_ENV defined. So I try to add the following line to my .bash_profile without any success:
export RAILS_ENV="development"
I also tried to add RAILS_ENV=development at the end of my task but it also did not worked.
Is there a way to use Rake in development only ?
Update #1
Thanks to Taryn East for the quick comment. I'm trying to update my post as quick as possible to make it easy for you to answer efficiently.
What do you actually observe when you run that command?
The command are simply executed twice. Since I have different database for my development and test environment, it does not show me any error at the moment. On the other hand, it does force me to use two database for no reason at all. I also tried to set the same database for the two environments, but then I was getting error since Rake tried to run the task twice on the database.
eg of output for rake db:drop
Dropped database 'db'
Database 'db' does not exist
It did drop my database, but as you can see Rake tried to run the command another time for the other environment.
Why aren't you using the test environment?
Because I don't see any advantage of it for my current project. I don't have time to create and update tests. I'm also not used to use both development and test environment at the same time.
If you want to run a rake task in just a single environment, you can also add RAILS_ENV to the command line.
As I said in my post, it does not work. Here's what I did:
RAILS_ENV=development rake db:drop
And here's the output:
Dropped database 'db'
Database 'db' does not exist
Update #2
Here's my database.yml configuration:
default: &default
adapter:mysql2
pool: 5
timeout: 5000
development:
<<: *default
database: db
username: something
password: something
host: localhost
port: 3306
test:
<<: *default
database: db_test
username: something
password: something
host: localhost
port: 3306
I don't know if it can help, but my environment is development according to the rake about task.
I have read somewhere that Rake run task in both environment when
there is no RAILS_ENV defined.
This is not correct. If the environment is development, test is also added to current environments. See: https://github.com/rails/rails/blob/f507085fa2a7625324e6f4e3ecef9b27dabefb4f/activerecord/lib/active_record/tasks/database_tasks.rb#L339
So this is expected behaviour.
On the other hand what you're saying will be true for any other env. E.g. RAILS_ENV=test rails db:drop will only execute for test env.
I am trying to create a database using rake db:create task.
When I am running the command bundle exec rake db:create to create the database and load the schema, I get the following error:
rake aborted!
Mysql2::Error: Unknown database 'xxx_development'
My database.yml:
development:
adapter: mysql2
host: localhost
reconnect: true
username: user
password: password
pool: 50
database: xxx_development
I checked that mysql server is running and I am able to connect to it using the password/username I have in the database.yml
I also understand that I can go ahead and create the database in mysql and then run the bundle exec rake db:create but isn't the rake task db:create also creates a database in case it doesn't exists?
The rails version is 3.2.22.
and mysql2 version is 2.9.13.
Any pointers will be highly appreciated.
If you are using ohmyzsh with bundler plugin which makes rake run by default with bundle exec, you can use
unbundled_rake db:create
Ran into a similar issue on Rails 5 and the following steps helped me fix it:
Spring stop
./bin/rails db:create
I have two ideas.
Check the User permissions
When I run rake tasks, I don't use bundle exec (i.e just rake db:create)
Hope this is helpful
Try rake db:create RAILS_ENV=development hope this will work
For some reason rails db:create failed but my workaround was
to create dbs directly in mysql
sudo mysql
create database myapp_development;
create database myapp_test;
After that run as normal
rake db:migrate db:seed
I have my config/database.yml like this:
development:
adapter: postgresql
database: psql_dev
username: postgres
min_messages: WARNING
test:
adapter: sqlite3
database: db/test.sqlite3
min_messages: WARNING
When I run rake test:units, it reports an error:
rake aborted!
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Why didn't it connect to my test DB(db/test.sqlite3).
and, If I run the test like this rake test RAILS_ENV=test, it works well.
Isn't RAILS_ENV=test the default setting for rake test?
I'm running rails 2.3.5 with ruby 1.8.7, and my $RAILS_ENV is not defined in my shell.
What's happening is that rake test depends on rake db:test:prepare which will attempt to load the current schema from the development database. That's how the test database gets updated when a migration is run on the development database
do you have a test:units rake task? Run:
rake test
does that work? Also can you paste the output of:
rake -T | grep tests
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.
To clarify here - the problem is that the client MySQL library does not work currently with Rails 2.3.
To fix this:
Download an older version of the client library here
Drop this file in your ruby bin directory
Restart your MySQL service
[1]: [1]: http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll "here"
I'm not answering your question per se, but since you're just learning, why not stick to the simpler sqlite for now?
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.
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.