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

自宅サーバーにデータベースを立ててみた

Posted at

はじめに

SQLの勉強がしたかったので、自宅サーバーにデータベースを立ててみました。

データベースはMySQLを使うこととして、これを自宅サーバにインストールしてみようと思います。

実行環境

  • Ubuntu 24.04.3 LTS
  • MySQL 8.0.43

インストール

使いたいサーバにログインし、以下のコマンドを実行します。

$ sudo apt update
$ sudo apt install mysql-server

これでMySQLがインストールされました。

ユーザの作成

とりあえずユーザを作ります。ローカルで使うだけならrootユーザを用い、さらにパスワードを設定しないのが楽ですが、今回は自宅サーバに立てるということで、外部からログインすることも想定に入れます。そのためのユーザを作ります。

まずはrootユーザでMySQLを開きます。

$ sudo mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2012579
Server version: 8.0.43-0ubuntu0.24.04.1 (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>

以下のコマンドを実行し、guestというユーザをパスワード付きで作成したのち、権限を振ってあげましょう。

mysql> CREATE USER "guest"@"%" IDENTIFIED BY "password"
mysql> GRANT ALL PRIVILEGES ON *.* TO "guest"@"%";

これで設定完了です。*.*の所ですが、特定のデータベース上のみ権限を与えたい場合はdbname.*のようにピリオドの左側にデータベースの名前を入れてあげましょう。

その後、一回Ctrl+DでMySQLを終了し、以下のコマンドで作ったユーザにログインできるか確認しましょう。

$ mysql -u guest -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2012900
Server version: 8.0.43-0ubuntu0.24.04.1 (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コマンドが必要なので、接続元にもMySQLをインストールしておくのを忘れないようにしましょう。

ファイアウォールの設定

MySQLではデフォルトで3306番ポートが利用されます。このポートを開放し、外部からのアクセスを受け付けることができるようにしましょう。

ファイアウォール(ufw)がインストールされていない場合、以下でインストールしましょう。

$ sudo apt update
$ sudo apt install ufw

そして、3306番ポートでの通信を以下のように許可しましょう。許可したらufwを再起動しておきましょう。

$ sudo ufw allow 3306
$ sudo systemctl restart ufw

IPアドレスの制限解除

MySQLではデフォルトで接続元のIPが制限されています。

なので、

以下のコマンドで設定ファイルを編集しましょう。

$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

編集画面が開いたら、以下の行をコメントアウトします。これによってすべてのIPアドレスからアクセスできるようになります。

bind-address = 127.0.0.1

または、以下のようにすると「すべてのIPアドレスを受け付ける」ようになります。

bind-address = 0.0.0.0

もしくは、特定のIPアドレスを指定することもできます。

bind-address = 192.168.0.111

bind-addressは1つしか設定できないようです。以下のように2行書いた場合は最後の行の設定が反映されます。

bind-address = 127.0.0.1
bind-address = 192.168.0.111

接続確認

ここまでできたら、クライアント側で以下のコマンドを実行しましょう。

> mysql -h 10.32.10.83 -P 3306 -u guest -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2012919
Server version: 8.0.43-0ubuntu0.24.04.1 (Ubuntu)

Copyright (c) 2000, 2024, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| services           |
| social             |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

終わりに

自宅にデータベースサーバを作ることができました。

既にこのサーバをDiscord Botなどと連携させてみたりしているのですが、やはりデータベースがあるだけでできることの幅が広がる気がします。

SQLを勉強する環境が欲しかったり、とりあえず何かしらのデータを扱いたいとなった場合はこのようにデータベースサーバを立てるとよいと思います。

それではまた。

参考文献

MySQLをインストール後、ユーザーを作成するまでの手順 - たのしい駆動開発

外部ホストからのMySQL接続 [メモとかメモのようなものとか(By ルーキーの中のひと)]

MySQL Serverに外部から接続する #MySQL - Qiita

同じLANに接続しているPCでローカルのMySQLにリモート接続する方法 #VB.Net - Qiita

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