[RUBY] [Rails] Entwicklung mit MySQL

Ich habe bisher einige Webanwendungen erstellt und gelernt Alle DBs haben die Standard-SQLite verwendet.

Wenn Sie die Produktionsumgebung beim Erstellen einer neuen Anwendung dieses Mal auf AWS einstellen möchten, müssen Sie MySQL ... Also würde ich gerne MySQL benutzen! Ich bin noch ein Anfänger, bitte weisen Sie auf Fehler hin.

Was ist MySQL?

Es ist die am häufigsten verwendete Datenbank der Welt und verfügt über die folgenden Funktionen.

Unterschied zu SQLite

Das standardmäßig mit installierten Rails verwendete RDBMS ist ** SQLite ** SQLite verfügt über die folgenden Funktionen

Es scheint besser, MySQL zu verwenden, wenn es sich um eine riesige Datenbank handelt

Versuchen Sie, mit MySQL zu entwickeln

Rails verwendet also standardmäßig SQLite Um MySQL zu verwenden, geben Sie es beim Start an

Schienen neuer Anwendungsname--database=mysql

Kann ausgelassen werden

Schienen neuer Anwendungsname-d mysql

Auf diese Weise kann RDBMS anscheinend in MySQL geändert werden. Überprüfen Sie die Gemfile für alle Fälle ... Wenn nicht angegeben

Gemfile


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

Auf der anderen Seite, wenn Sie angeben

Gemfile


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

ist geworden! !!

Aber das Problem ist ... Rails New Anwendungsname -d Bei der Ausführung von MySQL ist ein Fehler aufgetreten. .. ..

・
・
(Viele Protokolle)
・

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/mariko/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
/Users/mariko/.rbenv/versions/2.6.5/bin/ruby -I /Users/mariko/.rbenv/versions/2.6.5/lib/ruby/2.6.0 -r ./siteconf20201104-33105-i9fwuc.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/opt/[email protected]/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/opt/[email protected]/lib
-----
creating Makefile

current directory: /Users/mariko/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/mariko/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/mariko/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/mariko/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-19/2.6.0/mysql2-0.5.3/gem_make.out

An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2
         run  bundle binstubs bundler
Could not find gem 'mysql2 (>= 0.4.4)' in any of the gem sources listed in your Gemfile.
         run  bundle exec spring binstub --all
Could not find gem 'mysql2 (>= 0.4.4)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
       rails  webpacker:install
Could not find gem 'mysql2 (>= 0.4.4)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

Ich werde es vorerst übersetzen "Während der Installation von mysql2 (0.5.3) ist ein Fehler aufgetreten, und Bundler kann nicht fortfahren." ···weiß nicht···. Ich muss es vorerst schlagen ...!

Lösungen

Tun Sie vorerst, was im Protokoll steht

$ gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'

ERROR:  While executing gem ... (OptionParser::InvalidArgument)
    invalid argument: --source https://rubygems.org/

Installieren Sie MySQL auf einem Mac

Ich habe den Teil der Installation von MySQL völlig übersehen. Installieren Sie anhand des folgenden Artikels! Verfahren zum Installieren von MySQL auf einem Mac mit Homebrew

Installieren Sie mit dem folgenden Befehl

$ brew update
$ brew install mysql

Überprüfen Sie, ob es installiert ist

$ brew info mysql

mysql: stable 8.0.22 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/8.0/en/
Conflicts with:
  mariadb (because mysql, mariadb, and percona install the same binaries)
  percona-server (because mysql, mariadb, and percona install the same binaries)
/usr/local/Cellar/mysql/8.0.22 (294 files, 296.5MB) *
  Poured from bottle on 2020-11-04 at 13:55:11
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mysql.rb
License: GPL-2.0
==> Dependencies
Build: cmake ✘
Required: [email protected] ✔, protobuf ✔
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Analytics
install: 85,249 (30 days), 214,834 (90 days), 813,126 (365 days)
install-on-request: 83,371 (30 days), 209,508 (90 days), 784,193 (365 days)
build-error: 0 (30 days)

Version 8.0.22 wurde installiert!

Versuchen Sie, MySQL zu starten

$mysql.server start
Starting MySQL
 SUCCESS! 

Schließen Sie an und überprüfen Sie den Betrieb

$mysql -uroot

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

・ ・ ・ ・ Ein weiterer Fehler (Weinen) Ist es nutzlos, weil es kein Passwort gibt? Ich denke, ich werde es vorerst einstellen.

Sicherheitseinstellungen

$mysql_secure_installation

Enter password for user root:(Geben Sie das Passwort ein)

Richten Sie ein Plug-In ein, das die Stärke Ihres Passworts überprüft? "Y"

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

Wählen Sie eine Verifizierungsstufe.

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

Da es sich anscheinend um eine Produktionsumgebung handelt, wählen Sie 2. Wenn die Stärke des zuvor eingegebenen Passworts nicht übereinstimmt, werden Sie aufgefordert, es erneut einzugeben. Wenn Sie das Passwort zweimal eingeben, ist es in Ordnung, es zu bestätigen? Weil gesagt wird, "y"

Estimated strength of the password: 50 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: 

Re-enter new password: 
Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

Anonymen Benutzer löschen (anonymer Benutzer)? "Y"

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Verhindert die Anmeldung von Remote-Root-Benutzern. Anmeldung verbieten? "Y"

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

Löschen Sie die Datenbank mit dem Namen test, die standardmäßig erstellt wird? "Y"

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.
 - Removing privileges on test database...
Success.

Laden Sie die Autorisierungstabelle sofort neu, damit die Änderungen wirksam werden? "Y"

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

Haben Sie vorerst die Einstellungen vorgenommen?

Überprüfen Sie den Betrieb erneut

$mysql -u root -p
Enter password:(Passworteingabe)

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.32 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>

Äh, es hat funktioniert! !! !! Weinen

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

Die Testdatenbank ist auch weg!

Beenden Sie MySQL einmal ... Hör auf ...

$ mysql.server stop
Shutting down MySQL
. SUCCESS! 

Gehen Sie zurück zur App und bündeln Sie die Installation

Der gleiche Fehler ... weinen

Ich fuhr mit Bezug auf den folgenden Artikel fort! [Rails] Was tun, wenn MySQL2 die Installation nicht bündeln kann?

$ brew info openssl
[email protected]: stable 1.1.1h (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/usr/local/Cellar/[email protected]/1.1.1g (8,059 files, 18MB)
  Poured from bottle on 2020-05-21 at 18:53:48
/usr/local/Cellar/[email protected]/1.1.1h (8,067 files, 18.5MB)
  Poured from bottle on 2020-11-01 at 12:26:48
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/[email protected]
License: OpenSSL
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/[email protected]/certs

and run
  /usr/local/opt/[email protected]/bin/c_rehash

[email protected] is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> /Users/mariko/.bash_profile

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

For pkg-config to find [email protected] you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"

==> Analytics
install: 835,013 (30 days), 1,906,254 (90 days), 7,237,126 (365 days)
install-on-request: 136,062 (30 days), 257,990 (90 days), 1,007,070 (365 days)
build-error: 0 (30 days)

Dieser Teil scheint wichtig zu sein.

export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

Führen Sie die folgenden zwei Befehle aus

$ bundle config --local build.mysql2 "--with-cppflags=-I/usr/local/opt/[email protected]/include"
You are replacing the current local value of build.mysql2, which is currently nil

$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/[email protected]/lib"
You are replacing the current local value of build.mysql2, which is currently "--with-cppflags=-I/usr/local/opt/[email protected]/include"

Bundle erneut installieren Ich konnte es installieren ...! !! !! Weinen

Zusammenfassung

Ich musste hin und her gehen, bis ich MySQL in meine App einführte. Es tut mir leid, wenn es schwer zu lesen ist. Ich hoffe, Sie gewöhnen sich auch nur ein wenig an die Macht von jemandem.

Vielen Dank an die Autoren für ihre Referenz.

Es scheint ein Verfahren zu geben, um unterwegs auf SQLite umzusteigen, daher werde ich es mit den Apps versuchen, die ich bisher erstellt habe.

Recommended Posts

[Rails] Entwicklung mit MySQL
[Umgebungskonstruktion mit Docker] Rails 6 & MySQL 8
Erstellen Sie eine Entwicklungsumgebung für Rails-Anwendungen mit Docker [Docker, Rails, Puma, Nginx, MySQL]
Stellen Sie Heroku mit Rails6 (Cloud9 + Ubuntu) + MySQL bereit
Mit Docker auf Heroku bereitstellen (Rails 6, MySQL)
[Docker] Entwicklung der Entwicklungsumgebung Rails6 / Ruby2.7 / MySQL8
Verfahren zum Erstellen einer Rails-Anwendungsentwicklungsumgebung mit Docker [Rails, MySQL, Docker]
[Rails 6] Laufzeitfehler mit $ Rails s
Gerät mit Schienen handhaben
[Rails] Lernen mit Rails Tutorial
[Schienen] Test mit RSpec
Plug-In-Entwicklung mit ImageJ
Fehler beim Bereitstellen von Rails5 + MySQL auf Heroku mit Docker-Compose
Unterstützt Mehrsprachigkeit mit Rails!
Konstruktionshandbuch für eine stabile Entwicklungsumgebung für "Rails6" mit "Docker-compose"
Rails5 + MySQL8.0 + Top-Level-Volume-Umgebungskonstruktion mit Docker-Compose
Etwa der Ablauf der Entwicklung von Webanwendungen mit Rails.
Build Rails (API) x MySQL x Nuxt.js Umgebung mit Docker
Aufbau der Rails6-Entwicklungsumgebung [Mac]
[Vorlage] MySQL-Verbindung mit Java
[Rails] Polymorph mit graphql-rubin ausdrücken
Rails6 (MySQL, Ubuntu-Umgebung, Cloud9)
[Rails] Videos mit Rails hochladen (ActiveStorage)
[Vue Rails] "Hallo Vue!" Wird mit Vue + Rails angezeigt
Japanisieren Sie mit i18n mit Rails
Vorbereitung für die Entwicklung mit Rails
Führen Sie Rails immer im Docker aus
Stellen Sie mit Java eine Verbindung zu MySQL 8 her
Verwenden Sie mit Rails 6.0 mehrere Datenbanken
[Rails] Geben Sie das Format mit link_to an
[Rails / MySQL] Aufbau einer Mac-Umgebung
[Docker] Wird immer mit Docker + Rails verwendet
Ich habe eine Entwicklungsumgebung mit Rails6 + Docker + PostgreSQL + Materialise erstellt.
[Vagrant] Bereiten Sie die LAMP-Entwicklungsumgebung mit Vagrant vor (Centos + Apache + MySQL + PHP).
Erstellen Sie ein Portfolio mit Rails + Postgres SQL
[Schienen] Getriebe mit LINE Bot schieben
Vergleich der WEB-Anwendungsentwicklung mit Rails und Java Servlet + JSP
[Rails] Pagenation mit Ajax kompatibel machen
Hot Deploy mit Spring Boot-Entwicklung
Erstellen einer Entwicklungsumgebung für Ruby on Rails mit Docker + VSCode (Remote Container)
JDBC-Verbindung mit MySQL 8.x * Hinweise *
Bereiten Sie die Java-Entwicklungsumgebung mit Atom vor
[Rails 6] API-Entwicklung mit GraphQL (Query)