LoginSignup
0
0

More than 3 years have passed since last update.

ローカルでLAMP環境を構築しよう(2. MySQLをインストール)

Last updated at Posted at 2020-04-24

はじめに

ローカルでLAMP環境を構築しよう(1)に引き続き、LAMP環境構築を進めます。

前提

過去記事のWindows10にVagrantをを入れてCentOS7をインストールしよう(123456)、
ローカルでLAMP環境を構築しよう(01)で構築した環境を前提とします。

使用ツール

  • Tera Term

手順

1. mariaDBの削除
2. MySQL公式yumリポジトリの追加
3. MySQLインストール
4. MySQL起動・停止
5. MySQLセットアップ

やってみよう

1. mariaDBの削除

CentOS7には、mariaDB(MySQL互換のデータベースサーバー)が
インストールされていることがあるため、
これからインストールするMySQLと競合しないよう以下のコマンドを使用して削除します。

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

2. MySQL公式yumリポジトリの追加

以下のコマンドでCentOS7にMySQLのyumリポジトリを追加します。

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

リポジトリを追加すると、以下のコマンドでパッケージの詳細を確認可能です。。

yum info mysql-community-server

3. MySQLインストール

以下のコマンドでMySQLのインストールを行います。

yum -y install mysql-community-server

インストールしたMySQLののバージョンを確認してみましょう。

mysqld --version

4. MySQL起動・停止

初回起動時は下記コマンドでMySQLの自動起動設定を行います。

systemctl enable mysqld.service

自動起動設定が有効になっているかは下記コマンドで確認可能です。

systemctl is-enabled mysqld.service

下記コマンドでMySQL起動します。

systemctl start mysqld.service

下記コマンドでMySQL停止します。

systemctl stop mysqld.service

5. MySQLセットアップ

初期パスワード確認

MySQL5.7では、初回起動と同時にrootユーザーにランダムな文字列がパスワードとして設定されます。
初期パスワードはログファイル/var/log/mysqld.logに出力されます。

[Note] A temporary password is generated for root@localhost: ada??u+5l&-I

パスワード変更

mysql_secure_installationコマンドを使用して、
パスワードの変更及び最低限のセキュリティ設定を行います。

mysql_secure_installation

mysql_secure_installationを実行すると、下記のように指示が出てきますので、
指示通りyかNを入力していきましょう。
だいたいyでOKです。

初期パスワードを入力しましょう。

Securing the MySQL server deployment.

Enter password for user root:

お好きな新しいパスワードを入力しましょう。
8文字以上、数字、大文字、記号が混ざっているパスワードにしないと怒られます。

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

New password:

再度入力しましょう。

Re-enter new password: 

「validate_passwordプラグインを導入しているので、パスワードの強度を測ってあげるね。」
みたいなことが書いてあるんだと思います。
「入力したパスワードの強度は100だけど、入力した分から変えたいですか?」
と聞かれるので、問題なければyを入力してください。

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

その後、上記で入力した新しいパスワードの確認で2回の入力が要求されます。

New password: 
Re-enter new password: 

入力したパスワードで続けますか?と聞かれるので良ければyと入力してください。

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

「テストやインストールを円滑に行う目的で匿名ユーザーがあるんだけど、
 製造に入る前に匿名ユーザーは削除したほうがいいよ。」と言われますので
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 fo 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) :

「普通rootユーザーのパスワードを外部から知られないように
 rootはlocalhostからしか接続できないようにすべきなのだけど、そうしますか?」
と言われています。ここもyを入力します。

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

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

「デフォルトでテストのために『test』というだれでもアクセスできるデータベースがあり、
 製造に入る前に削除すべきなのだけど、そうしますか?」
と言われています。ここもyを入力します。

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を入力します。

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) :

これで終わりです。

All done!

設定ファイルによる設定

文字コードの設定

/etc/my.cnfmysqld節に以下を追記してください。
これで文字コードが「UTF-8」に設定されます。

character-set-server = utf8

パスワードの有効期限の設定

同じく/etc/my.cnfmysqld節に以下を追記してください。
これでパスワードの有効期限が無期限に設定されます。

default_password_lifetime = 0

MySQLの操作については、過去記事「よく使うMySqlコマンド」が参考になるかと思います。

参考サイト

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

関連ページ

Windows10にVagrantをを入れてCentOS7をインストールしよう

1. VagrantインストールからVagrantfileを設置まで
2. 仮想マシンの操作
3. WinSCP、Tera Termに秘密鍵でログイン
4. WinSCP、Tera Termにrootユーザーでパスワードログイン
5. zip/unzipをインストール
6. Vagrantにて仮想環境を配布

ローカルでLAMP環境を構築しよう

0. 事前準備
1. Apacheをインストール
2. MySQLをインストール
3. PHPをインストール
4. ファイアウォールとか停止する

過去記事

よく使うMySqlコマンド

0
0
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
0
0