Quantcast
Channel: omnioo lab. record » PHP/MySQL
Viewing all articles
Browse latest Browse all 59

MySQLを外部ホストで稼働させる設定

$
0
0

外部ホストというかリモートホストにMySQLサーバーを作る時、なんだかよくわからないけどいろいろ手間です。まず何よりも素直に繋がらない。セキュリティー的に云々の世界の話しなのでそれはそれで仕方ないということでしょうか。
リモートホストのMySQLにつながらないという場合は、まずはFWを切ってみる。

# chkconfig iptables –list
iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off

というふうになっていたら無効になっているのでよし。オンになっている場合は、chkconfig iptables offで切っちゃう。これでもダメだったらSULinuxを疑う。これもとりあえず無効にしてみる。

# vi /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing – SELinux security policy is enforced.
# permissive – SELinux prints warnings instead of enforcing.
# disabled – No SELinux policy is loaded.
SELINUX=disabled # ← これで無効になっている
# SELINUXTYPE= can take one of these two values:
# targeted – Targeted processes are protected,
# mls – Multi Level Security protection.
SELINUXTYPE=targeted

これでもダメだったら、ポートが開いていないのかもしれないです。なのでnetstatする。

# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 48 123.456.789.xxx:22 12.34.56.78:52153 ESTABLISHED

TCPの3306が開いていたらOK。開いていなかったらMySQL自体が止まっているのかもしれないので、service mysqld startしてやる。
障害がなさそうなんだけど、実際にmysqlでログインしようと思うとエラーになる。
ERROR 1130 (HY000): Host 'db.example.com' is not allowed to connect to this MySQL server
あ、そうか。mysql側で許可ホストを設定しなくてはならないのだと想い出す。

mysql> grant all privileges on *.* to root@’%';
mysql> select user,host,password from mysql.user;
+——+———–+——————————————-+
| user | host | password |
+——+———–+——————————————-+
| root | localhost | *8E777DA50479EA2400847A6C020F21C64E6B08C5 |
| root | xxxxxxxx | |
| root | 127.0.0.1 | |
| root | % | *8E777DA50479EA2400847A6C020F21C64E6B08C5 |
+——+———–+——————————————-+

とやってみたら、
ERROR 1045 (28000): Access denied for user 'root'@'db.example.com' (using password: NO)
ほーrootは駄目ですか。そういういえばそうだったかもです。

mysql> grant all privileges on *.* to user@’%';
mysql> select user,host,password from mysql.user;
+——+———–+——————————————-+
| user | host | password |
+——+———–+——————————————-+
| root | localhost | *8E777DA50479EA24xxxx7A6C020F21C64E6B08C5 |
| root | 25g7t93y | |
| root | 127.0.0.1 | |
| user | % | |
+——+———–+——————————————-+

外部ホストからmysqlサーバーへアクセス。

# mysql -u user -h 123.456.789.xxx
Welcome to the MySQL monitor. Commands end with ; or \g.
(省略)
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>

できた。けど、この設定だと結構セキュリティー的にあまりよくないので、MySQLサーバーにアクセスできるホストを絞る。%はどのホストからのアクセスも許可してしまうので、あんまり使わない方がいい。
grant all privileges on *.* to user@'allow.example.com' identified by 'PASSWORD' with grant option;とかこういう感じでアクセスできるホストを限定してゆく。


Viewing all articles
Browse latest Browse all 59

Trending Articles