目次
MySQLについて
MySQL(マイ エスキューエル)は、オープンソースのリレーショナルデータベース管理システム(RDBMS)です。
その名前は、共同設立者のMichael Wideniusの娘の名前である「My」と、Structured Query Languageの略称である「SQL」を組み合わせたものらしいのです。
多くの企業や一般の方が利用するデータベースシステムで、本ブログでも解説しているWordpressでも記事や設定データの管理で利用しています。
構築環境
- CentOS Linux release 8.2.2004 (Core)
- mysql Ver 8.0.21
ココがポイント
WEBサーバーやDNSサーバー、メールサーバー等を構築する際は、ルータで各サービスに利用するポートの開放を行う必要がありますが、MySQL等のデータベースはPHP等で組まれたAPIからアクセスされる事が多いため、外部NWからクライアントが直接DBにアクセスすることは基本的にはありません。そのためセキュリティー強化のため、MySQLで使われているポート番号は(3306)は開放しないようにしましょう!
データベースに外部からアクセスする場合は、一度Linuxサーバー等の踏み台サーバーにSSHで接続した後、MySQLにアクセスするようにします。
MySQLのインストール
▼最新版のMySQLのバージョンを調べる
CentOS 8 では dnf コマンドを利用してパッケージ情報検索やインストール、削除を行うことができます。
パッケージファイルが管理されているダウンロードサーバーをリポジトリと呼び、新規でリポジトリを追加することで、ダウンロードできるファイルの種類を増やすことが可能です。
command # dnf info mysql メタデータの期限切れの最終確認: 0:14:20 時間前の 2020年09月27日 16時22分59秒 に実施しました。 利用可能なパッケージ 名前 : mysql バージョン : 8.0.21 :
▼インストール実施
「-y」オプションを指定すると、インストール可否の確認で「yes」となり自動的にインストールが開始します。
MySQLは依存関係のあるパッケージを多く必要としますが、dnfコマンド実行時はそれらパッケージも自動でインストールしてくれます。
command # dnf install mysql-server
MySQLのコンフィグの設定
コンフィグを書く場合は my.cnf に直接ではなく下記のフォルダのファイルを編集するか新規ファイルを下記ディレクトリに設置します。
my.cnfは下記ディレクトリをインクルードしているので任意のファイル名で作成しても問題ありません。
設置後はmysqldの再起動で反映されます。
command # ls -lrt /etc/my.cnf.d 合計 12 -rw-r--r-- 1 root root 295 5月 14 2019 client.cnf -rw-r--r-- 1 root root 565 9月 15 23:56 mysql-default-authentication-plugin.cnf -rw-r--r-- 1 root root 639 10月 6 03:14 mysql-server.cnf
▼mysql serverの文字コードをUTF-8にする
command # vi /etc/my.cnf.d/mysql-server.cnf
mysql-server.cnf
▼下記の設定を一番下の行あたりに追記
character-set-server=utf8
MySQLのサービスを構成
▼MySQLの自動起動設定
mysqld.serviceという名前のsystemdサービスとして実行するようにMySQLを構成。
command # systemctl start mysqld.service Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
MySQLの起動設定
▼MySQLの起動
command # systemctl start mysqld
▼MySQLの起動確認
command # systemctl status mysqld ● mysqld.service - MySQL 8.0 database server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled) Active: active (running) since Sun 2020-09-27 17:02:19 +03; 27s ago
最後までエラーが無く Active: active (running) となっていればOK。
▼MySQLの自動起動設定
command # systemctl enable mysqld Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
▼自動起動の状態確認
command # systemctl is-enabled mysqld enabled
enabled になっていればサーバー再起動で自動で敵にサービスが起動します。
MySQLのセキュリティ設定
mysql コマンドを利用してパスワード等のセキュリティ設定を行います。
command # mysql_secure_installation
mysql_secure_installation実行後の標準出力
▼パスワードの強度をチェックしてくれるプラグインのセットアップするかどうかの質問。
「y」を入力してセットアップを実施します。
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
▼パスワードのチェックを選択。
ここは任意で選択。
LOW:8文字以上
MEDIUM:8文字以上 + 数字・アルファベットの大文字と小文字・特殊文字を含む
STRONG:8文字以上 + 数字・アルファベットの大文字と小文字・特殊文字を含む + 辞書ファイルでのチェック
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.
▼パスワードチェックのルールに基づいて新しいパスワードを2回入力します。
Please set the password for root here.
New password: ******
Re-enter new password: ******
2回目の入力が完了するとパスワードの強度が数値で表示されます。
パスワードの強度が高ければ「100」に近づきます。
このパスワードで確定して良いか聞かれるので問題がなければ「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
▼匿名(anonymous)ユーザーの設定
削除するか聞かれるので「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.」が表示されれば削除完了。
Success.
▼リモートログインの設定
リモート接続時に root でログインができないようにするか聞かれるので「y」を入力します。
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.」が表示されれば削除完了。
Success.
▼testデータベースの削除
MySQLインストール時に作成される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
▼testデータベースが削除(drop)されます
- Dropping test database...
Success.
▼testデータベースの削除と同時に権限も削除されます
- Removing privileges on test database...
Success.
▼権限テーブルの有効化
権限テーブルをリロードして変更結果を有効にするか聞かれるので「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) : y
▼「Success.」が表示されればリロード完了。
Success.
▼「All done!」が表示で設定が完了。
All done!
MySQLのセキュリティ設定の確認
▼設定したパスワードでログイン
command $ mysql -u root -p Enter password: 先ほど設定したパスワード Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.21 Source distribution 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>
▼データベース「test」がなくなっているのを確認
command mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)
▼quitでMySQLからログアウト
command mysql> quit Bye [username@localhost ~]$
MySQLをアンインストールする場合の補足説明
パッケージをアンインストールしてもDBデータは残るため、データも含めて完全削除を行う場合は、「mysql」ディレクトリを削除します。
command # rm -rf /var/lib/mysql
ココに注意
データベースの削除は慎重に行ってください。
削除する前にmysqldumpでバックアップ取ることをお勧めします。
mysqldumpの使い方は こちら