LAMP環境構築マニュアル ③ MySQL(8 or 5.7) インストール編
目次
MySQLデータベースのセットアップ
前回の内容でLAMP環境のためのWebサーバー、Apacheがセットアップできたら
次はRDBMS(リレーショナルデータベース管理システム)としてMySQLをセットアップしていきます。
環境: 「Conoha VPS」メモリ 512MB/CPU 1Coreプラン、CentOS 7.6.1810 (Core)、MySQL 8.0.19 for Linux on x86_64
LAMPとは… 以下の要素の頭文字で構成するサーバー環境のこと
- Linux
- Apache
- MySQL (今ここ)
- PHP
SSHクライアントからサーバー接続して下記の手順を実行していきます。
MySQL をインストール
本記事執筆時点、最新のMySQLバージョン8をインストールします。
mysql公式サイトのyumリポジトリページへアクセス(こちら)
1. Red Hat Enterprise Linux 7 / Oracle Linux 7のDownloadリンクを開く
2. rpmパッケージのリンクをコピーする。
No thanks, just start my download. と書かれた部分を右クリックで開き、リンクのアドレスをコピーをクリックする。
MySQLのインストールに必要なrpmパッケージのリンクが以下のURLでコピーされる
https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
3. rpmパッケージをインストールする。
2でコピーしたURLを yum install コマンドへ指定して実行し、
rpmパッケージをダウンロードします。
※実行途中にy(Yes)を選択するのが面倒であればコマンドに-yオプションを指定してスキップします。
1 |
$ sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm |
以下のように表示されればOK。
この時点ではパッケージをインストールしただけで、MySQL自体のインストールは完了していません。
1 2 3 4 |
インストール: mysql80-community-release.noarch 0:el7-3 完了しました! |
4. MySQLのインストールを行う。
1 |
$ sudo yum install mysql-community-server |
インストールが完了したら以下のコマンドでMySQLのバージョン確認を行います。
1 |
$ mysql --version |
1 |
mysql Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL) |
MySQL バージョン5.7の場合
rpmパッケージのインストールURL
http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
1 |
sudo yum -y install http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm |
パッケージのダウンロードが完了したらMySQLをインストール
sudo yum install mysql-community-server
MySQLを起動させて初期設定を行う
※サービス名はmysqlではなく、mysqld
1. 起動
1 |
$ sudo systemctl start mysqld.service |
2. MySQLの状態を確認する
1 |
$ sudo systemctl status mysqld.service |
active (running)で起動中
3. サーバー起動時に自動起動するようにしておく
1 |
$ sudo systemctl enable mysqld.service |
rootの初期パスワードを確認する
MySQLの管理者ユーザである※rootの初期パスワードを確認しておきます。
※(注意)名前が同じですが、MySQL独自のアカウントでありサーバー本体のrootとは異なります。
1. 以下のコマンドを実行
1 |
$ sudo cat /var/log/mysqld.log | grep root |
下記のように表示され、root@localhost: の後の文字列にrootの初期パスワードが表示される。
1 |
2020-02-16T05:47:06.020817Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: XXXXXXXXXX |
MySQLの基本的なセキュリティ設定を行う
コマンド mysql_secure_installation を実行
1 |
$ mysql_secure_installation |
コマンドを実行すると先程確認しておいたrootの初期パスワードの入力を求められ、
下記の内容が対話形式で行なわれていきます。
基本的に全てy(Yes)を押していけばOKです。(セキュリティ設定)
- 「Change the password for root ?」 ※rootのパスワードを初期設定から変更する(英大文字、英小文字・数字・記号を含む8文字以上)
- 「Remove anonymous users?」 匿名ユーザーアカウントを削除する(ユーザー名やパスワードなしでログインできるアカウント)
- 「Disallow root login remotely?」 リモート(外部)からrootログイン出来ないようにする
- 「Remove test database and access to it?」 test database(テスト用データベース)の削除
- 「Reload privilege tables now?」 設定を反映する
※パスワードは最低でも4回正しく入力することになっています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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' component is installed on the server. The subsequent steps will run with the existing configuration of the component. 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! |
All done! で上記は完了です。
MySQLへ接続(ログイン)出来るか確認する。
以下のコマンドを実行し、rootユーザーでMySQLへの接続確認を行います。
1 |
$ mysql -u root -p |
Enter password: と表示されたら、先ほど初期から変更したrootパスワードを入力してEnter
以下のように表示されたら、MySQLへの接続が完了です。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.19 Copyright (c) 2000, 2020, 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> |
(接続を切る場合は exit + Enter)
MySQLのセットアップが出来たら次回は開発言語のPHPを導入します。
参考: CentOS7にMySQL5.7をインストール&ログイン
関連記事
“LAMP環境構築マニュアル ③ MySQL(8 or 5.7) インストール編” に対して1件のコメントがあります。
コメントは受け付けていません。