6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQL8.0が接続できない

Posted at

背景

Ruby on Railsで個人アプリ開発中にMySQLへの接続ができないエラーが発生した。
MySQLのバージョンを8.0に上げたことが大きな原因のようだった。以下に詳細を示す。

環境

  • mac OS Mojova 10.14.2
  • MySQL 8.0.13 for osx10.14 on x86_64 (Homebrew)
  • ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin17]
  • Rails 4.2.10

目次

エラーは主に2つ。

  • Sequel Proからrootユーザーで接続できない
  • schema_migrationsテーブルが存在しない。

Sequel Proからrootユーザーで接続できない

エラー概要

  • SequelProからrootユーザでローカルのMysqlに接続...できず。
  • ターミナルからMysqlにログインし、確認しようとしたら下記エラー。
$ mysql -uroot

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

原因

  • あったはずのrootユーザが存在しなくなっていた。(これは原因不明でした...)

解決

1. MySQL再起動、接続
$ mysql.server stop
//MySQL停止

$ mysql.server start --skip-grant-tables
//MySQL起動(権限不要のオプションをつける)

$ mysql -uroot
//MySQL接続
2. データベース選択、userレコード削除
mysql> use mysql;
// userテーブルがあるmysqlDBを選択。

mysql> truncate table user;
//userテーブルのレコードをすべて削除

mysql> flush privileges;
//DBに反映
3. rootユーザ作成
mysql> CREATE USER 'root'@'%' IDENTIFIED BY '';
//rootユーザ作成(権限無し)
//   passwordを指定してもよい。その場合は下記(passwordの文字列は任意で指定)
//   mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'passpord';

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
//rootユーザに全権限を付与

mysql> flush privileges;
//DBに反映

これでユーザ権限問題は解決。

参考

https://dev.mysql.com/doc/refman/8.0/en/privilege-changes.html
https://www.aipacommander.com/entry/2014/05/26/152247
https://stackoverflow.com/questions/50177216/how-to-grant-all-privileges-to-root-user-in-mysql-8-0

schema_migrationsテーブルが存在しないとのこと。

エラー概要

Railsサーバーを立ち上げ、アプリアクセス時にAcitiveRecordでエラー。MySQLにschema_migrationsテーブルが存在しないとのこと。

ActiveRecord::StatementInvalid: Mysql2::Error: The user specified as a definer ('mysql.infoschema'@'localhost') does not exist: SHOW TABLES LIKE 'schema_migrations'

解決

  • 以下のコマンドを入力
$mysql_upgrade -u root -p

参考

https://qiita.com/yoshi1729/items/8a023d28c92dc5e62f86
上記記事の内容をなぞった。感謝。

最後に

  • 上記方法で解決しましたが、かなり時間がかかりました。
  • 内容の誤り等ありましたらコメントいただけると幸いです。
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?