本トピックでは、Linux 環境で MySQL データベースをインストールして設定する方法について説明します。
操作手順
Linux サーバーのターミナルにログインします。
説明本トピックでは Ubuntu 18.04 を例として使用します。他の Linux ディストリビューションでは手順が異なる場合があります。
ソフトウェアリポジトリを更新し、wget をインストールします。
sudo apt update
sudo apt install wget
MySQL インストールパッケージをダウンロードします。
wget https://dev.mysql.com/get/mysql-apt-config_0.8.16-1_all.deb -y
説明wget がインストールされていない場合は、apt install wget を実行してインストールしてください。
.deb パッケージを使用してソフトウェアリポジトリを更新します。
sudo dpkg -i mysql-apt-config_0.8.16-1_all.deb
表示されるダイアログボックスで、[MySQL Server & Cluster (Currently selected: mysql-5.7)] を選択し、[Ok] を選択します。

インストール可能なソフトウェアパッケージのすべてのバージョンを照会します。
sudo apt update
sudo apt-cache policy mysql-server
mysql-server: Installed: (none) Candidate: 5.7.32-1ubuntu18.04 Version table: ...出力の 5.7.32-1ubuntu18.04 は、インストール可能な MySQL データベースのバージョンです。
取得したバージョンに基づいて MySQL 5.7 をインストールします。インストール中に、root ユーザーのパスワードを設定し、パスワードを機密に保つ必要があります。
sudo apt install -f mysql-client=5.7.32-1ubuntu18.04 mysql-community-server=5.7.32-1ubuntu18.04 mysql-server=5.7.32-1ubuntu18.04
MySQL データベースを設定します。
定型セキュリティを設定します。
sudo mysql_secure_installation
以下のように設定を構成します。
Enter current password for root (enter for none): <Enter password> VALIDATE PASSWORD PLUGIN 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 plugin? Press y|Y for Yes, any other key for No: Y 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 Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1 Using existing password for root. Estimated strength of the password: 25 Change the password for root ? ((Press y|Y for Yes, any other key for No) : d Remove anonymous users? [Y/n] Y Disallow root login remotely? [Y/n] Y Remove test database and access to it? [Y/n] Y Reload privilege tables now? [Y/n] Yリモートアクセス機能を有効にします。
MySQL データベースの設定ファイルを編集します。
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
I キーを押し、bind-address の値を 0.0.0.0 に変更します。
Esc キーを押し、:wq! と入力してファイルを保存し、終了します。
MySQL データベースを再起動して設定を有効にします。
sudo systemctl restart mysql
MySQL データベースが正常に動作するかどうかを確認します。
root ユーザーとして MySQL データベースにログインします。
mysql -u root -p
MySQL データベースコンソールで既存のデータベースを照会します。
show databases;
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)上記のクエリ結果は、既存の MySQL データベースが正常に動作していることを示します。