ネットワーク繋がる環境にあるんであればsshしろよみたいな話とか、 セキュリティどうのって話もあるんですけど、 「とりあえずローカルからリモートのmysqlにアクセスしたい」 したかった。 した。
mysqlのポート(3306)開けてあるけどなんか接続できない
Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)
neko:~$ sudo netstat -tlpn Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 28703/mysqld tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13414/nginx -g daem tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1266/sshd tcp6 0 0 :::80 :::* LISTEN 13414/nginx -g daem tcp6 0 0 :::22 :::* LISTEN 1266/sshd
となっててmysqlが外からの受付をしてません。 そこで
cd /etc/mysql/mysql.conf.d
neko:/etc/mysql/mysql.conf.d$ sudo vim mysqld.cnf
#bind-address = 127.0.0.1
のように編集
neko:/etc/mysql/mysql.conf.d$ sudo /etc/init.d/mysql restart [ ok ] Restarting mysql (via systemctl): mysql.service. neko:/etc/mysql/mysql.conf.d$ sudo netstat -tlpn Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13414/nginx -g daem tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1266/sshd tcp6 0 0 :::3306 :::* LISTEN 14217/mysqld tcp6 0 0 :::80 :::* LISTEN 13414/nginx -g daem tcp6 0 0 :::22 :::* LISTEN 1266/sshd
とやってやっとこさ接続できるかと思えば Host 'mydomain' is not allowed to connect to this MySQL server
と怒られてしまった….
調べたらmysqlの方でも接続を許可するipを設定しないといけないらしい。 ひとまず以下のコマンドで任意のdatabaseに今接続可能なHostとユーザーがわかるらしい
mysql> use database mysql> select user , host from mysql.user; +------------------+-----------+ | user | host | +------------------+-----------+ | debian-sys-maint | localhost | | mysql.session | localhost | | mysql.sys | localhost | | neko | localhost | | root | localhost | +------------------+-----------+ 5 rows in set (0.00 sec)
そこで、nekoというユーザーからは全てのデータベースに全てのホストから接続できるようにしてみる
mysql> grant all privileges on *.* to [email protected]"%" identified by 'password' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
できた 試しにmacからawsのインスタンスにあるmysqlに接続してみる
たのしい
MacBook-Air:~ $ mysql -h myhost.com -u neko -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. 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>