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.
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
I'm a new Rails Developer, and I'm working on a legacy Rails app. Whenever I run the rake db:create command, I get an error that the database couldn't be created. I have found many StackOverflow questions related to this, but in troubleshooting nearly all permutations of solutions, I couldn't resolve the issue.
I created the three Dbs (dev, prod, test), created the user with all access privileges to these dbs, and ran rake db:create.
I'm running Mac OS X Lion, MySQL 5.5.28, Rails 2.3.5, Ruby 1.8.7. Here are my settings
development:
adapter: mysql
encoding: utf8
database: adva_development
username: adva
password: ****
host: localhost
socket: /tmp/mysql.sock
Here's the error:
Couldn't create database for {"adapter"=>"mysql", "username"=>"adva", "host"=>"localhost", "encoding"=>"utf8", "database"=>"adva_development", "socket"=>"/tmp/mysql.sock", "password"=>"****"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)
I have done the following troubleshooting:
Verified user and password are correct, and the user has access to the DB. (Double checked user access with SELECT * FROM mysql.db WHERE Db = 'adva_development' \G; User has all privileges.)
Verify the socket is correct. I don't really understand sockets, but I can plainly see it at /tmp/mysql.sock.
Checked collation and character set. I found out I had created the DB in latin charset and collation, so I recreated them. I ran show variables like "collation_database"; and show variables like "character_set_database"; and came back with utf8 and utf8_unicode_ci respectively.
I followed the instructions in this question. After uninstalling mysql gem, I ran the following but came up with the same error:
gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/usr/local/mysql-5.5.28-osx10.6-x86_64/bin --with-mysql-config=/usr/local/mysql-5.5.28-osx10.6-x86_64/bin/mysql_config
Following Matt's suggestion, here's what a rake --trace db:create reveals:
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:create
Couldn't create database for {"database"=>"adva_development", "adapter"=>"mysql", "host"=>"127.0.0.1", "password"=>"woof2adva", "username"=>"adva", "encoding"=>"utf8"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)
After 3 days and six or seven hours, I have pretty much run out of options. I tried various random things, like replacing localhost with 127.0.0.1 to no avail.
Could there be something wrong related to my specific environment? Mac OS X Lion + MySQL 5.5.28? I plan on trying on setting up everything in a Linux environment.
Thanks!
You can try changing localhost to 127.0.0.1 and removing socket to use TCP/IP instead of a local socket.
Or try to verify if your database server uses the same socket as Rails:
mysqladmin variables | grep socket
If this doesn't help, then try to connect to the database server with pure Ruby and create a new database:
require 'rubygems'
require 'mysql'
begin
db = Mysql.new('localhost', 'root', 'yourpassword')
db.query('create database sample_database;');
rescue Mysql::Error => e
puts e
ensure
db.close unless db.nil?
end
and see if this runs. You should have mysql gem installed (gem install mysql)
Make sure you running a more recent version of the mysql gem. I ran into this problem with version 2.8. Upgrading to 2.9 fixed the issue.
change 'localhost' into '127.0.0.1' (loop back address) , hope its works
I'm on a Mac and used brew to install an updated MySQL. In an effort to streamline my local database creation, I took the password off my local root user (I know, I know), and after creating a new Rails app (rails new myapp -d mysql), should just be able to run rake db:create, right?
However, despite having /etc/my.cnf set to define the standard mysql.sock location...
[mysqld]
socket=/usr/local/var/mysql/mysql.sock
[client]
socket=/usr/local/var/mysql/mysql.sock
... Rails still won't use the newly defined mysql.sock file on a rake db:create:
rake db:create
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "reconnect"=>false, "database"=>"delayed_jobs_development", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost"}, charset: , collation:
Running mysqladmin variables lists the correct socket file location, as does mysql --help. I've restarted the MySQL server and even recreated the Rails app.
So my question: What else could be defaulting the socket file to /tmp/mysql.sock, or is there an alternate, preferably global, config file where I could specify its location that the rake task will honor? Could the rake task be calling a different commandline MySQL tool that uses a different config file?
Obviously, I could either create a symlink from the /tmp location to my real location, or edit my database.yml file to refer to it. I understand how to get Rails to talk to the DB server correctly, but the point is to have the proper defaults set up once, so any future Rails app I create locally is good to go without extra edits (/tmp/mysql.sock gets cleared on reboot).
In fact, I'm not even sure why it's trying to connect via the socket file at all, since my database.yml file tells it to use the hostname:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: myapp_development
pool: 5
username: root
password:
host: localhost
As the MySQL documentation says, /tmp/mysql.sock is the default location for the socket file. This is also what rails defaults to (more below).
As you mentioned, you can always set the socket (the one you configured and that is returned by mysqladmin variables) explicitly in your database.yml file:
socket: /usr/local/var/mysql/mysql.sock
I think the connection is made via the socket file as you set host: localhost. If you change that to 127.0.0.1, the connection should by made over TCP/IP.
I found rather old information (a really old version of the mysql adapter and a blog entry from 2007) which implies that the MYSQL_UNIX_ADDR environment variable is used in resolving the socket. Also see this comment on GitHub by the developer of Ruby/MySQL, in which he says to set the MYSQL_UNIX_PORT variable instead. A third env variable is MYSQL_SOCKET mentioned in the source of the Ruby/MySQL connector.
export MYSQL_UNIX_ADDR=/usr/local/var/mysql/mysql.sock
export MYSQL_UNIX_PORT=/usr/local/var/mysql/mysql.sock
export MYSQL_SOCKET=/usr/local/var/mysql/mysql.sock
I do not know if these variables get evaluated somewhere along the way. Still, you could try them out.
Right now, rails mysql_adapter.rb says that the configured socket is used and that it defaults to /tmp/mysql.sock (set in app_generator.rb). No other locations seem to get checked. I did not found anything else in the default Ruby/MySQL adapter used by rails.
So basically – without being an expert on this topic – I don't think there is a way to set a global default location for the MySQL socket file that gets evaluated by rails or the used default adapter.
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.