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.
Related
I have a live app on Heroku, and I'm attempting to build a database for my staging app using the following command:
heroku run rake db:schema:load --remote staging
The first roadblock I hit is this error:
ActiveRecord::NoEnvironmentInSchemaError:
Environment data not found in the schema. To resolve this issue, run:
bin/rails db:environment:set RAILS_ENV=production
After running this command, I get a second error:
rails aborted!
ActiveRecord::NoDatabaseError: FATAL: role "appname" does not exist
Not great at dealing with database issues. Any help would be appreciated.
This is a common thing you may have to provision a new database you can check the following link
https://devcenter.heroku.com/articles/postgres-logs-errors#fatal-role-role-name
But for locally you can do the following
sudo -u postgres createuser --superuser appname
or
createuser -P -d -e appname
I'm using Ubuntu version 15.10. After I type rails s in the terminal to start a Ruby app and navigate to 0.0.0.0:3000, I get the following error on the web page:
ActiveRecord::NoDatabaseError at /
FATAL: database "local-election_development" does not exist
Run $ bin/rake db:create db:migrate to create your database
When I type bin/rake db:create db:migrate from the terminal, I get this error:
PG::InsufficientPrivilege: ERROR: permission denied to create database
I found this answer, but I did not know how to execute the command ALTER USER new_user CREATEDB;.
How can I solve this error?
You can to enter the SQL command on the PostgreSQL console, which can be invoked with psql.
If it complains that the database does not exist, then use createdb, if the user doesn't exist, use createuser. Search for PostgreSQL-related questions if you have an issue with any of these.
Log into the postgres user:
su - postgres
Log into by typing:
psql
You should now see a prompt for postgres=#
So I'm using Heroku Postgres in my Rails app, but I'm not hosting my app on Heroku itself. I used the Active Record connection details from Heroku in my database.yml, and it looks like this:
development:
adapter: postgresql
encoding: unicode
pool: 5
database: [database]
username: [username]
password: [password]
host: ec2-54-227-243-78.compute-1.amazonaws.com
port: 5432
However, now I'm trying to rake db:migrate my app so the database gets all set up with my models. Running that command doesn't do anything, so I tried rake db:reset and I get this:
Couldn't drop df2cokjfj0k4vu : #<PG::Error: FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
df2cokjfj0k4vu already exists
-- initialize_schema_migrations_table()
-> 1.3997s
-- assume_migrated_upto_version(20130924040351, ["/home/action/braindb/db/migrate"])
-> 0.0882s
Any idea what I'm doing wrong. I'm still pretty new to Rails so sometimes I forget how to get my Postgres database setup properly when migrating hosts.
Use heroku pg:reset DATABASE instead as noted in https://devcenter.heroku.com/articles/rake
You cannot drop databases in Heroku with rake db:reset because user has no privileges.
You can't drop your PG database on Heroku.
I recently had this problem and resolved it through the following steps.
heroku pg --help gets the name of commands for using postgres
heroku pg:reset DATABASE # resets the db
Answer the prompt to confirm
Like others I had a similar problem but running pg:reset did not help: that's where I ran into the user does not have CONNECT privilege issue.
1) heroku pg:reset you will be prompted to enter the project name to confirm.
2) From Heroku's docs run heroku rake db:schema:load
I had the same problem. I fixed it by running:
heroku pg:reset DATABASE
(note: must specify DATABASE as above)
If you got this because you were running rake db:create, instead, simply skip to the migration. For whatever reason, postgresql on heroku doesn't require the rake db:create step as may be required on local installations.
In other words, instead of
heroku run rake db:create db:migrate db:seed
run this instead
heroku run rake db:migrate db:seed
The solution to me was to just go straight to migration because the database is already created (don't go on "heroku run rails db:create")
heroku run rails db:migrate -a YOURAPPNAME
For one of my apps, upgrading to the first paid tier of a Heroku database seemed to work for me: https://devcenter.heroku.com/articles/upgrade-heroku-postgres-with-pgbackups#provision-new-plan
Reason was that 'heroku pg:info' revealed that I was over my row limit as shown here:
Rows: 12392/10000 (Write access revoked)
It's taken me two days to get this far. But I'm almost there. When I run cap deploy:cold the deployment fails with this message:
servers: ["xxx"]
[xxx] executing command
** [out :: xxx] rake aborted!
** [out :: xxx] FATAL: password authentication failed for user "my_app"
My database.yaml has this:
production:
adapter: postgresql
encoding: utf8
database: my_app_production
pool: 5
host: localhost
username: my_app
password: secret
I'm running cap deploy under a user I created called deployer. I installed Postgres under deployer. I also created the user my_app in psql:
create user my_app with password 'secret';
create database my_app_production owner my_app;
I verified that the user my_app exists by running \du. When I ssh through deployer#xxx and I run the command psql I get psql: FATAL: role "deployer" does not exist.
What am I doing wrong?
For the psql invocation to be similar as what rails does, it should be:
psql -h localhost -U my_app -d my_app_production
If you omit these options, psql will take by default:
the OS username as the database username
the OS username as the database name
a Unix socket directory as the host (which differs from localhost and generally has different authentication rules in pg_hba.conf)
I've gone through the very same Railscasts you are going through I think. I just got my site up with it, though I am still having a couple glitches with cap deploy. In any case, for clarity.
You created a deployer user on the remote host obviously. But, that doesn't mean "deployer" is a user in Postgres. Once you ssh as deployer#xxx.xxx.xxx.xxx you have to run sudo -u postgres psql to enter psql as the default postgres admin. Presuming you're using Ubuntu, there is no default password.
Where I'm confused is how, if you can't get in on the remote system with deployer, did you create the my_app user in psql there? It almost sounds like you did this locally (Vagrant maybe?) and didn't do it remotely. As I said, I think I just finished the same stuff you're looking at and it works if the my_app user is created on the remote box with the correct password.
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?