Rails make database mysql - mysql

I'm using Rails 3, and Ubuntu. I want to change the default Rails SQLite3 into a mysql database. I ran these commands:
sudo apt-get install mysql-server mysql-client libmysqlclient15-dev
sudo apt-get install libmysql-ruby1.8
These were successful. Then I did this in the gemfile
#gem 'sqlite3'
gem 'mysql'
This worked, now, what else should I do to complete the MySQL setup. I know for one the database.yml needs to change, but how does it need to look for Ubuntu and how do I fill in the parameters?

Your config/database.yml must look like :
base: &base
adapter: mysql
username: YourUserName
password: YourPassword
host: localhost
encoding: utf8
pool: 5
timeout: 5000
development:
database: YourApp_dev
<<: *base
test:
database: YourApp_test
<<: *base
production:
database: YourApp_prod
<<: *base

Is the database on the same host?
You need to log into mysql, set a user, create a database. If the database is on a database server, outside of the application server, then you will need to make accessible from the net.
http://www.slac.stanford.edu/BFROOT/www/Computing/Online/Databases/CfgDB/CfgDB-MySQL-Setup.html
After you're done setting up the database (not just installing it), then you're database.yml files need to be modified per the above.

Related

Transition from mysql to postgress error

I wanted to change from mysql to postgresql.
I don't care about data so I have changed database.yml:
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.postgresql
test:
<<: *default
database: db/test.postgresql
production:
<<: *default
database: db/production.postgresql
And run
rails db:reset db:migrate
But I get error
rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I have restarted both IDE (using c9) and server, but it gave no effect.
Any idea how to solve that problem?
#Edit After running sudo service postgresql start servers starts, and then when I run rails db:create
I get error:
psql: FATAL: role "ubuntu" does not exist
Just run this in your terminal :
$ cd /usr/local/var/postgres/
$ rm postmaster.pid
It's because last time your computer was shut off, Postgresql did not exit gracefully (usually due to running out of battery).

RoR database Error

Trying to run the database in RoR i have this error
Couldn't create database for {"adapter"=>"sqlite3", "pool"=>5, "timeout"=>5000, "database"=>"db/test.sqlite3"}
rake aborted!
Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded. Add gem 'mysql2' to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
but when i do gem list i get that i have mysql2 (0.4.1)
How can i fix this? and also, why does this happens?
Check /config/database.yml file
Probably your file contains something like this:
development:
adapter: sqlite3
pool: 5
timeout: 5000
database: db/development.sqlite3
Change sqlite to mysql and add login settings, also check environment (development, production or test)
you need to run bundle install as you have already added the gem in the Gemflle.Also you need to setup mysql and other libs before installing it.
You should include the mysql2 gem into your gemfile and run 'bundle install'.
Also your config/database.yml should look something like the below
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
Here the username and password will be the one you gave at the time of configuring the mysql inyour system

Rails connects to MySQL from WEBrick but not from Passenger

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.

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.

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