[Ruby on Rails] From MySQL construction to database change

Introduction

I managed to introduce it while referring to various articles, so I will leave it as a memorandum. If there is something strange or something that should be done like this I would appreciate it if you could teach me.

Development environment

ruby 2.5.7 Rails 5.2.4.3 Vagrant 2.2.4 VirtualBox 6.0.14 OS: macOS Catalina centos 7

flow

1 Build MySQL on vagrant 2 Change existing app from Sqlite to MySQL 3 Set new app to MySQL

Build MySQL on vagrant

CentOS confirmation

First, check the current CentOS. To check, check the Vagrantfile in the vagrant file.

Vagrantfile


Vagrant.configure("2") do |config|
    GUEST_RUBY_VERSION = '2.5.7'
    config.vm.box = "centos/7"

...

This time, we will proceed on the assumption that CentOS is 7. CentOS is a typical Linux OS used to build a virtual environment.

Install MySQL (for CentOS7)

Vagrant + Rails6 + MySQL development environment construction First of all, build MySQL on vagrant by referring to this article.

Terminal


$ vagrant ssh
$ sudo yum -y install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
$ sudo yum -y install mysql-community-server
$ mysqld --version

If the version is displayed, it's OK.

After setting automatic start, start MySQL

Set to start automatically when vagrant starts. The second line is OK if mysqld.service is enabled.

Terminal


$ sudo systemctl enable mysqld.service
$ sudo systemctl list-unit-files -t service | grep mysqld
$ sudo systemctl start mysqld.service

MySQL initialization (optional)

https://style.potepan.com/articles/19020.html This article was easy to understand, so After executing $ mysql_secure_installation Let's do the initial setup of MySQL in this article! Please set from.

#Set password for root user(Set root password this time)
$ /usr/bin/mysqladmin -u root password 'root'

#Security-related initial settings(When asked for a password here'root'To)
$ mysql_secure_installation

After setting, execute the following, enter the password, and then mysql> It is OK if this display is displayed.

Terminal


$ mysql -u root -p

Change existing app from Sqlite to MySQL

I was addicted to installing mysql2 with Rails [Beginner] How to change the DB of an existing application to MySQL We will introduce it with reference to the above article.

Create rails app, check database

Create a post table with scaffold as a trial.

Terminal


$ rails new sam
$ cd sam
$ rails g scaffold post name:string

I think that config / database.yml is written like this. By default, it uses the sqlite3 database.

config/database.yml


default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# 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/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

Introduction of gem

Gemfile


# Use sqlite3 as the database for Active Record
gem 'sqlite3'
↓
gem 'mysql2'

Terminal


$ bundle install

If you get an error, do the following or Gemfile gem'mysql2' version gem 'mysql2', '~> 0.4.4' Try changing to.

Terminal


$ sudo yum install -y mysql-devel

Change database settings to MySQL

congig/database.yml


default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password:Enter the password if you have made the initial settings
  host: localhost

development:
  <<: *default
  database: sam_development #sam is the app name.

# 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: sam_test #sam is the app name.

production:
  <<: *default
  database: sample_production
  username: sample_app
  password: <%= ENV['SAMPLE_DATABASE_PASSWORD'] %>

Create a database below.

Terminal


$ bundle exec rake db:create
$ rails db:migrate

Terminal


$ mysql -u root -p
$ show tables from sam_development;
+---------------------------+
| Tables_in_sam_development |
+---------------------------+
| ar_internal_metadata      |
| posts                     |
| schema_migrations         |
+---------------------------+
3 rows in set (0.00 sec)

If it looks like this, the setting is complete.

Set new app to MySQL

This is easier than changing an existing app.

Terminal


$ rails new sample -d mysql
$ cd sample
$ rails g scaffold post name:string

congig/database.yml


default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:Enter the password if you have made the initial settings
  socket: /var/lib/mysql/mysql.sock

development:
  <<: *default
  database: sample_development

# 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: sample_test

# 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
  database: sample_production
  username: sample
  password: <%= ENV['SAMPLE_DATABASE_PASSWORD'] %>

Gemfile


# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'

Terminal


$ bundle exec rake db:create
$ rails db:migrate

If you get an Access denied error, Please log in once as shown below and then execute the above again.

Terminal


$ mysql -u root -p
exit

Summary

It may be played with Access denied by initial setting, It may be unavoidable for security reasons. I would appreciate it if you could teach me any incorrect descriptions or methods.

Also, on twitter, technologies and ideas that have not been uploaded to Qiita are also uploaded, so I would be grateful if you could follow me. Click here for details https://twitter.com/japwork

Recommended Posts

[Ruby on Rails] From MySQL construction to database change
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
[Ruby on Rails] How to change the column name
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby on Rails] Change URL id to column name
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
How to use Ruby on Rails
Ruby on Rails 6.0 environment construction memo
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
Change DB from SQLite to MySQL
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
From Ruby on Rails error message display to Japanese localization
[Ruby on rails + Mysql] Data migration procedure memo when switching from heroku to AWS
[Ruby on Rails] How to use CarrierWave
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Button to return to top
Muscle Ruby on Rails Day 1 ~ Environment Construction ~
[Error] Switch environment construction to use Ruby on Rails oss (open source)
How to solve the local environment construction of Ruby on Rails (MAC)!
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
[Personal memo] Ruby on Rails environment construction (Windows)
[Rails MySQL] How to reset DB on heroku
Ruby on Rails development environment construction on M1 Mac
DataSource connection from WebSphere to MySQL (DataSource property change?)
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
Ruby on Rails Elementary
Ruby on Rails basics
From Java to Ruby !!
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
[Environment construction] Ruby on Rails 5.2 system development environment construction [within 1 hour]
Change the database (MySQL) primary key to any column.
To connect from Spring to MySQL on virtual server (unsolved)
[Ruby On Rails] How to reset DB in Heroku
[Environment construction Mac] Ruby on Rails (+ Webpacker handles errors)
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
(Ruby on Rails6) Reflecting the posted content from the form
(Ruby on Rails6) How to create models and tables
Settings to be done when changing from Sublime Text to VScode (when writing Ruby On Rails)
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
[Ruby On Rails] De-root_path! Redirect notation from a nested "child" view to a nested "parent": show
Ruby on rails learning record -2020.10.03
Rails engineer environment construction ruby2.7.1
Portfolio creation Ruby on Rails
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②