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.
ruby 2.5.7 Rails 5.2.4.3 Vagrant 2.2.4 VirtualBox 6.0.14 OS: macOS Catalina centos 7
1 Build MySQL on vagrant 2 Change existing app from Sqlite to MySQL 3 Set new app to MySQL
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.
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.
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
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
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 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
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
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.
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
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