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?

More than 1 year has passed since last update.

ubuntu(virtual box)のMySQLへリモートクライアントからクエリを発行してみた

Last updated at Posted at 2023-10-02

参照サイト
https://tex2e.github.io/blog/database/mysql-setup
https://wiki.rookie-inc.com/serverapps/db/mysql/connectexternal
https://www.t3a.jp/blog/infrastructure/mysql-start-stop/
https://stackoverflow.com/questions/19101243/error-1130-hy000-host-is-not-allowed-to-connect-to-this-mysql-server

設定(サーバー)

rootでログインする

terminal
sudo mysql -u root

リモート接続可のユーザーを作成する

terminal
CREATE USER 'user'@'%' IDENTIFIED BY '1';
GRANT ALL ON weather.* TO 'user'@'%';
  • ホスト名を%にすることにより、すべてのクライアントから接続できるようになる
  • クライアントip addressを指定することにより、クライアントを制限できる

ユーザーの接続可能リモートホストを確認する

terminal
SELECT host FROM mysql.user WHERE User = 'user';
+-----------+
| host      |
+-----------+
| %         |
| localhost |
+-----------+
2 rows in set (0.01 sec)

mysqld.cnfのバックアップを取る

terminal
cd /etc/mysql/mysql.conf.d
sudo cp mysqld.cnf mysqld.cnf.copy
ls -la
合計 20
drwxr-xr-x 2 root root 4096 10月  2 22:27 .
drwxr-xr-x 4 root root 4096  9月 24 19:12 ..
-rw-r--r-- 1 root root  132  6月 15 04:23 mysql.cnf
-rw-r--r-- 1 root root 2220  6月 15 04:23 mysqld.cnf
-rw-r--r-- 1 root root 2220 10月  2 22:27 mysqld.cnf.copy

mysqld.cnfを編集する

mysqld.cnf(編集前)
bind-address		= 127.0.0.1
mysqlx-bind-address	= 127.0.0.1
mysqld.cnf(編集後)
#bind-address		= 127.0.0.1
#mysqlx-bind-address	= 127.0.0.1
  • bind-addressの設定を削除することにより、サーバー上のすべてのip addressでlistenするようになる

mysqlを再起動する

terminal
systemctl restart mysql

インストール(クライアント)

MySQLクライアントをインストール

terminal
sudo apt update
sudo apt install mysql-client

MySQLサーバーにリモートログインする

terminal
sudo mysql -u user -p weather -h x.x.x.x;

SELECTを実行する

terminal
SELECT * FROM weather WHERE observationDate = '2020-1-1';
+-----------------+------------------+-----------------------+--------------------+--------------------+-------------------+----------------+
| observationDate | observationPoint | observationPrefecture | averageTemperature | highestTemperature | lowestTemperature | rainfallAmount |
+-----------------+------------------+-----------------------+--------------------+--------------------+-------------------+----------------+
| 2020-01-01      | 八丈島           | 東京                  |               10.9 |               13.1 |               9.4 |              0 |
| 2020-01-01      | 八王子           | 東京                  |                  3 |                9.4 |              -1.9 |              0 |
| 2020-01-01      | 大島             | 東京                  |                7.5 |               12.4 |               4.4 |              0 |
| 2020-01-01      | 小河内           | 東京                  |                1.4 |                6.3 |              -1.5 |              0 |
| 2020-01-01      | 東京             | 東京                  |                5.5 |               10.2 |               3.2 |              0 |
| 2020-01-01      | 江戸川臨海       | 東京                  |                  6 |               10.4 |               2.3 |              0 |
| 2020-01-01      | 練馬             | 東京                  |                5.3 |                9.5 |               2.2 |              0 |
+-----------------+------------------+-----------------------+--------------------+--------------------+-------------------+----------------+
7 rows in set (0.00 sec)
  • リモートからsqlを実行できた
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?