0
1

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サーバーにMySQLをインストールし、基本的なCRUD操作をしてみた

Last updated at Posted at 2025-01-01

はじめに

今回は、自宅のVirtualBox上で稼働しているUbuntuにMySQLを導入し、簡単なCRUD(Create, Read, Update, Delete)の操作を実行する方法を紹介します。

Ubuntuは多くの開発者に利用されている人気のLinuxディストリビューションであり、MySQLはアプリケーションで使用される主要なデータベース管理システムです。

本記事では、基本的なインストール手順からデータベースを作成し、CRUD操作を行う方法まで解説します。

前提条件

本記事では以下の環境を前提としています。

・OS:Ubuntu Server
・仮想環境: VirtualBox
・ネットワーク: インターネットに接続可能であること

事前にUbuntu ServerがVirtualBox上で稼働していることを確認してください。

スクリーンショット 2025-01-01 10.38.13.png

知識整理

UbuntuはDebian系のLinuxディストリビューションで、開発者やサーバー環境で広く利用されています。

前回の記事では、VirtualBox上でAlmaLinuxにMySQLを導入する技術検証を行いました。同様の手順を知りたい方は、以下の記事をご参考ください。

UbuntuにおけるMySQLの導入

UbuntuにMySQLを導入する際は、aptパッケージマネージャを使用します。これにより、簡単かつ効率的にMySQLをインストールすることが可能です。

スクリーンショット 2025-01-01 10.50.52.png
引用画像:https://qiita.com/studio_meowtoon/items/613291e1d3c88d3ef12e

MySQLとは?

MySQLはリレーショナルデータベース管理システム(RDBMS)の一つで、構造化データを効率的に管理できます。詳細な解説は以下をご覧ください。

インストール後、データベースを作成し、簡単なCRUD操作(Create, Read, Update, Delete)を実行します!

データベースの作成の流れ

1. MySQLの導入

まず、UbuntuにMySQLをインストールするために、以下のコマンドを実行します。

sudo apt update
sudo apt install mysql-server

次に、MySQLサービスを起動し、OS起動時に自動的に起動するよう設定します。

sudo systemctl start mysql
sudo systemctl enable mysql

2. MySQLの初期設定

MySQLのインストール後に、セキュリティ強化のための初期設定を行います。以下のコマンドを実行してください。

sudo mysql_secure_installation

この設定では、新しいパスワードの設定、匿名ユーザーの削除、テストデータベースの削除などが求められます。指示に従って適切に入力してください。

root@honda-ubuntu:~# sudo 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

Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.

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) :

 ... skipping.
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!

データベースの作成とCRUD操作

1. データベースの作成

まず、MySQLにログインしてデータベースを作成します。

sudo mysql -u root -p

新しいデータベースを作成します(例:test_db)。

CREATE DATABASE test_db;

実行結果の例

mysql> CREATE DATABASE test_db;
Query OK, 1 row affected (0.03 sec)

2. ユーザー作成と権限付与

新しいユーザーを作成し、データベースにアクセスできるように権限を付与します。

CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON test_db.* TO 'test_user'@'localhost';
FLUSH PRIVILEGES;

Query OK と表示されれば正常に完了しています。

3. CRUD操作の実行

ここでは、CRUD(作成、読み取り、更新、削除)の基本操作を試してみます。

テーブル作成

まず、データベースを選択し、users テーブルを作成します。

USE test_db;
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    age INT
);

こちらも、Query OK, 0 rows affected (xxx sec)となることを確認してください。

Create(データの作成)

新しいデータを挿入します。

INSERT INTO users (name, age) VALUES ('Alice', 30), ('Bob', 25);

実行結果の例

mysql> INSERT INTO users (name, age) VALUES ('Alice', 30), ('Bob', 25);
Query OK, 2 rows affected (0.04 sec)
Records: 2  Duplicates: 0  Warnings: 0

Read(データの読み取り)

テーブル内の全データを取得します。

SELECT * FROM users;

実行結果の例

mysql> SELECT * FROM users;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | Alice |   30 |
|  2 | Bob   |   25 |
+----+-------+------+
2 rows in set (0.00 sec)

Update(データの更新)

特定のレコードを更新します。以下の例では、Bobage26 に変更します。

UPDATE users SET age = 26 WHERE name = 'Bob';

実行結果の例

mysql> UPDATE users SET age = 26 WHERE name = 'Bob';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

Delete(データの削除)

特定のレコードを削除します。以下の例では、Alice のレコードを削除します。

DELETE FROM users WHERE name = 'Alice';

実行結果の例

mysql> DELETE FROM users WHERE name = 'Alice';
Query OK, 1 row affected (0.02 sec)

これでCRUD操作の基本的な流れを体験することができました(笑)!

まとめ

この記事では、VirtualBox上で稼働するUbuntuにMySQLをインストールし、基本的なCRUD操作を行う方法を解説しました。

MySQLの導入は比較的シンプルですが、初期設定やセキュリティ対策を確実に行うことが重要です。

また、CRUD操作を通じて、MySQLの基本的な使い方を体験していただけたかと思います。ぜひ、自分の環境でも試してみてください!

参考文献

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?