鶏口牛後な日々

心の赴くまま、やりたいことを仕事に。

Amazon LinuxにyumでMysql5.7をインストール&初期設定した

最初に

環境構築中に少し間、期間が空いてしまったため、Mysqlをインストールしたかどうかを忘れてしまいました。 バージョンを見てみると、 MariaDBバージョンが出てきたので、インストールした模様、と思ってしまいました。 が、よく見るとバージョンも古いし、使って居るMySQLに合わせてインストールし直すことにしました。

MySQL5.7を入れることにしました。

MariaDBのアンインストール

まず、CentOSには、デフォルトでMariaDBが入っていることがあるらしいです。 MariaDBは、バージョン5.5くらいまでは互換性があり、ほぼ機能も同じだったらしいですが、その後袂を分かって、5.6、5.7とバージョンを追うごとに色々と違いが出てきているのだとか。

今回は、MySQL5.7を入れたいので、デフォルトで入っていたらしいMariaDBの競合しそうなライブラリをアンインストールし、データフォルダを削除します。

yum remove mariadb-lib
rm -rf /var/lib/mysql/

MySQL5.7が含まれているレポジトリをダウンロード

yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

MySQL5.7のライブラリをインストール

yum install mysql-community-server

インストールできたか確認

mysqld --version

f:id:TACOSVilledge:20181019181651p:plain

これでインストールができました。

初期設定

あれ? パスワードってなんだっけ、と思いましたが、 /var/log/mysql.log に表示されています。 ログは流されていってしまうので、grep 'temporary password' /var/log/mysqld.log で検索すると一発で表示してくれます。

2018-10-19T09:58:36.185781Z 1 [Note] A temporary password is generated for root@localhost: ksrnuawejal,yad

この後の動きとして、パスワードが分からなくなってしまった場合を調べて以下2ケース実施しましたがいずれも「Access denied」と出てしまいました。

phpMyAdminコマンドを使う

mysqladmin -u root password 'newpassword'

こちらについては、過去に使っていたのですが、今回はうまくいかず。 ちなみに、

~/.bash_historyに平文で保存されてしまうのでオススメしない

という記述を見つけました。(引用元

セーフモードで権限管理のテーブルを読み込ませないようにしてrootでパスワードを入れずにログイン

mysqld_safe --skip-grant-tables &

こちらのコマンドが出てきました。 章題のごとく、権限管理のテーブルを読み込ませないようにするコマンドらしく、これを打つとrootにパスワードなしでログインできるようになるのだとか。 ただ、私はこちらがうまく通りませんでした。

CentOS 7ではsystemdで管理されているため、mysqld_safeが使用できないようです。 そのため、systemctlから設定を施す必要があるようです。

との回答を発見しました。

systemctl stop mysqld
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
systemctl start mysqld
mysql -u root

とすることで、パスワードなしにログインできるとのこと。 必要な時はこれもやってみる。

mysql_secure_installation

実際にやったのはこちら。

/usr/bin/mysql_secure_installation

このコマンドで、セキュアインストレーションが開きます。

  • password for user root は、先ほどログファイルで確認した初期パスワードを入力
  • 'validate_password' plugin というのは、パスワードに設定できる文字列に制限がかけられる
  • 新しいパスワード(&確認)を二回聞かれるのは、 validate password plugin を入れた場合、その制限にあったパスワードかどうかを判定するために再度入力が必要
  • anonymousユーザーの削除
  • リモートサーバーからrootでログインするのを禁止するかどうか
  • テストDBを削除するかどうか
  • priviledgeテーブルをリロードするか

などが聞かれて、一度に設定することができます。

Securing the MySQL server deployment.

Enter password for user root: <初期パスワード>

The existing password for the user account root has expired. Please set a new password.

New password: <新しいパスワード>

Re-enter new password: <新しいパスワードの確認>

The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: <新しいパスワード>

Re-enter new password: <新しいパスワードの確認>

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

おまけ

下記の参考ページを参照し、365日でパスワードを変更するように求められるのをしなくていいように、 my.cnf ファイルに設定を追加しました。

sudo vim /etc/my.cnf

下記を追加。

[mysqld]
character-set-server = utf8
default_password_lifetime = 0

以上。

参考記事:

MySQL 5.7 を CentOS 7 に yum インストールする手順 | WEB ARCH LABO MySQL 5.7 をインストールしたら最初に行うセットアップ | WEB ARCH LABO