1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu(WSL)上でMySQLの環境構築を行う

1
Last updated at Posted at 2025-10-13

はじめに

今回はMySQLの環境構築を進めていきます。前提として、WSLの環境は整っているものとします。
WSL、Ubuntuの環境構築が無いという方は、こちらの記事を参考にしてください。

MySQLの環境構築

事前準備

既存のパッケージの更新がないか確認しておきます。

sudo apt update

1. MySQL のインストール

sudo apt install mysql-server
# なんかいろいろパッケージのこととか表示されます

0 upgraded, 30 newly installed, 0 to remove and 29 not upgraded.
Need to get 29.6 MB of archives.
After this operation, 243 MB of additional disk space will be used.
Do you want to continue? [Y/n]

こんだけ容量使うけど続けるかい?と聞かれるので、Y と書いて続行します。

2. バージョン確認

きちんとインストールされたかどうかを、バージョン確認で確かめます。

mysql --version

記事作成段階では、MySQL8.0 がインストールされました。

mysql  Ver 8.0.43-0ubuntu0.24.04.2 for Linux on x86_64 ((Ubuntu))

3. MySQL サーバーの起動/状態の確認

下記のコマンドで、サーバーを立ち上げて、状態を確認できます。systemctl については後述するので、気になる方は見てみて下さい

systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since YYYY-MM-DD #日付などが表示されます
    Process: 8600 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 8609 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 19135)
     Memory: 351.4M (peak: 379.5M)
        CPU: 878ms
     CGroup: /system.slice/mysql.service
             └─8609 /usr/sbin/mysqld

Action: active (runnnig) となっていれば起動できています。

4. MySQL のプロンプトを起動

CLI で MySQL をいじれるよう、プロンプトを起動します。

sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.43-0ubuntu0.24.04.2 (Ubuntu)

Copyright (c) 2000, 2025, 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> に代わっていたら、もう MySQL の中です。

これで完成です! MySQL が触れるようになりました!

データベースを作ってみる

データベースを確認する

データベースのリストを表示するコマンドで、中身を確認していきます。

SHOW DATABASES;
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

デフォルトでいくつか作成されているのがわかるかと思います。

データベースを作成する

CREATE DATABASE sample_database;

CREATE DATABASE <お好きな名前> でデータベースを作ることができます。末尾にセミコロンが必要なので、忘れないように気をつけてください。
できたかどうか確認してみましょう。

SHOW DATABASES;
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sample_database    |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

sample_database が追加されていますね!

削除するときは

DROP DATABASE sample_database;

DROP DATABASE <削除するデータベース名> で消すことができます。稼働しているものを消すと大変なことになるので、気をつけてくださいね。

MySQL プロンプトの終了

quit

こちらで MySQL を出ることができます。

mysql> quit
Bye
hoge@fuga:~/Documents$

帰ってこれましたね。お疲れ様でした!

systemctl コマンドについて

systemctl とは、Linuxシステムでサービスを管理するためのコマンドです。サービスの起動、停止、再起動、状態確認、自動起動設定などを行うことができます。
基本的には、sudo systemctl <やりたいこと> <サービス名> で使用します。

今回は MySQL に絞って解説します。

やりたいこと コマンド
起動 sudo systemctl start mysql
停止 sudo systemctl stop mysql
再起動 sudo systemctl restart mysql
状態確認 sudo systemctl status mysql
OS起動時に自動起動設定 sudo systemctl enable mysql
OS起動時の自動起動をオフ sudo systemctl disable mysql

おわりに

今回は MySQL の環境構築について解説してきました。
本番環境はとてもじゃないですかいじれないので、自分の手元で、自分だけの環境でいろいろ試して学んでいこうと思います。皆さんもぜひ、いろいろ試してみてください!

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?