こちらのページを参考にしました。
Host xxx is not allowed to connect to this MySQL server の対応
次が必要です。
- サーバーの設定
- 外部から接続できるユーザーの作成
サーバーの設定
/etc/mysql/mariadb.conf.d/50-server.cnf
(省略)
# bind-address = 127.0.0.1
(省略)
MariaDB の再起動
sudo systemctl restart mariadb
外部から接続できるユーザーの作成
次の状況では、外部から接続できません。
MariaDB [(none)]> select user, host from mysql.user;
+-------------+-----------+
| User | Host |
+-------------+-----------+
| mariadb.sys | localhost |
| mysql | localhost |
| root | localhost |
| scott | localhost |
+-------------+-----------+
4 rows in set (0.001 sec)
次の状況では、外部から接続ができます。
MariaDB [(none)]> select user, host from mysql.user;
+-------------+-----------+
| User | Host |
+-------------+-----------+
| scott | % |
| mariadb.sys | localhost |
| mysql | localhost |
| root | localhost |
| scott | localhost |
+-------------+-----------+
5 rows in set (0.002 sec)
ユーザーの作成方法
CREATE USER 'scott' identified by 'tiger123';
GRANT ALL PRIVILEGES ON *.* TO 'scott'@'%' with grant option;
外部から接続
$ mysql -uscott -ptiger123 city -h 10.39.166.1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 36
Server version: 10.11.2-MariaDB-1-log Ubuntu 23.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [city]>