#ローカルでのMySQLインストール
Homebrewがインストールされているかを確認
brew --version
Homebrewのインストールが完了している状態で、次にMySQLをインストール。バージョン指定したいときは指定してあげる。
brew install mysql
インストールされたMySQLの情報は下記のコマンドで確認
brew info mysql
Railsにてアプリケーションを新規で作成する際、デフォルトではSQLiteが設定されるため、新規アプリ作成時のコマンドにて下記の通り明示的にMySQLを利用することを宣言。
rails new アプリケーション名 -d mysql
MySQLを「起動」する際には下記のコマンド
mysql.server start
実際にMySQLを利用するにはログインする必要があるため、一旦デフォルトで用意されている「root」ユーザーでログイン。
下記のコマンドで「root」ユーザーでのログインが可能。
mysql -uroot
MySQLはメモリの使用量も多く、利用していない時にはデータベースを停止しておくことをおすすめ。
MySQLを停止するコマンド↓
mysql.server stop
MySQLのセキュリティ設定
mysql_secure_installation
参考↓
Mac環境にMySQLをインストールしてみよう!初期設定までの手順を徹底解説! | 「ポテパンスタイル」
サーバー立ち上げ
設定したらサーバー立ち上げ
mysql.server start
rails s
登録したパスワードでログイン
mysql -uroot -p
http://localhost:3000/
を開くが失敗↓
「パスワードがないから開かねえぞ!」と怒られる
解決策
configフォルダ下のdatabase.ymlファイルを修正
先ほど「MySQLのセキュリティ設定」のときに決めたパスワードを入れる↓
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
socket: /tmp/mysql.sock
パスワードを入れたら再度
http://localhost:3000/
を開く。
Unknown database ‘tomalog_development’
今度は「tomalog_development なんてデータベース知らないよ!」と怒られる。笑
この「tomalog_development」というのはさっきのdatabase.ymlファイルのここの部分のことですね↓
「development環境のデータベースだよ〜」と勝手に定義してくれていたみたいです。
そのデータベースがMySQLにないから表示できないよというのがこのエラーが言いたいこと!
「tomalog_development」がMySQLにないか確認しに行ってみる。
SQLサーバーを立ち上げてログイン。
asatokensei@noMacBook-Air tomalog % mysql.server start
Starting MySQL
. SUCCESS!
asatokensei@noMacBook-Air tomalog % mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.19 Homebrew
Copyright (c) 2000, 2020, 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.
mysql>
SQLサーバー上でデータベース一覧を見るには下のコマンド↓
show databases;
実行すると、
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
うん、たしかにないですね!
ということでtomalog_developmentデータベースを作ることに。
データベースを作るコマンドは
create database データベース名;
実行してみる。
mysql> create database tomalog_development;
Query OK, 1 row affected (0.01 sec)
もう一度確認。
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| tomalog_development |
+---------------------+
5 rows in set (0.01 sec)
できてる!
tomalog_development が追加された!
http://localhost:3000/ に再挑戦!
やったああああ!
開けた!!!
おまけ 個人的によく使うコマンド
ローカル環境での立ち上げとログイン↓
mysql.server start
mysql -uroot -p
mysql.server stop
仮想環境でのサーバー立ち上げとログイン↓
[vagrant@localhost ~]$ sudo systemctl start mysqld.service
[vagrant@localhost tomalog]$ rails db
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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.
mysql>
mysql> exit
Bye
[vagrant@localhost tomalog]$
パスワードの変更↓
mysql> SET PASSWORD = password('新パスワード');
Query OK, 0 rows affected, 1 warning (0.00 sec)
参考