Rails connects to MySQL from WEBrick but not from Passenger - mysql

I just created a new Rails 4 app with MySql as follows:
rails new mysqltest -d mysql
And modified the database.yml with the right credentials.
I generated a sample contoller and updated the routes for root route.
When I start using WEBrick in production,
rails s -e production
The site works. I see the index page.
When I start using Passenger without 3000 port, I see the following error:
database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
Passsenger is running in Production environment.
My database.yml
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MYSQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
database: sample
pool: 5
username: sample
password: sample
socket: /var/run/mysqld/mysqld.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: sample
pool: 5
username: sample
password: sample
socket: /var/run/mysqld/mysqld.sock
production:
adapter: mysql2
encoding: utf8
database: sample
pool: 5
username: sample
password: sample
socket: /var/run/mysqld/mysqld.sock

The case I used in database.yml file and the apache config file were different.
Both should be same. eg. production.
In apache config, I gave as Production. After changing it to production it worked.
Source - https://groups.google.com/forum/#!topic/phusion-passenger/Kr-R0gSw6i8

Did you create your local production database?
rake db:create:all
RAILS_ENV=production bundle exec rake db:migrate
Is your database.yml file setup properly to use a production database?
Do you have a mysql gem in your gem file.
Common problems.

Related

Rails 4 connect to remote MySQL database and pull Database structure for easy access

In my team, we are developing a system that is accessed by different platforms such as a Ruby on Rails website, a desktop java application and Android as well as iOS apps.
Our central MySQL database is running remotely on a server and can be accessed through PHPMyAdmin (and ControlPanel).
While the connection between the other platforms works well, I struggle with connecting my Rails app to the database. I would like to copy the database structure (tables, indexes, columns) so that I can access the database within Rails just as I would access data from a model that has been created locally (e.g. Customer.find(name: "Florian") ).
Is there a way to accomplish this? I tried several things such as altering my database.yml file, but when I then run something, like for example rails c it shows:
/Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/mysql2-0.4.2/lib/mysql2.rb:31:in `require': dlopen(/Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-15/2.2.0-static/mysql2-0.4.2/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.18.dylib
Referenced from: /Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-15/2.2.0-static/mysql2-0.4.2/mysql2/mysql2.bundle
Reason: image not found - /Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-15/2.2.0-static/mysql2-0.4.2/mysql2/mysql2.bundle (LoadError)
...
My `database.yml' file:
development:
adapter: mysql2
encoding: utf8
host: <host-IP>
username: <username>
password: <password>
port: <port>
database: <database>
pool: 5
timeout: 5000
The same block is under test and production as well. In my Gemfile I have included:
gem 'mysql2'
I'm running Mac OS X 10.11.2 El Capitan and the server is a linux system. My Rails version is Rails 4.2.4 and Ruby ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin15].
Thank you!
EDIT:
Thank you all, finally fixed my database.yml file and running rake db:schema:dump did it. But how do I convert my schema.rb to locally usable ActiveRecord::Models ?
Use this in your Gemfile
gem 'mysql2', '~> 0.3.18'
config/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
socket: /var/run/mysqld/mysqld.sock
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
insert into your .bashrc
export DATABASE_USER='root'
export DATABASE_PASSWORD='123'
Add library path to your .bash_profile file:
MYSQL=/usr/local/mysql/bin
export PATH=$PATH:$MYSQL
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
The problem not in connection but with loading mysql itself.

Need to switch from SQLite3 to MySQL, and want to deploy with MySQL

We are currently using SQLite3 as our database and want to switch it to MySQL before we send our Rails app live via Passenger and Nginx to our Linode Ubuntu 10.04 Lucid box.
we set up the msql server on our linode ubuntu 10.04 box
and set a password for the root user. how do we configure database.yml ? what are the steps to set up this MySQL database? We have the mysql2 gem installed and in our Gemfile.
What does database.yml need to look like in order to deploy with MySQL ?
Does the database need any specific name as in database name?
Do we go about running rake db:create on our machine or run it on our Ubuntu box?
current database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 2000
# 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: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 2000
production:
adapter: mysql2
encoding: utf8
database: production
pool: 5
username: root
password: "mysql password"
Did you try a simple connection test? If not, here is a script to test the connection to your db.
#! /usr/bin/ruby
# getting required gems
require 'rubygems'
require 'mysql'
## ----- test the connection -----
# http://rubydoc.info/gems/mysql/2.8.1/frames
# real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
mysql = Mysql.connect('localhost', 'root', 'password', 'db_name', 3006, '', '')
## ----- if connected, will list databases -----
puts mysql.list_dbs().to_s
In case the 'mysql' did not install properly, you will have to custom configure the install properties. On my Mac (different that you linux machine), I referenced this blog post to get something like this,
sudo env ARCHFLAGS="-arch x86_64" gem install --no-rdoc --no-ri mysql -- --with mysqlconfig=/usr/local/mysql/bin/mysql_config
UPDATE - Had to reinstall mysql gem for a different ruby version, and removing the architecture notation allowed me to install the gem.
sudo gem install --no-rdoc --no-ri mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
On your production server, go to your app's root directory and type nano config/database.yml and paste this code in. Change the names.
production:
adapter: mysql
database: something_production
username: somethingotherthanroot
password: passwordnostring
host: 127.0.0.1
Now, either create the database on your own, on the server, or you can run rake RAILS_ENV=production db:create:allhell i've never tried but i'm sure you can do rake db:create:production too.
once done, just do rake RAILS_ENV=production db:migrate
Looks like your database.yml is fine, except for the fact that you should have your production name database named like this: projectname_production
Answering your question of how to create the database once on the server, ssh into your application server and do a rake db:create RAILS_ENV=production
This will create the production database on the server. Same thing with your migration. It should be rake db:migrate RAILS_ENV=production. This will start the migration for your production database.

mysql error help... while running rake db:setup RAILS_ENV="production"

I am diligently trying to get mysql up and running for my first rails app ever.
I keep getting the following error when running rake db:setup RAILS_ENV="production":
rake aborted!
dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dyl Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2/mysql2.bundle
Reason: image not found - /Library/Ruby/Gems/1.8/gems/mysql2-0.3.2/lib/mysql2 /mysql2.bundle
/Users/chris/rails_projects/sienab/Rakefile:4
(See full trace by running task with --trace)
I am running snow leopard, mysql 5.5, gem mysql2, rails 3.
Any help is great. many thanks.
database.yml below
# SQLite version 3.x
# gem install sqlite3
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: sienab_production
pool: 5
username: username
password: password
host: localhost
Looks like you need to install the mysql2 libs:
sudo apt-get install libmysqlclient-dev libmysqlclient16
Also, I suggest you use gem 'mysql2','0.2.7' in your Gemfile if you're using Rails 3.0.x.
Don't forget to run bundle install after making this change.

Cannot access mysql on ruby on rails

I am trying get my first simple project in rails to run. I have installed wamp. And trying to make use of that same mysql db server installed with wamp. But I decided to make use of webrick, and not apache. Hoping that the configuration would be easier.
I edited the database.yml file and the gemfile under my projects directory:
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql
encoding: utf8
reconnect: false
database: rubybeg_test
pool: 5
username: root
password: 1234
host: localhost
# 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: mysql
encoding: utf8
reconnect: false
database: rubybeg_test
pool: 5
username: root
password: 1234
host: localhost
production:
adapter: mysql
encoding: utf8
reconnect: false
database: rubybeg_test
pool: 5
username: root
password: 1234
host: localhost
And here's gemfile:
source 'http://rubygems.org'
gem 'rails', '3.0.5'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'mysql'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'
# gem 'ruby-debug19', :require => 'ruby-debug'
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
# group :development, :test do
# gem 'webrat'
# end
I used the command gem install mysql because mysql2 doesn't work for me.
What I did was to launch wampserver and stopped the apache service. It would take forever to load:
http://localhost:3000/rails/info/properties
Then webrick would crash. But if I do not launch wamp, I get this:
Did you configure mysql to run on a port other than 3306? If you're using a different port than that, you'll need to edit as follows:
development:
adapter: mysql
encoding: utf8
reconnect: false
database: rubybeg_test
pool: 5
username: root
password: 1234
host: localhost
port: 3306 #replace with the correct port
If that doesn't work, also try changing localhost to 127.0.0.1 in order to force it to use TCP. Sometimes it tries to use sockets and cannot find the socket file.

Create a new Ruby on Rails application using MySQL instead of SQLite

I want to create my Rails application with MySQL, because I like it so much. How can I do that in the latest version of Rails instead of the default SQLite?
Normally, you would create a new Rails app using
rails ProjectName
To use MySQL, use
rails new ProjectName -d mysql
If you already have a rails project, change the adapter in the config/database.yml file to mysql and make sure you specify a valid username and password, and optionally, a socket:
development:
adapter: mysql2
database: db_name_dev
username: koploper
password:
host: localhost
socket: /tmp/mysql.sock
Next, make sure you edit your Gemfile to include the mysql2 or activerecord-jdbcmysql-adapter (if using jruby).
For Rails 3 you can use this command to create a new project using mysql:
$ rails new projectname -d mysql
Go to the terminal and write:
rails new <project_name> -d mysql
If you have not created your app yet, just go to cmd(for windows) or terminal(for linux/unix) and type the following command to create a rails application with mysql database:
$rails new <your_app_name> -d mysql
It works for anything above rails version 3. If you have already created your app, then you can do one of the 2 following things:
Create a another_name app with mysql database, go to cd another_name/config/ and copy the database.yml file from this new app. Paste it into the database.yml of your_app_name app. But ensure to change the database names and set username/password of your database accordingly in the database.yml file after doing so.
OR
Go to cd your_app_name/config/ and open database.yml. Rename as following:
development:
adapter: mysql2
database: db_name_name
username: root
password:
host: localhost
socket: /tmp/mysql.sock
Moreover, remove gem 'sqlite3' from your Gemfile and add the gem 'mysql2'
If you are using rails 3 or greater version
rails new your_project_name -d mysql
if you have earlier version
rails new -d mysql your_project_name
So before you create your project you need to find the rails version. that you can find by
rails -v
rails -d mysql ProjectName
rails new <project_name> -d mysql
OR
rails new projectname
Changes in config/database.yml
development:
adapter: mysql2
database: db_name_name
username: root
password:
host: localhost
socket: /tmp/mysql.sock
Create application with -d option
rails new AppName -d mysql
$ rails --help
is always your best friend
usage:
$ rails new APP_PATH[options]
also note that options should be given after the application name
rails and mysql
$ rails new project_name -d mysql
rails and postgresql
$ rails new project_name -d postgresql
You should use the switch -D instead of -d because it will generate two apps and mysql with no documentation folders.
rails -D mysql project_name (less than version 3)
rails new project_name -D mysql (version 3 and up)
Alternatively you just use the --database option.
In Rails 3, you could do
$rails new projectname --database=mysql
If you are creating a new rails application you can set the database using the -d switch like this:
rails -d mysql myapp
Its always easy to switch your database later though, and using sqlite really is easier if you are developing on a Mac.
Just go to rails console and type:
rails new YOURAPPNAME -d mysql
On new project, easy peasy:
rails new your_new_project_name -d mysql
On existing project, definitely trickier. This has given me a number of issues on existing rails projects. This kind of works with me:
# On Gemfile:
gem 'mysql2', '>= 0.3.18', '< 0.5' # copied from a new project for rails 5.1 :)
gem 'activerecord-mysql-adapter' # needed for mysql..
# On Dockerfile or on CLI:
sudo apt-get install -y mysql-client libmysqlclient-dev
First make sure that mysql gem is installed, if not? than type following command in your console
gem install mysql2
Than create new rails app and set mysql database as default database by typing following command in your console
rails new app-name -d mysql
Use following command to create new app for API with mysql database
rails new <appname> --api -d mysql
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
database.yml
# MySQL. Versions 5.1.10 and up are supported.
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html
#
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: localhost
database: database_name
username: username
password: secret
development:
<<: *default
# 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
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass#localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
Gemfile:
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
you first should make sure that MySQL driver is on your system if not run this on your terminal if you are using Ubuntu or any Debian distro
sudo apt-get install mysql-client libmysqlclient-dev
and add this to your Gemfile
gem 'mysql2', '~> 0.3.16'
then run in your root directory of the project
bundle install
after that you can add the mysql config to config/database.yml as the previous answers