Marchan4
@Marchan4 (Marchan4)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

rbenvを使ってruby 3.0.0をインストールした場合、mysql2をどういうコマンドでインストールすればよいのでしょうか?

使用環境はrubyバージョン3.0.0, mysqlバージョン5.7, bundlerバージョン2.2.3です。
使用機器macOS Big Sur バージョン11.2.2です。使用中のテキストエディタはAtomです。

rbenvを使ってruby 3.0.0をインストールしたり、Homebrewを使って(brewコマンドを使って)mysql 5.7をインストールした場合、どのようなコマンドを入力すればよいのでしょうか。

rubyの現在のバージョンは以下の通りです。

$ ruby -v

ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin17]

mysqlの現在のバージョンは以下の通りです。

$ mysql --version

mysql  Ver 14.14 Distrib 5.7.32, for osx10.16 (x86_64) using  EditLine wr
apper

homebrewの現在のバージョンは以下の通りです。

$ brew -v

Homebrew 3.0.5
Homebrew/homebrew-core (git revision f376c69429; last commit 2021-03-15)
Homebrew/homebrew-cask (git revision 83abe76d01; last commit 2021-03-15)

自分で試したこと

$ bundle install

Fetching gem metadata from https://rubygems.org/.
Using bundler 2.2.3
Following files may not be writable, so sudo is needed:
  /Users/ユーザ名/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/build_info/mysql2-0.5
.3.info
Fetching mysql2 0.5.3



Your user account isn't allowed to install to the system RubyGems.
  You can cancel this installation and run:

      bundle config set --local path 'vendor/bundle'
      bundle install

  to install the gems into ./vendor/bundle/, or you can enter your password
  and install the bundled gems to RubyGems using sudo.

  Password:
Installing mysql2 0.5.3 with native extensions
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
$ brew install openssl@1.1

Error: openssl@1.1 1.1.1j is already installed.
To upgrade to 1.1.1k, run:
  brew upgrade openssl@1.1
$ brew upgrade openssl@1.1

==> Upgrading 1 outdated package:
openssl@1.1 1.1.1j -> 1.1.1k
==> Upgrading openssl@1.1 1.1.1j -> 1.1.1k
==> Downloading https://homebrew.bintray.com/bottles/openssl%401.1-1.1.1k.big_sur.bot
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/17d94c51ddfa8364baed5f3a75
######################################################################## 100.0%
==> Pouring openssl@1.1-1.1.1k.big_sur.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs


and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have openssl@1.1 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> /Users/ユーザ名/.b
ash_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/openssl@1.1/1.1.1k: 8,071 files, 18.5MB
Removing: /usr/local/Cellar/openssl@1.1/1.1.1j... (8,071 files, 18.5MB)
Removing: /Users/ユーザ名/Library/Caches/Homebrew/openssl@1.1--1.1.1j.big_sur.bot
tle.tar.gz... (5.4MB)
$ brew install openssl@1.1

Warning: openssl@1.1 1.1.1k is already installed and up-to-date.
To reinstall 1.1.1k, run:
  brew reinstall openssl@1.1
$ brew reinstall openssl@1.1

==> Downloading https://homebrew.bintray.com/bottles/openssl%401.1-1.1.1k.big_sur.bot
Already downloaded: /Users/ユーザ名/Library/Caches/Homebrew/downloads/fae83f76186
7e592ca8209f4cf851a681825ea9a2b5af870b9237aa35ca1ef0f--openssl@1.1-1.1.1k.big_sur.bot
tle.tar.gz
==> Reinstalling openssl@1.1
==> Pouring openssl@1.1-1.1.1k.big_sur.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs


and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have openssl@1.1 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> /Users/ユーザ名/.b
ash_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/openssl@1.1/1.1.1k: 8,071 files, 18.5MB
$ gem install mysql2 -- --with-cppflags=-I/usr/l
ocal/opt/openssl@1.1/include --with-ldflags=-L/usr/local/opt/openssl@1.1/lib

Building native extensions with: '--with-cppflags=-I/usr/local/opt/openssl@1.1/includ
e --with-ldflags=-L/usr/local/opt/openssl@1.1/lib'
This could take a while...
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied @ rb_sysopen - /Users/ユーザ名/.rbenv/versions/3.0.0/lib/ru
by/gems/3.0.0/build_info/mysql2-0.5.3.info
$ brew info openssl@1.1

openssl@1.1: stable 1.1.1k (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/usr/local/Cellar/openssl@1.1/1.1.1k (8,071 files, 18.5MB)
  Poured from bottle on 2021-03-29 at 14:37:20
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@1.1.rb
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/openssl@1.1/certs


and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have openssl@1.1 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> /Users/ユーザ名/.b
ash_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

==> Analytics
install: 841,099 (30 days), 2,355,026 (90 days), 8,918,368 (365 days)
install-on-request: 73,357 (30 days), 317,809 (90 days), 1,268,061 (365 days)
build-error: 0 (30 days)
$ bundle config --local build.mysql2 "--with-cpp

flags=-I/usr/local/opt/openssl@1.1/include --with-ldflags=-L/usr/local/opt/openssl@1.
1/lib"

You are replacing the current local value of build.mysql2, which is currently "--with
-ldflags=-L/usr/local/opt/openssl@1.1/lib --with-cppflags=-I/usr/local/opt/openssl@1.
1/include"
$ bundle install

Fetching gem metadata from https://rubygems.org/.
Using bundler 2.2.3
Following files may not be writable, so sudo is needed:
  /Users/ユーザ名/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/build_info/mysql2-0.5
.3.info
Fetching mysql2 0.5.3


Your user account isn't allowed to install to the system RubyGems.
  You can cancel this installation and run:

      bundle config set --local path 'vendor/bundle'
      bundle install

  to install the gems into ./vendor/bundle/, or you can enter your password
  and install the bundled gems to RubyGems using sudo.


  Password:


Your user account isn't allowed to install to the system RubyGems.
  You can cancel this installation and run:

      bundle config set --local path 'vendor/bundle'
      bundle install

  to install the gems into ./vendor/bundle/, or you can enter your password
  and install the bundled gems to RubyGems using sudo.

  Password:
Installing mysql2 0.5.3 with native extensions
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
$ bundle install

Using bundler 2.2.3
Using mysql2 0.5.3
Following files may not be writable, so sudo is needed:
  /Users/ユーザ名/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/build_info/mysql2-0.5
.3.info
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
0

2Answer

~/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/build_info/mysql2-0.5
.3.info に書き込み権限がないようです。 mysql2 gem をインストールするときに sudo をつけてインストールコマンドを実行されたのでは?

権限を正しい状態にするには gem を再インストールするのが手っ取り早いです。 sudo gem uninstall mysql2 でアンインストールして sudo をつけずに bundle install を実行してください。

もし gem ではなく rbenv か Ruby をインストールするときに sudo をつけたなら、 .rbenv をまるごと消して作り直すほうがいいかもしれません。

1Like

最後のbundle installでgem入ってるぽいので、あとはmysql2を動かすだけじゃないでしょうか?
mysql2がインストールされているか不安なら$ bundle listってしてmysql2があれば入ってますよ。

あとは、rubyファイルでmysql2呼び出して実行するだけじゃないかしら。

hoge.rb
require mysql2

db_option = {
  host: 127.0.0.1,
  username: root,
  password: pass
}

client = Mysql2::Client.new(db_option)
client.query("INSERT INTO test VALUES (1)")
0Like

Your answer might help someone💌