データベース · 3 min read · Sep 26, 2025
Ubuntu 22.04にMySQL 8をインストールして使用する方法

MySQLは、Oracle Cloudによって提供される無料のオープンソースのリレーショナルデータベース管理プラットフォームです。その信頼性、迅速な処理、使いやすさ、柔軟性から非常に人気があります。MySQLは、データベースのコンテンツを追加、アクセス、管理するために構造化クエリ言語を使用します。MySQL 8.0は、InnoDBと呼ばれる実績のあるトランザクショナルストレージエンジンにメタデータを保存します。クライアント/サーバーアーキテクチャで動作し、Ubuntu、Windows、CentOS、Debianを含むすべての主要なオペレーティングシステムにインストールできます。
このチュートリアルでは、Ubuntu 22.04サーバーにMySQL 8をインストールする方法を示します。
前提条件
- Ubuntu 22.04を実行しているサーバー。
- サーバーにルートパスワードが設定されていること。
始めに
まず、次のコマンドを実行してすべてのシステムパッケージを最新バージョンに更新およびアップグレードします:
apt update -y
apt upgrade -yすべてのパッケージが更新されたら、次のステップに進むことができます。
Ubuntu 22.04にMySQL 8をインストールする
デフォルトでは、最新のMySQLサーバーのバージョンはUbuntuのデフォルトリポジトリに含まれています。次のコマンドを実行してインストールできます:
apt install mysql-server -yMySQLサーバーがインストールされたら、次のコマンドを使用してMySQLのバージョンを確認できます:
mysql --version次の出力にMySQLのバージョンが表示されるはずです:
mysql Ver 8.0.30-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))
すべてのパッケージが更新されたら、次のステップに進むことができます。
MySQLサービスの管理
デフォルトでは、MySQLサービスはsystemdによって管理されています。systemctlコマンドを使用して、MySQLを簡単に開始、停止、およびステータスを確認できます。
MySQLサービスを開始するには、次のコマンドを実行します:
systemctl start mysqlMySQLサービスを停止するには、次のコマンドを実行します:
systemctl stop mysql次のコマンドを使用してMySQLサービスのステータスを確認できます:
systemctl status mysql次の出力が表示されるはずです:
? mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2022-08-21 12:47:24 UTC; 28s ago
Process: 26157 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 26185 (mysqld)
Status: "Server is operational"
Tasks: 41 (limit: 2242)
Memory: 359.8M
CPU: 1.383s
CGroup: /system.slice/mysql.service
??26185 /usr/sbin/mysqld
Aug 21 12:47:23 ubuntu2204 systemd[1]: Starting MySQL Community Server...
Aug 21 12:47:24 ubuntu2204 systemd[1]: Started MySQL Community Server.
デフォルトでは、MySQLはポート3306でリッスンします。次のコマンドで確認できます:
ss -antpl | grep -i mysql次の出力にMySQLのリッスンポートが表示されるはずです:
LISTEN 0 70 127.0.0.1:33060 0.0.0.0:* users:(("mysqld",pid=26185,fd=21))
LISTEN 0 151 127.0.0.1:3306 0.0.0.0:* users:(("mysqld",pid=26185,fd=23))
すべてのパッケージが更新されたら、次のステップに進むことができます。
MySQLインストールのセキュリティ
次に、mysql_secure_installationスクリプトを実行して、新しいMySQLルートパスワードの設定、匿名ユーザーの削除、リモートログインの無効化などの追加のセキュリティ機能を有効にすることは常に良いアイデアです。
mysql_secure_installation以下のようにすべての質問に答えてください:
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
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
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.以下のように新しいパスワードを設定するように求められます:
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.Yを入力してEnterキーを押して匿名ユーザーを削除します。
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.Yを入力してEnterキーを押してリモートルートログインを無効にします。
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) : YYを入力してEnterキーを押してテストデータベースを削除します。
- 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! 完了したら、次のステップに進むことができます。
MySQLルートパスワードを設定する
デフォルトでは、MySQLルートパスワードは設定されていません。設定するには、MySQLシェルに接続します:
mysqlMySQLシェルに接続したら、次のコマンドを使用してMySQLパスワードを設定します:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'securepassword';次に、次のコマンドを実行して変更を保存します:
mysql> FLUSH PRIVILEGES;次に、次のコマンドを使用してMySQLシェルから退出します:
mysql> EXIT;次に、ルートパスワードを確認するためにMySQLシェルに再度ログインします:
mysql -u root -pログインすると、以下のようにMySQLシェルに入ります:
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.30-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> 完了したら、次のステップに進むことができます。
MySQLでデータベースとユーザーを作成する
次のコマンドを使用してdb1という名前のデータベースを作成しましょう:
mysql> CREATE DATABASE db1;次のコマンドを使用して作成したデータベースを確認できます:
mysql> SHOW databases;次の出力が得られます:
+--------------------+
| Database |
+--------------------+
| db1 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
db1にデータベースを変更するには、次のコマンドを実行します:
mysql> USE db1;dbuserという名前のユーザーを作成するには、次のコマンドを実行します:
mysql> CREATE USER 'dbuser'@'%' IDENTIFIED BY 'password';すべてのデータベースに対してdbuserにすべての権限を付与するには、次のコマンドを実行します:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'%' WITH GRANT OPTION;変更を保存するには、次のコマンドを実行します:
mysql> FLUSH PRIVILEGES;次のコマンドを使用してMySQLシェルから退出できます:
mysql> EXIT;完了したら、次のステップに進むことができます。
MySQLサーバーをアンインストールする
サーバーからMySQLサーバーを削除したい場合は、次のコマンドを実行します:
apt remove mysql-server --purge次に、次のコマンドを使用して不要なパッケージをすべて削除します:
apt autoremove結論
この記事では、Ubuntu 22.04にMySQL 8をインストールする方法を学びました。また、MySQLサービスの管理、データベースとユーザーの作成方法も学びました。これで、MySQLで新しいデータベースとデータベースユーザーを作成し始めることができます。
新しい投稿を受信箱で受け取る
スパムはありません。いつでも購読を解除できます。