Nuxt.js,Vuetifyにてスクレイピングアプリを作成中。
MySQLを導入し、Node.jsのライブラリであるSequelizeを導入している際のエラー。
どちらかというとSequelizeというよりはMySQLのエラーなのですが、備忘録かつ一助となればと思い、記します。
※Sequelize...MySQL等のDBに簡単にアクセスできるNode.jsのライブラリ。
エラー1 「ERROR: connect ECONNREFUSED 127.0.0.1:3306」
local
$npxsequelize-climodel:generate--nameUser--attributesname:string,itemName:string,price:integer,material:string,brandStyleId:string$Newmodelwascreatedat/Users/../Desktop/nuxt-scraping-app/models/user.js.Newmigrationwascreatedat/Users/../Desktop/nuxt-scraping-app/migrations/20201218120926-create-user.js.
上記によりテーブルとカラムを正常に作成した。そして、
local
$npxsequelize-clidb:migrate
によりmigrateしようとすると、
local
ERROR:connectECONNREFUSED127.0.0.1:3306
接続エラーが発生。
local
$mysql-uroot-Ddatabase_development-p-P3306$ERROR2002(HY000):Can't connect to local MySQL server through socket '/tmp/mysql.sock'(38)
?...なぜか接続できない。
初歩的なミスに気づく。そもそもMySQLサービスをスタートさせていない。
local
$sudomysql.serverstartStartingMySQLSUCCESS!
エラー2 「ERROR: Access denied for user 'root'@'127.0.0.1' to database 'database_development」
こうしてようやくMySQLにログインできたので、再度migrate実行。
local
$npxsequelize-clidb:migrate$ERROR:Accessdeniedforuser'root'@'127.0.0.1'todatabase'database_development'
接続が拒否される。何故だろうと思ったが、以前に同じようなエラーに遭遇し、権限関連のエラーと判定したため権限確認。
local
mysql>showgrantsforroot@'127.0.0.1';+------------------------------------------+|Grantsforroot@127.0.0.1|+------------------------------------------+|GRANTUSAGEON*.*TO`root`@`127.0.0.1`|+------------------------------------------+1rowinset(0.00sec)
config.jsonのユーザー・hostで権限確認するとやはりアクセス権限が付与されていないことを確認。
local
mysql>GRANTALLPRIVILEGESONdatabase_development.*TOroot@'127.0.0.1';# 当該DBへのアクセス権限付与mysql>showgrantsforroot@'127.0.0.1';+------------------------------------------------------------------------+|Grantsforroot@127.0.0.1|+------------------------------------------------------------------------+|GRANTUSAGEON*.*TO`root`@`127.0.0.1`||GRANTALLPRIVILEGESON`database_development`.*TO`root`@`127.0.0.1`|+------------------------------------------------------------------------+2rowsinset(0.00sec)
アクセス権限が付与されたことを確認。これでOKなはず!
local
$npxsequelize-clidb:migrateSequelizeCLI[Node:14.4.0,CLI:6.2.0,ORM:6.3.5]Loadedconfigurationfile"config/config.json".Usingenvironment"development".==20201218120925-create-user: migrating=========20201218120925-create-user: migrated(0.023s)
Done!
学び
MySQLでのエラーは権限関連が多いと感じました。実務に駆け出したばかりのエンジニアは自分を含め、DB周りに弱いと聞きます。積極的にMySQLに触れていくことで、慣れると考えます。実務に入る前に再度MySQLに慣れていくことが必要だと感じました。