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.
Related
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
Im making an application with a pre-existing database, I set up the database.yml to use the database.
database.yml
development:
adapter: mysql2
encoding: utf8
# database: ttlem_demo_development
database: ttle
pool: 5
username: root
password:
socket: /tmp/mysql.sock
# 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:
adapter: mysql2
encoding: utf8
database: ttlem_demo_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql2
encoding: utf8
database: ttlem_demo_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock
I only want one table out of the database it is called account views, I try to generate a scaffold for this with all the correct fields but it tells me i need to migrate (when i render it in the browser), if i migrate i wont be able to use the existing data, is this correct? How can i make a model that will use the existing data and fields?
Thank you for all your help :)
Two things to try:...
#1 Run your scaffold without a migration so it doesn't think you're missing one.
rails g scaffold yourmodelname fieldone:fieldtype ... etc --no-migration
#2 If that doesn't work you can go the long way round but dumping and reloading with a valid schema version number
Add this db to yml to your gemfile:
gem 'yaml_db', github: 'jetthoughts/yaml_db', ref: 'fb4b6bd7e12de3cffa93e0a298a1e5253d7e92ba'
It works for either rails 3 or rails 4.
Do a schema dump of your current database so you'll get a schema.rb with a valid version number.
bundle exec rake db:schema:dump
Now that you have a valid schema dump your data.
bundle exec rake db:data:dump
Drop your database (you can do it manually using the mysql commands if you prefer or run rake db:drop)
Now recreate it with your schema file.
bundle exec rake db:schema:load
Now add back your data
bundle exec rake db:data:load
Start your server and assuming your correctly matched all your data fields in your model (so the proper strong parameters are set from your scaffold) you should be good.
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.
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.