受講料が最大70%OFF 受講料が最大70%OFF

MySQLのパスワード変更方法についてまとめています。

実行OSはUbuntu 18.04、MySQLは5.7.28を前提としています。

MySQLのパスワードを変更するには、set passwordを使用

mysqlコマンドにて、MySQLのrootアカウントで接続し、set passwodでパスワードを設定します。

# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> set password for root@localhost='my-password';
Query OK, 0 rows affected (0.00 sec)

上記の例では、root@localhostアカウントに、パスワード「my-passowrd」を設定しています。

MySQL 5.6と5.7では、パスワードの保管カラムが変更

MySQL5.7では、パスワードのハッシュ値はmysq.userテーブルのauthentication_stringに変更になりました。5.6では、mysql.userテーブルのpasswordカラムにハッシュ値が格納されていました。

MySQL5.7では、ホスト名、ユーザ名、パスワードハッシュ値一覧を取得するSQLは以下のようになります。

mysql> SELECT Host, User, authentication_string FROM mysql.user;
+-----------+---------------+-------------------------------------------+
| Host      | User          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| localhost | root          | *27C01464AD101AED1E65AC21152499A396B4CF72 |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| %         | root          | *27C01464AD101AED1E65AC21152499A396B4CF72 |
+-----------+---------------+-------------------------------------------+
4 rows in set (0.01 sec)

MySQLのrootパスワードを忘れたときの対処方法

rootの初期パスワードを確認するには、MySQLのログをチェック

MySQL5.7は、初回起動時にrootの初期パスワードをランダムに設定します。設定された初期パスワードは、以下のコマンドで確認できます。

% grep "temporary password" /var/log/mysql/error.log 
[Note] A temporary password is generated for root@localhost: xxxxxx

また、OSのroot権限での実行が出来る場合は、mysqlをセーフモードで実行してパスワードを上書き変更することが可能です。

参考)MySQLでrootパスワードを忘れた場合の対処方法

コマンド行からrootパスワードを割り当てるには、mysqladmin

mysqladminで、MySQLのrootアカウントのパスワードを変更できます。

# mysqladmin -u root -p  password "mypass"
Enter password: 
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

上記の例は、MySQLのパスワードをmypassに変更しています。

MySQLにパスワード無しでログインするには、skip-grant-tablesを設定

MySQL5.7のインストール時にmysql_secure_installationを実行している場合は、パスワードの長さや使用文字種の制限が設定され、パスワード無しや短いパスワードの設定ができなくなります。

【関連記事】
MySQL 8.0をUbuntu 18.04にインストール リポジトリの登録方法は?

ローカルの検証環境など、パスワードなしでMySQLにログインしたい場合は、/etc/mysql/mysql.conf.d/mysqld.cnfの[mysqld]エントリに、skip-grant-tablesを追加します。

# cat /etc/mysql/mysql.conf.d/mysqld.cnf 
# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
 :
 :
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
#log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
skip-grant-tables

※上記では、MySQLのrootパスワードを、空(””)に設定しています。

さらに、MySQLアカウントに空のパスワードを設定します。

% mysqladmin -u root -pmypass password ""
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

mysqlコマンドを実行して、パスワード無しでMySQLサービスに接続することができました。

% mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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>

まとめ

エンジニアになりたい人に選ばれるプログラミングスクール「ポテパンキャンプ 」

ポテパンキャンプは卒業生の多くがWebエンジニアとして活躍している実践型プログラミングスクールです。 1000名以上が受講しており、その多くが上場企業、ベンチャー企業のWebエンジニアとして活躍しています。

基礎的な学習だけで満足せず、実際にプログラミングを覚えて実践で使えるレベルまで学習したいという方に人気です。 プログラミングを学習し実践で使うには様々な要素が必要です。

それがマルっと詰まっているポテパンキャンプでプログラミングを学習してみませんか?

卒業生の多くがWebエンジニアとして活躍

卒業生の多くがWeb企業で活躍しております。
実践的なカリキュラムをこなしているからこそ現場でも戦力となっております。
活躍する卒業生のインタビューもございますので是非御覧ください。

経験豊富なエンジニア陣が直接指導

実践的なカリキュラムと経験豊富なエンジニアが直接指導にあたります。
有名企業のエンジニアも多数在籍し品質高いWebアプリケーションを作れるようサポートします。

満足度高くコスパの高いプログラミングスクール「ポテパンキャンプ」

運営する株式会社ポテパンは10,000人以上のエンジニアのキャリアサポートを行ってきております。
そのノウハウを活かして実践的なカリキュラムを随時アップデートしております。

代表の宮崎もプログラミングを覚えサイトを作りポテパンを創業しました。
本気でプログラミングを身につけたいという方にコスパ良く受講していただきたいと思っておりますので、気になる方はぜひスクール詳細をのぞいてくださいませ。