LoginSignup
8
6

More than 5 years have passed since last update.

mysql rootのパスワードを忘れてしまったら

Posted at

参考記事
mysqlのリファレンス

エラー内容は以下のような感じになるとおもいます。
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

1.mysql serverの停止

mysql.server stop

MySQLサーバの起動と停止
mysql.server start
mysql.server stop

プロセスの確認
ps ax | grep mysql
ここで止まってなかったら、killコマンドでとめます。

プロセス停止
kill (PID)
プロセス強制停止
kill -9 (PID)
(-9)は送信するシグナルの指定
9 もしくは SIGKILL を指定

2.mysql_safeで起動

mysqld_safe --skip-grant-tables

--skip-grant-tablesコマンドについて

通常の起動では、サーバーは mysql.plugins システム変数を読み取ることによって、ロードするプラグインを判別します。サーバーが --skip-grant-tables オプションで起動された場合、サーバーは mysql.plugins テーブルを参照せず、そこにリストされているプラグインをロードしません。--plugin-load により、--skip-grant-tables が指定されている場合でもプラグインのロードを可能にします。--plugin-load は、プラグインを実行時にロードできない構成で、プラグインを起動時にロードできるようにします。

3.rootユーザーでログイン

mysql -u root

4.パスワードの再設定

use mysql;
update user set password=PASSWORD("ここにパスワードを記述") where User='root';
flush privileges;

5.mysqlサーバーの再起動

mysql.server stop
mysql.server start

以上でできました。

おまけ

今回はpostgresで作っていて途中でmysqlに変更したのですが、
その際にdeviseのmigrateファイルでエラーがでました。

devise/lib/generators/active_record/devise_generator.rb

devise/lib/generators/active_record/devise_generator.rb
 ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.#{ip_column} :current_sign_in_ip # ここがエラーになった箇所です。
      t.#{ip_column} :last_sign_in_ip    # postgresではinet,mysqlではstringになるようです。

~中略

      def ip_column
        # Padded with spaces so it aligns nicely with the rest of the columns.
        "%-8s" % (inet? ? "inet" : "string")
      end

      def inet?
        postgresql?
      end

      def rails5?
        Rails.version.start_with? '5'
      end

      def postgresql?
        config = ActiveRecord::Base.configurations[Rails.env]
        config && config['adapter'] == 'postgresql'
      end

8
6
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
8
6