I started using Rails 2 last April but stopped this June because I thought learning it when Rails 3 was released would be more practical since a lot of it was completely refactored and restructured. I used to work with Ubuntu 10.04 (with SQLite3 as the default db) but now I'm using Windows 7 and MySQL 5. I already installed the gem adapter for MySQL, but to use it I still need to tweak database.yml. Thanks.
In terms of database configuration, nothing much has really changed between Rails 2 and 3 with the exception of how you load your MySQL driver. This used to be done in config/environment.rb but is now done in Gemfile:
gem 'mysql'
The default config/database.yml file is set up with SQLite, but you can easily change this over to be MySQL. A generic version looks like:
defaults: &defaults
adapter: mysql
username: localdev
password: mylocaldevpasswordwhateveritis
host: localhost
development:
<<: *defaults
database: project_dev
test:
<<: *defaults
database: project_test
It's the adapter declaration line that sets what driver to use.
In tadman's answer, use gem 'mysql2' for the rails 3 since rails 3 now uses the new mysql adapter !!
You can change rails to default to MySql when you generate a new application, but you have to edit a line in your rails installation. You'll have to make the change to every version, and every time you update the rails gem.
I use Ruby-Enterprise. So here's what I do:
In file (where 1.8 is the ruby version and 3.0.4 is the rails version):
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/railties-3.0.4/lib/rails/generators/rails/app/app_generator.rb
Edit: In rails-3.1.0-rc1 the file is:
gems/railties-3.1.0.rc1/lib/rails/generators/app_base.rb
Search for this line:
class_option :database, :type => :string, :aliases => "-d", :default => "sqlite3",
Change "sqlite3" to "mysql".
class_option :database, :type => :string, :aliases => "-d", :default => "mysql",
So instead of doing:
rails new application_name -d mysql
I can just do (and the database.yml and Gemfiles are configured for the mysql2 gem):
rails new application_name
This assumes you have the correct mysql2 gem installed already. Also, I've only been doing this since Rails 3 came out. It's probably similar for previous versions. Again, every time you update Rails, you'll have to find and edit that file.
Since Rails 3.2 you can define a .railsrc file with custom command line options that will always apply to rails new
So, if you create a file called .railsrc and put it in your home directory w/ the contents like this -d mysql it wll make mysql be your default database. You can put any of the command line options in there (including application templates which are supper awesome!)
Run rails new --help from the command line to see all your options.
Related
I'm trying to import ruby on rails project to my computer but am running into all sorts of problems with the database.
Here's the situation:
I have an appname.tar.gz that I got from another developer. I extract it and relocated it to my user directory to work with it.
Next I run bundle install in the directory to install gem
dependencies.
Then I run rake db:create to create the database and
load the schema and structure from the DB folder in the same directory. This is where I'm running into all sorts of issues. When I launch the app I get an error saying DB migration pending.
structure.sql is a MySQL dump 10.13
while the database.yml file had the adapter set to postgres: adapter: postgres. Is this normal?
Is this the best way to import an existing app into your environment?
Any help will be appreciated.
To me this sounds like you got a MySQL dump and the application is configured to use postgres. I am wonderning why this is but you can try using the mysql adapter in your config/database.yml file by setting something like
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
host: localhost
development:
<<: *default
database: foobar_development
The interesting bit is adapter: mysql2.
I have taken this from a brand new rails application. You need mysql running for sure.
Hope this helps. If not you have to reach out to the other person to get some postgresql compatible dump.
I got it to work! Thank you all for your help! Really appreciated it.
Below are the steps that worked for me:
Copy the project to the new computer
Open terminal and change directory to the project
Run bundle install to install all the gem dependencies
Run bundle update to update installed gems
Start the Postgres server by running pg_ctl -D /usr/local/var/postgres start
Run createdb databasename to create the database on the new computer. The database name has to match the database mentioned in the database.yml file in the config folder.
Run rake db:schema:load and that's it!
After following the above steps I was able to run rails s to check out the website.
In my rails app I have this configuration for database
adapter: mysql2
host: *****
username: *****
password: <%= ENV['MYSQL_PW'] %>
database: *****
encoding: utf8
timeout: 5000
pool: 5
It is working perfectly in the server. But recently there was a bug and I tried to access rails console, but I get this error
Access denied for user '****' (using password: NO) (Mysql2::Error).
I also I tried to run migration and I get same error again. I don't understand what is the problem here. How can I solve this?
Also how can I check if ENV['MYSQL_PW'] is set in the unix environment variable?
Here is my log
$ rake db:migrate
DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to `config.serve_static_files` to clarify its role (it merely enables serving everything in the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in <top (required)> at )
DEPRECATION WARNING: You did not specify a `log_level` in `production.rb`. Currently, the default value for `log_level` is `:info` for the production environment and `:debug` in all other environments. In Rails 5 the default value will be unified to `:debug` across all environments. To preserve the current setting, add the following line to your `production.rb`:
config.log_level = :info
. (called from block in tsort_each )
rake aborted!
Mysql2::Error: Access denied for user '****' (using password: NO)
Don't forget to specify RAILS_ENV when you are running your commands.
By default rails assumes the environment is development, but as I see here you want to run those on production.
Simply do
RAILS_ENV=production rake db:migrate
RAILS_ENV=production rails c
# or alternatively
rails c -e production
http://guides.rubyonrails.org/command_line.html
Try the following:
Edit database.yml password to be a string and not an ENV variable, to identify the problem.
To test the ENV Variable, log in the rails console with rails c and input ENV['YourVariable'] to see if it is set
You can solve this problem by sourcing your bash_profile file if you have Unix system (linux/ubuntu/mac) in the terminal input source ~/.bash_profile
If you have Rails 4 upon you should run the following terminal command spring stop
In database.yml you should include the ENV Variable with the following syntax, beacuse yml needs the .erb syntax. <%= ENV['YOUR VARIABLE'] %>
Failing to access environment variables within `database.yml` file
The ENV Variable is Case Sensitive
I had this problems and I was able to solve it, but I keep having them. For this reason I read several discussions:
Rails 4.1 environment variables not reloading
Sorry if I was not able to help more
Fabrizio Bertoglio
as i explained i want to use different databases based on the user.
I got a partner table created with devise and i have multiple databases that i created.Everyone of those databases have the same schema and same relationships.
What i want to accomplish is something like
default: &default
adapter: mysql2
pool: 5
timeout: 5000
development:
<<: *default
database: db_ <%= current_partners%>
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db_ <%= current_partners%>
production:
<<: *default
database: db_ <%= current_partners%>
why can't i use this statement ??
Btw i m using mysql2 as seen in the code fragment.
In your database.yml, just mention default database configuration. Whenever, you want to switch between databases, call user defined switch_database method to connect to another database before making hit to corresponding database. Something like:
def switch_database
establish_connection (
:adapter => "mysql2",
:host => "another_host_name",
:username => "username",
:password => "password",
:database => "db_#{current_partner}"
)
end
Place above method in appropriate file as per your convenient to call it.
database.yml is loaded on application boot, variables like current_partners are not available at this point as they are request specific. You need to create all the databases within your database.yml and then tell rails which one to use in your controller. This however may cause a lot of concurrency issues (not sure though, never actually did this - see below).
Note however that this is not considered the best practice. Why not to have partners models and then have an associations instead?
You might want to checkout these gems which can help you achieve multi-tenancy in your Rails application here.
Apartment is one of quite active and Rack based database multi-tenancy ruby gems. I hope it helps.
I want to use mysql with a rails app (I have never used mysql before). My experience has been solely with sqlite3 and postgresql, which are really easy to use.
I am now creating a new app to learn to use mysql. I installed mysql with homebrew (brew install mysql), and created a new rails app that uses mysql rather than sqlite3. I have included the right gem in my gemfile (gem 'mysql2', '~> 0.2.6').
However, I don't know how to proceed. I have not set up anything more in mysql other than to install it on my system, I don't understand anything to do with how you set it up to run, where it stores the database for my app, and so on.
Please could anyone let me know either the next steps, or a tutorial that would get me understanding enough to develop my app to function just as it would with a simpler (sqlite) database system?
I believe the next step would be create the database in "your system" and let Rails know that you want to use that database.
If you want to create the database using the MySQL server directly, you want to log in by using this command from your console:
mysql -u root
By default, mysql root user doesn't need a password, otherwise you would specify it with -p, so it would be:
mysql -u root -p
Once you are logged in your MySQL server, you want to create the database by doing this command:
CREATE DATABASE my_project_database;
You might want to look deeper into users and permissions in MySQL, but this is just a starting point :).
If you want to avoid going into the MySQL server and do it the Rails way, you can create the database using rake. You want to use this command: rake db:create. This will do the same that we did before, but notice that before doing that command you need to create the database.yml file. So let's do that:
You want to have a database.yml file like this:
development:
host: localhost
adapter: mysql2
database: my_project_database
username: root (notice that you might want to change this user later)
password:
I suppose that next step would be to create the Migrations that would generate the tables you want to use and so on.
One of the things that I love the most about Rails is its ORM called Active Record. It will abstract all database operations, so you don't have to worry either if you are calling a MySQL or a SQLite...
I am having trouble in rails, I have just installed it but when I update after updating mysql settings and running
rake db:create
and then
rails server
It started server and then when I tried viewing it via browser there errors saying active record connection not established error in strange way. I am new to both ruby and rails so that's why not understanding by debugging info. I assume that there is some thing wrong in MySQL configuration. I am using it on windows and using railsinstaller and using MySQL that came with XAMPP.
So can anyone tell that what is wrong with it and how can it be solved? Or is it better to use Linux for RoR? I do many things on windows thats why if there will be some solution at windows then that would be helpeful.
thanks for your time, following is attached output image.
I also observed that rake db:create command is not creating db, I had to do this manually. Following is my configurations for db:
adapter:mysql2
host:localhost
encoding:utf8
database:kaasib_new
pool:5
username:root
password:~
So is this fine? I don't have password on local machine db and do I need to mention 3306 for mysql in it?
A couple of things to try:
If the tilde character in the password field above is a typo, ok,
but there shouldn't be anything there.
Not sure if it's a function
of posting here, but Whitespace matters in YAML files. It should be
set up with indents like below (socket is optional):
.
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: app_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
Open Gemfile from your project.
add line => gem 'mysql2'
run command => bundle update
restart your server.