Check if Homebrew is installed
brew --version
With the Homebrew installation completed, then install MySQL. If you want to specify the version, specify it.
brew install mysql
Check the installed MySQL information with the following command
brew info mysql
When creating a new application with Rails, SQLite is set by default, so it is explicitly declared to use MySQL as follows in the command when creating a new application.
rails new application name-d mysql
The following command to "start" MySQL
mysql.server start
Since you need to log in to actually use MySQL, log in as the "root" user that is prepared by default. You can log in as the "root" user with the following command.
mysql -uroot
MySQL uses a lot of memory, so it is recommended to stop the database when it is not in use. Command to stop MySQL ↓
mysql.server stop
mysql_secure_installation
After setting, start the server
mysql.server start
rails s
Login with the registered password
mysql -uroot -p
http://localhost:3000/ Opens but fails ↓
"I don't have a password, so I can't open it!"
Modify the database.yml file under the config folder
Enter the password you decided in "MySQL security settings" earlier ↓
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
socket: /tmp/mysql.sock
Enter the password and try again http://localhost:3000/ open.
… But again an error. ↓
Unknown database ‘tomalog_development’ This time, he gets angry, "I don't know the database about tomalog_development!". Lol
This "tomalog_development" is this part of the database.yml file earlier ↓
It seems that he arbitrarily defined "It's a database of development environment".
What this error says is that the database isn't in MySQL and can't be viewed!
Go check if "tomalog_development" is in MySQL.
Start SQL server and log in.
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>
To see the database list on SQL server, use the command below ↓
show databases;
When executed,
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
Yeah, it's true!
So I decided to create a tomalog_development database.
The command to create a database is
create database database name;
Try to run it.
mysql> create database tomalog_development;
Query OK, 1 row affected (0.01 sec)
Check again.
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| tomalog_development |
+---------------------+
5 rows in set (0.01 sec)
It's done! tomalog_development has been added!
Re-challenge http: // localhost: 3000 /!
I did it ah ah! I opened it! !! !!
Launch and login in local environment ↓
mysql.server start
mysql -uroot -p
mysql.server stop
Server startup and login in a virtual environment ↓
[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]$
Change password ↓
mysql> SET PASSWORD = password('New password');
Query OK, 0 rows affected, 1 warning (0.00 sec)
https://style.potepan.com/articles/19020.html
[Build CentOS7 + Rails + MySQL environment on Vagrant --Qiita](https://qiita.com/kt_flcl/items/50504b125e2c4eaee23d#rails%E3%83%97%E3%83%AD%E3%82%B8% E3% 82% A7% E3% 82% AF% E3% 83% 88% E3% 82% 92% E4% BD% 9C% E6% 88% 90% E3% 81% 97% E3% 81% A6% E3% 81% BF% E3% 82% 8B)
Recommended Posts