fluid_27’s blog

勉強した内容をアウトプットするためのブログ

MySQLのrootパスワードが分からなくなったので、パスワード再設定

 

やりたかった事

railsで作成したアプリをunicorn+nginx+MySQLでデプロイする。

 

問題となった事

MySQLのrootパスワードが環境変数で指定していたものと違うらしく、unicornが立ち上がらない。

 

エラーの流れ

unicornを起動してみる。

[ryoji@ip-10-0-0-225 tempo]$ unicorn_rails -c /var/www/rails/tempo/config/unicorn.conf.rb -D -E production
master failed to start, check stderr log for details

立ち上がらない。

で、 $ vi unicorn.log でログを見てみる。

E, [2021-05-09T04:28:16.335526 #2759] ERROR -- : Access denied for user 'root'@'localhost' (using password: YES) (Mysql2::Error::ConnectionError)
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/var/www/rails/tempo/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.6/lib/active_record/connection_handling.rb:90:in `connection'
/var/www/rails/tempo/config/unicorn.conf.rb:20:in `block in reload'
/home/ryoji/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/unicorn-5.8.0/lib/unicorn/http_server.rb:542:in `spawn_missing_workers'
/home/ryoji/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/unicorn-5.8.0/lib/unicorn/http_server.rb:144:in `start'
/home/ryoji/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/unicorn-5.8.0/bin/unicorn_rails:209:in `<top (required)>'
/home/ryoji/.rbenv/versions/2.6.2/bin/unicorn_rails:23:in `load'
/home/ryoji/.rbenv/versions/2.6.2/bin/unicorn_rails:23:in `<main>'

んー、MySQLのパスワードが違う。もしくは権限がない、と。

 

mysql -u root -p

でパスワード打ち込んでみる。

 

入らない。。

コンソールを立ち上げて

pry(main)> Rails.application.credentials[:db][:password]

 

環境変数に保存していたパスワードを確認。

入力してみる。


やっぱり、入らない。。

ってことは権限の問題ではなくdatabase.ymlで指定しているパスワードが違うってこと。
改めてメモっていたパスワードを確認して入力。


やっぱり入らない。。なぜだ。

 

解決策

いったん、rootパスワードを空にしてから再設定。

で解決した。

 

手順

 1. いったんMySQLを止める。

 

 2. /etc/my.cnf に

  skip-grant-tables を追記。

 

 3. MySQLを立ち上げる

 

 4. MySQLにログインし、

  UPDATE mysql.user SET authentication_string=null WHERE User='root';

  でパスワードを空にする。

 

 5. MySQLを止め、/etc/my.cnf の

  skip-grant-tables を削除。

 

 6. MySQLを起動させ、MySQLにパスワードなしでログイン。

 

 7.  ALTER USER 'root'@'localhost' identified BY '新しいパスワード';

   で新しいパスワードを設定。

 

実際にやった流れ

https://server-recipe.com/1412/


の記事を参考に

$ service mysqld stop
$ vi /etc/my.cnf


で以下を追記

skip-grant-tables

 

$ service mysqld start
して

mysql -u root

 

で、MySQLにログインする。

mysql> UPDATE mysql.user SET authentication_string=null WHERE User='root';

でいったんパスワードを空にする。

 

その後、上記のサイトだと

mysql> update user set authentication_string= password('(新しいパスワード)') where user='root';

で新しいパスワードを設定する流れなのですが、上記の方法だとうまくいきませんでした。。

一見、設定できたっぽかったので、

skip-grant-tables

を削除して、新しいパスワードでログインしようとすると、パスワードが違う。

となってしまって、結局ログインできず。。

 

なぜ?となってググってみた結果、どうやら、今使っているバージョンではパスワード設定コマンドが違うらしい。

下記サイト参考

https://www-creators.com/archives/5574

 

 

ので、代わりに

$ ALTER USER 'root'@'localhost' identified BY '新しいパスワード';

という文体でパスワードを設定してみることに。

 

もっかい

skip-grant-tables

を $ vi /etc/my.cnf で追記して、MySQL再起動→ MySQLログイン→ ALTER USER〜でパスワード再設定してみる。 

[ryoji@ip-10-0-0-225 ~]$ sudo service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
[ryoji@ip-10-0-0-225 ~]$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 8.0.23 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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> USE mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed
mysql> ALTER USER 'root'@'localhost' identified BY '新しいパスワード'; ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement mysql> exit


一応できたっぽい。


再びMySQL止めて、

skip-grant-tables

を削除。
からの、MySQL立ち上げ→ 新しいパスワードで入れるか確認してみる。

[ryoji@ip-10-0-0-225 ~]$ sudo service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service

[ryoji@ip-10-0-0-225 ~]$ mysql -u root -p Enter password:
Welcome to the MySQL monitor.
・・・(略)
mysql> exit

 

改めてunicornを立ち上げてみる。

[ryoji@ip-10-0-0-225 ~]$ unicorn_rails -c /var/www/rails/tempo/config/unicorn.conf.rb -D -E production

 

立ち上がったか、確認。

[ryoji@ip-10-0-0-225 ~]$ ps -ef | grep unicorn | grep -v grep

ryoji 5381 1 6 07:19 ? 00:00:00 unicorn_rails master -c /var/www/rails/tempo/config/unicorn.conf.rb -D -E production ryoji 5387 5381 0 07:19 ? 00:00:00 unicorn_rails worker[0] -c /var/www/rails/tempo/config/unicorn.conf.rb -D -E production ryoji 5389 5381 0 07:19 ? 00:00:00 unicorn_rails worker[1] -c /var/www/rails/tempo/config/unicorn.conf.rb -D -E

 

無事立ち上がりました!!