LoginSignup
3
2

More than 3 years have passed since last update.

Conoha で web サーバーと db サーバーをプライベートネットワークに構築する(DBサーバーは使わない)

Last updated at Posted at 2020-08-09

Conoha の DB サーバーは utf8mb4 に対応していません。
そのため、VPS を2つ用いて web サーバーと db サーバーの分離を行いたいと思います。

サーバーの作成

Ubuntu 18.04 で作ります

プライベートネットワークの作成

公式の使い方を参考に作成

コンソール画面からはネットワークインターフェイスの追加のみが行われるため、追加の設定は ssh してサーバー内で行う必要があります。

ネットワークの設定

$ cd /etc/netplan
$ vim 11-localnetwork.yaml # ファイル名はなんでもいいが、*.yaml でないといけないという噂を聞いた

そして以下のようにします。

11-localnetwork.yaml
network:
    version: 2
    renderer: networkd
    ethernets:
        eth1:
            addresses: [192.168.0.1/24] # Conohaコンソールのプライベートネットワーク画面で表示されているIP
            dhcp4: true
            dhcp6: false
            accept-ra: false
            optional: true

$ ip a を叩いて、eth1にIPが振られていたら設定完了です。

これを web サーバーと db サーバーの両方で行います。

それが完了したらお互いの local ip に向けて ping が通るはず。

mysql のインストール

# db サーバー
$ apt install mysql-server mysql-client

# web サーバー
$ apt install mysql-client

mysql のユーザー作成

db.server
mysql > GRANT ALL ON *.* TO remote@[web サーバーのIP] identified by 'pass';

設定変更

アクセスできる IP を指定します。
0.0.0.0 だと全てのホストからの接続を許可してしまうのですが、これは Firewall との組み合わせで絞り込んでいこうと思います。

$ vim /etc/mysql/mysql.conf.d/mysqld.cnf
mysqld.cnf
...
#bind-address            = 127.0.0.1
bind-address            = 0.0.0.0
...

$ systemctl restart mysql mysql の再起動

Firewall 設定

こんな感じになるように設定していきます。

$ ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
3306                       ALLOW       192.168.0.0/24
22/tcp (v6)                ALLOW       Anywhere (v6)
$ ufw default deny
$ ufw allow ssh
$ ufw allow from 192.168.0.1/24 to any port 3306
$ ufw reload

以上で web サーバー側から以下のように接続することができるようになります。

$  mysql -uremote -h192.168.0.2 -ppass
3
2
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
3
2