$ curl -v telnet://<IPアドレス>:3306
* Trying <IPアドレス>:3306...
* connect to <IPアドレス> port 3306 failed: Connection refused
* Failed to connect to <IPアドレス> port 3306: Connection refused
* Closing connection 0
curl: (7) Failed to connect to <IPアドレス> port 3306: Connection refused
mysql サーバーから確認します。ポート 3306 はリッスンされていますが、127.0.0.1 (localhost) に制限されています。
$ ss -atn | egrep "State|3306"
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 70 127.0.0.1:33060 0.0.0.0:*
LISTEN 0 151 127.0.0.1:3306 0.0.0.0:*
外部からの接続を許可する方法
bind-address の設定を変更する
mysql サーバーにおいて mysqld.cnf を編集し、bind-address の設定を変更します。bind-address のエントリをコメントアウトするか、bind-address に許可するIPアドレスを指定します。bind-address は単一のアドレスやカンマ区切りが使用できます。詳細は、こちらのドキュメントを参照ください。
** 変更前 **
$ cat /etc/mysql/mysql.conf.d/mysqld.cnf
...
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
** 変更後 **
...
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1
$ systemctl status mysql
$ sudo systemctl restart mysql
$ systemctl status mysql
mysql サーバーから確認します。ポート 3306 はリッスンされ、特にIPアドレスは制限されていません。
$ ss -atn | egrep "State|3306"
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 70 127.0.0.1:33060 0.0.0.0:*
LISTEN 0 151 *:3306 *:*
$ curl -v telnet://<IPアドレス>:3306
* Trying <IPアドレス>:3306...
* Connected to <IPアドレス> (<IPアドレス>) port 3306 (#0)
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: " to save to a file.
* Failure writing output to destination
* Closing connection 0
DBユーザーに接続元のIP アドレスを許可する
user テーブルに、接続元の host (mysql クライアント) が登録されていなければ、以下のエラーが出力されます。接続元の host は、例として192.168.1.100 のIPアドレスを持ちます。
$ mysql -h <IPアドレス> -u dbadmin -p
Enter password:
ERROR 1130 (HY000): Host 'ip-192-168-1-100.ap-northeast-1.compute.internal' is not allowed to connect to this MySQL server
mysql サーバーにおいて、接続元 host のIPアドレス、あるいは host が所属するサブネットワークのアドレスを指定して、ユーザーを作成、権限を付与します。(例: dbadmin / dbadmin-password)
mysql> CREATE USER 'dbadmin'@'192.168.1.%' IDENTIFIED BY 'dbadmin-password';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL ON * . * TO 'dbadmin'@'192.168.1.%';
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT host FROM mysql.user WHERE user = "dbadmin";
+-------------+
| host |
+-------------+
| 192.168.1.% |
| localhost |
+-------------+
2 rows in set (0.00 sec)
$ mysql -h <IPアドレス> -u dbadmin -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.35-0ubuntu0.22.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>