LoginSignup
0
0

はじめてMySQLサーバーにリモート接続してみた

Last updated at Posted at 2024-05-30

環境

  • Windows 11
  • MySQL 8.0
  • Ubuntu 22.04

はじめに

わたしは何回かごっちゃになってしまったので念のため書いておきますが、

  • ホスト: 接続される側
  • クライアント: 接続する側

です。忘れないように!(自戒)

準備

MySQLがWindowsにダウンロードされていて、PATHが通っている状態から始めます。
コマンドプロンプトで以下コマンドを入力し、このように出れば準備おっけー◎

cmd
C:\Users\user> mysql --version
mysql Ver 8.0.37 for Win64 on x86_64 (MySQL Community Server - GPL)

出来ていなければ、Windows MySQL ダウンロード とかで調べましょう(゜-゜)

ホスト側MySQLの設定

では早速ホストのMySQLの設定をしていきます。

まず、MySQLサーバーのディレクトリ内の、my.iniというファイルを見ていきます。
ファイルの場所は、C:\ProgramData\MySQL\MySQL Server 8.0\my.iniです。

これ、管理者権限で開かないと変更できないので注意!

① コマンドプロンプトを管理者として実行する

image.png

my.iniを開く

開いたコマンドプロンプトで、こんな感じでファイルの名前をうつと開いて編集できるようになってます。

cmd
C:\ProgramdData\MySQL\MySQL Server 8.0> my.ini

開いたファイルに、bind-address = 127.0.0.1という文字列があったら、コメントアウトします。
なければそのままでおっけー。

my.ini
# bind-address = 127.0.0.1

こんな感じで#を文頭に書けばコメントアウトできます。
編集したらしっかり保存しておきましょう。

③ MySQLサーバーの再起動

さっきと同じ、管理者権限で開いたコマンドプロンプトにコマンドを打って、こうなればおけい!

cmd
C:\Windows\System32> net stop mysql
MySQL サービスを停止中です.
MySQL サービスは正常に停止されました。

C:\Windows\System32> net start mysql
MySQL サービスを開始します.
MySQL サービスは正常に開始されました。

無事再起動できました。

④ MySQLにログイン
cmd
C:\Users\user> mysql -u root -p
Enter password: *************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.37 MySQL Community Server - GPL

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>
⑤ リモート接続用のアカウントを作成、権限付与
sql
mysql> CREATE USER remoteuser@'%' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.01 sec)

remoteuserpasswordはお好みで変えてください。

チャンとアカウントが作られたか確認してみましょう。

sql
mysql> SELECT user,host From mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| remoteuser       | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
| seed             | localhost |
+------------------+-----------+
6 rows in set (0.00 sec)

こんな感じで、一番上に先ほど作ったユーザーが出来ていればおっけー◎

つぎに、このユーザーに権限を付与します。
今回はいったんすべての権限を付与しますが、適宜変更してくださいまし。

sql
mysql> GRANT ALL PRIVILEGES ON * . * TO 'remoteuser'@'%';
Query OK, 0 rows affected (0.01 sec)

設定が終わったら、一度リロードして変更を反映させます。

sql
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
⑥ ログイン確認

では、先ほど作ったユーザーでログインできるか一度試してみましょう。
一度ログアウトして、

sql
mysql> \q
Bye

さっきのユーザーでログインします。

cmd
C:\Users\user> mysql -u remoteuser -p
Enter password: **********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.37 MySQL Community Server - GPL

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でやることは終わりです。

Windows ファイヤーウォールの設定

コントロールパネル→システムとセキュリティ→Windows Defender ファイヤウォール→詳細設定
の順に進みます。

① 開かれたウィンドウの、左側のメニューから「受診の規則」を選択。

image.png

② 右側のメニューから「新しい規則…」を選択。

image.png

③ 規則の種類 → ポート

image.png

④ プロトコル及びポート → TCP 特定のローカルポート: 3306

image.png

⑤ 操作:接続を許可する

image.png

⑥プロファイル:すべてチェック

image.png

⑦ 名前:適当(mysqlとか)

image.png

これで完了ボタンを押せばOKです。

IPアドレス確認

ここで、自分のIPアドレスを調べておきましょう。
コマンドプロンプトで調べることが出来ます。

cmd
C:\Users\user> ipconfig

Windows IP 構成


イーサネット アダプター イーサネット:

   接続固有の DNS サフィックス . . . . .: ************
   IPv6 アドレス . . . . . . . . . . . .: ****:****:****:****:****:****:****:****
   一時 IPv6 アドレス. . . . . . . . . .: ****:****:****:****:****:****:****:****
   リンクローカル IPv6 アドレス. . . . .: ****::****:****:****:****
   IPv4 アドレス . . . . . . . . . . . .: ***.***.*.**
   サブネット マスク . . . . . . . . . .: ***.***.***.*
   デフォルト ゲートウェイ . . . . . . .: ****::****:****:****
                                          ***.***.*.*

Wireless LAN adapter Wi-Fi:
...

こんな感じで出てくるので、この「IPv4アドレス」の箇所をメモしておきましょう。

やっとクライアント側操作です

では、いよいよ別のパソコン、もしくは別のOSなどからリモート接続してみます。
クライアントは、他のパソコンでもいいし、LinuxとかいれてればそれでもOKです。
私は今回Linuxから接続しました。

まずMySQLがダウンロードされていることを確認してください。

Ubuntu
user@PC:~$ mysql --version
mysql  Ver 8.0.36-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))

こんな感じになればおっけい◎

では早速ログインです。
この部分を変更して以下のコマンドを入力。

  • remoteuser: ユーザー名
  • ***.***.*.**: IPアドレス
Ubuntu
user@PC:~$ sudo mysql -u remoteuser -h ***.***.*.** -P 3306 -p

sudoのパスワードが聞かれたらLinuxのパスワードを入力します。
(文字打っても表示されないのでわかりづらい!怒)

Ubuntu
[sudo] user のパスワード: (Linuxのパスワード入力)

正しいパスワードが入力できると、MySQLのパスワード入力を求められますので、
さっき設定したパスワードを入力します。

Ubuntu
Enter password: **********(先ほど設定したremoteuserのパスワード)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.37 MySQL Community Server - GPL

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>

こんな感じで接続できました!やった~!❤

おきもち(?)

自力で何とか接続できた時の喜び、はんぱない!
勉強できてよかった。
サーバーとか全然何もわからん状態なので、これからも頑張って勉強していきたい

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