そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

Windows10(XAMPPインストール済)からサーバ上のMariaDBに接続する手順

※[PR]当ブログの記事の中にはプロモーションが含まれています。

・Windows10(XAMPPでMariaDB(MySQL)をインストール済)から、DBサーバ上のMariaDB(MySQL)に接続したい。
・具体的な手順を教えてほしい。

こういった疑問に答えます。

本記事の内容

  1. Windows10(XAMPPでMariaDB(MySQL)をインストール済)から、DBサーバ上のMariaDB(MySQL)に接続するための設定

サトナカ (@souiunogaii)

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

こういった私が、解説していきます。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

Windows10(XAMPPでMariaDBをインストール済)から、DBサーバ上のMariaDBに接続するための設定

前提事項

Windows10端末には、XAMPPでMariaDBが導入済み。
DBサーバ側はCentOS8で、MariaDBがインストール済みの状態です。

souiunogaii.hatenablog.com

souiunogaii.hatenablog.com

Windows10から接続しようとしたらエラー

mysql -h sv-neptune.planet.space.com -u root -p●●●●●●●●
c:\xampp\mysql>cd bin

c:\xampp\mysql\bin>mysql -h sv-neptune.planet.space.com -u root -p●●●●●●●●
ERROR 1045 (28000): Access denied for user 'root'@'192.168.1.101' (using password: YES)

Access denied for user 'root'@'192.168.1.101' というエラーになってしまいました。

DBサーバ側のMariaDBの設定を確認

ポートの確認

netstat -tlpn | grep mysql
$ sudo netstat -tlpn | grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      1313/mysqld

TCPポート3306がLISTEN状態になっています。

show variables like 'port';
$ mysql -uroot -p●●●●●●●●
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5979
Server version: 10.3.27-MariaDB MariaDB Server

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 [(none)]> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.001 sec)

ユーザーの確認

select user, host from user;
MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> select user, host from user;
+--------------+---------------+
| user         | host          |
+--------------+---------------+
| root         | 127.0.0.1     |
| root         | 192.168.1.107 |
| root         | ::1           |
| root         | localhost     |
| wpdb01admin  | localhost     |
+--------------+---------------+
5 rows in set (0.000 sec)

rootユーザーのhostには、Windows10端末のIPアドレス192.168.1.101が未設定なので、追加が必要です。

MariaDBのユーザーを追加

GRANT ALL PRIVILEGES ON *.* TO root@'192.168.1.101' IDENTIFIED BY '●●●●●●●●' WITH GRANT OPTION;
MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO root@'192.168.1.101' IDENTIFIED BY '●●●●●●●●' WITH GRANT OPTION;
Query OK, 0 rows affected (0.001 sec)

MariaDB [mysql]> select user, host from user;
+--------------+---------------+
| user         | host          |
+--------------+---------------+
| root         | 127.0.0.1     |
| root         | 192.168.1.101 |
| root         | 192.168.1.107 |
| root         | ::1           |
| root         | localhost     |
| wpdb01admin  | localhost     |
+--------------+---------------+
6 rows in set (0.000 sec)

Windows10端末から再度、接続できるかテスト

mysql -h sv-neptune.planet.space.com -u root -pmariadbadmin83
c:\xampp\mysql\bin>mysql -h sv-neptune.planet.space.com -u root -p●●●●●●●●
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5981
Server version: 10.3.27-MariaDB MariaDB Server

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 [(none)]> 

Windows10端末から、DBサーバのMariaDBに接続できるようになりました。