開発環境
・MacOS mojave 10.14.4
・MySQL5.7
・CentOS7
はじめに
・ローカル環境のデータベースと仮想環境のMySQLのデータベースの値を同じにします。
・ローカルにはMySQLをインストールしてません。仮想環境のみにMySQLをインストールしてます。
ローカル環境のデータベース
ローカル環境のデータベースとは、Rails 5.2.3のデータベースです。
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password
host: hostnumber
#socket: /var/lib/mysql/mysql.sock
development:
<<: *default
database: databasename
①vagrantとVirtualBOXでCentOS7環境を構築
②vagrantでMySQL5.7のデータベースを作成
参照記事:https://enomotodev.hatenablog.com/entry/2016/09/01/225200
③root権限付与
GRANT ALL ON *.* TO root@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
参照記事:https://qiita.com/PallCreaker/items/0b02c5f42be5d1a14adb
④ローカル環境からCentOS7のMySQLにログイン
ローカル環境でデータベースを作成した時と同じhostnumber,passwordを使う。
databasenameも、ローカル環境でデータベースを作成したdatabasename=CentOS7上で作成したMySQLのdatabasenameの関係になるようにする
参照記事:http://mysql.javarou.com/dat/000389.html
$ mysql -hhostnumber -u root -ppassword
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
$ ls init* inittable.sql`
$ mysql -hhostnumber -u root -ppassword databasename < inittable.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
これでローカル環境からCentOS7環境のデータベースにアクセスできた。