db:migrate does not create tables - mysql

Working with:
Ruby 1.9.3
Rails 4
MySQL 5.6
Whenever I run "rake db:migrate" no tables are created in the database.
I have created a database and named it "simple_cms_development" and changed the development part of "database.yml" accordingly:
development:
adapter: sqlite3
database: simple_cms_development
pool: 5
username: simple_cms
password: ruby
timeout: 5000
I generated and model named "User" and edited "create_users.rb" to:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string "first_name", :limit => 25
t.string "last_name", :limit => 50
t.string "email", :default => "", :null => false
t.string "password", :limit => 40
t.timestamps
end
end
end
I run "rake db:migrate" and there is no change to the tables within the database. When I run SHOW TABLES; in MySQL I get back "Empty set (0.00sec)". I don't even get "schema_migrations" table.
Any ideas what is going on?
Thanks!

Your database.yml is set up to use sqlite3, not mySQL. Yours is now this:
development:
adapter: sqlite3
database: simple_cms_development
pool: 5
username: simple_cms
password: ruby
timeout: 5000
You should probably be using the mySQL2 gem, and your database.yml should look something like this:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: simple_cms_development
pool: 5
username: root
password: your-password
socket: /var/run/mysqld/mysqld.sock
According to this StackOverflow discussion you can get your socket by running
mysqladmin variables | grep socket
or if you have a password on your root:
mysqladmin password-here variables | grep socket`
If you want to use host and port instead of socket try this:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: simple_cms_development
pool: 5
username: root
password: your-password
host: 127.0.0.1
port: 3306
Here is a blog with instructions on how to set up mySQL with Rails 3.2, it might help with your Rails 4 issue: http://cicolink.blogspot.com/2011/06/how-to-install-ruby-on-rails-3-with.html

Related

How to change the database in a Rails application

The database of my Rails application is SQLite3, but I want to change it to MySQL. What do I need to do to change it in my application?
This is database.yml from my application:
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
You need to update the adapter to MySQL2 and add your credentials. For instance:
development:
adapter: mysql2
encoding: utf8mb4
database: development
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password
socket: /tmp/mysql.sock
Look at the offical documentation for more information.
Here is it with defaults:
default: &default
adapter: mysql2
timeout: 5000
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
socket: /tmp/mysql.sock
development:
<<: *default
database: development
username: <%= ENV.fetch("username") %>
password: <%= ENV.fetch("password") %>
test:
<<: *default
database: test
username: username
password: password
production:
<<: *default
database: production
username: <%= ENV.fetch("username") %>
password: <%= ENV.fetch("password") %>
You can also encode username and password in the database like this:
mysql://<username>:<password>#<host>:<port>/<db_name>
and then set host. Don't put username and password in your database.yml file.

Deploy Rails Application on Heroku : ERROR

I am trying to execute heroku run rake db:migrate, but that's the result :
rake aborted!
PG::Error: ERROR: invalid value for parameter "client_encoding": "utf8mb4"
I want to use mysql database in development and postgres in production, and so that's the way that I configured my database.yml :
postgres: &postgres
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
mysql: &mysql
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
socket: /var/run/mysqld/mysqld.sock
development:
<<: *mysql
database: event_development
test:
<<: *mysql
database: event_test
production:
<<: *postgres
database: event_production
username: event
password: <%= ENV['EVENT_DATABASE_PASSWORD'] %>
How can I solve this?
Just use url for it. If you have installed "Heroku Postgres" add-on. There supposed to be the DATABASE_URL in config vars.
database.yml:
...
production:
url: <%= ENV['DATABASE_URL'] %>

Ruby on Rails server cannot connect to mysql

everyone.
I am trying to set up rails 3 project with mysql as database on macOS X. However, I receive the next error:
/usr/local/bundle/gems/activerecord-3.2.22.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:68:in connection_url_to_hash': undefined method `sub' for nil:NilClass (NoMethodError)
config/database.yml
default: &default
adapter: mysql2
pool: 5
encoding: utf8
min_messages: warning
timeout: 5000
username: root
password:
database: heart_development
host: localhost
Can anyone suggest how to solve this problem?
From the database.yml which you have shared and the logs posted it seems like it is unable to find the path where the database is located.
Change the database.yml to
default: &default
adapter: mysql2
pool: 5
encoding: utf8
min_messages: warning
timeout: 5000
username: root
password:
database: heart_development
host: localhost
Specified host for which the database has to connect to.
I commented environmental variable DATABASE_URL in my .env file. Also I configured my database.yml in the next way:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: heart_development
pool: 5
username: root
password:
host: localhost
port: 3306
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: heart_test
pool: 5
username: root
password:
host: localhost
port: 3306
production:
adapter: mysql2
encoding: utf8cm
reconnect: false
database: heart_production
pool: 5
username: root
password:
host: localhost
port: 3306

crontab shows the mysql error

I am creating the rails app with 'whenever' gem.
And I defined the function in the User model and I want to execute the function every 7am.
But my function is not executing correctly and it shows the error.
schedule.rb
set :output, 'log/crontab.log'
set :environment, :production
every 1.day, at: '7:00 am' do
runner 'User.create_group'
end
crontab.log
/Users/michel/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:89:in `connect': Access denied for user ''#'localhost' to database 'appnorth_production' (Mysql2::Error)
here is my database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
socket: /tmp/mysql.sock
development:
<<: *default
database: appnorth_development
test:
<<: *default
database: appnorth_test
production:
<<: *default
database: appnorth_production
username: appnorth
password: <%= ENV['APPNORTH_DATABASE_PASSWORD'] %>
Does anyone help me?
Please insert the following line to schedule.rb.
And, Could you try?
job_type :runner, "export :environment_variable=:environment && cd :path && bin/rails runner ':task' :output"

Correct MySQL configuration for Ruby on Rails Database.yml file

I have this configuration:
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: mysql://127.0.0.1:3306
And I am getting this error:
Unknown MySQL server host 'mysql://127.0.0.1:3306' (1)
Is there something obvious that I am doing incorrectly?
You should separate the host from the port number.
You could have something, like:
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: 127.0.0.1
port: 3306
You also can do like this:
default: &default
adapter: mysql2
encoding: utf8
username: root
password:
host: 127.0.0.1
port: 3306
development:
<<: *default
database: development_db_name
test:
<<: *default
database: test_db_name
production:
<<: *default
database: production_db_name
Use 'utf8mb4' as encoding to cover all unicode (including emojis)
default: &default
adapter: mysql2
encoding: utf8mb4
collation: utf8mb4_bin
username: <%= ENV.fetch("MYSQL_USERNAME") %>
password: <%= ENV.fetch("MYSQL_PASSWORD") %>
host: <%= ENV.fetch("MYSQL_HOST") %>
(Reference1)
(Reference2)
If you can have an empty config/database.yml file then define ENV['DATABASE_URL'] variable, then It will work
$ cat config/database.yml
 
$ echo $DATABASE_URL
mysql://root:my_password#127.0.0.1:3306/my_db_name
for Heroku:
heroku config:set DATABASE_URL='mysql://root:my_password#host.com/my_db_name'
If you have multiple databases for testing and development this might help
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: DBNAME
pool: 5
username: usr
password: paswd
shost: localhost
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: DBNAME
pool: 5
username: usr
password: paswd
shost: localhost
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: DBNAME
pool: 5
username: usr
password: paswd
shost: localhost
None of these anwers worked for me, I found Werner Bihl's answer that fixed the problem.
Getting "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'" error when setting up mysql database for Ruby on Rails app