他のサーバに入れない。MySQLで他のサーバからのアクセスを許可する
この前後輩(大学2年生)と話をしていて、後輩がMySQLを今がりがりやってるとのこと。
私は本当に最低限の設定しか知らず他のことをやっていたのであまり詳しくないのです。
もともと疑問に思ったことを聞いてみました。
ヌ「自分からグローバルIPへのmysqlコマンドでのログインって可能なの?」
後「できますよ〜」
ヌ「え?((((;゚Д゚))))マジで?できないんだけど・・・」
後「いや〜できますよ〜グローバルとかローカルとか関係なくログインできます」
ヌ「マジか・・・今度やってみるわ・・・」
という話をしていました。
実際にできませんでした
mysql -h example.remote.com -P 3306 -u rootこんな感じで怒られるんです・・・
ERROR 1130 (HY000): Host 'localonuki.com' is not allowed to connect to this MySQL server
エラーログからググッてみたら、ユーザとホストの設定をしないと入れないとのこと。
まぁ〜当たり前ですよね。どっからでも簡単に入れちゃうならMySQLのクラックとか頻繁に起きそうだし・・・
まずはユーザ作成の方法での意味をちゃんと理解していなかったのが問題でした。
今回は私の備忘録の意味でもやり方を残しておこうと思います。
※以下に2つドメインがありますが、存在しないドメインです。また、わかりやすくするためにドメインにしていますが、実際はIPアドレスを記入してください。たぶんドメインでもできますがオススメはできません
mysql> use mysql;
mysql> select host from mysql.user;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- +
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
host |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- +
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
127.0.0.1 |
::1 |
localhost |
localhost |
localhost |
localhost |
example.onuki.com |
example.onuki.com |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- +
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
これで許可されてるホストが見れます。
次にどのユーザがログインできるのか確認します
mysql> select user from mysql.user;
-
-
-
-
-
-
-
-
-
-
-
- +
-
-
-
-
-
-
-
-
-
-
user |
-
-
-
-
-
-
-
-
-
-
-
- +
-
-
-
-
-
-
-
-
-
-
root |
root |
testuser |
root |
wikipe |
root |
-
-
-
-
-
-
-
-
-
-
-
- +
-
-
-
-
-
-
-
-
-
-
こんな感じでユーザが見れます。この2つを並べると一致できます。
でもそれぞれを目視で確認するのは難しいですよね。
これだけだとどのアカウントがどのホストからログインできるのかわかりません。
なので以下のコマンドで確認します。
mysql> select user , host from mysql.user;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- +
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
user | host |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- +
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
root | 127.0.0.1 |
root | ::1 |
localhost | |
testuser | localhost |
root | localhost |
wikipe | localhost |
example.onuki.com | |
root | example.onuki.com |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- +
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
※はてな表記で崩れてますがすいません・・・
これでどのユーザがどこからログインできるのかわかりますね
ここで確実に他のサーバからログインできないことがわかりました。
なので接続先のexample.onuki.comのMySQLにログインして外部接続用のアカウントを作成します。
今回はわかりやすくするためにパスワードを設定しない場合の手順です。
接続先のMySQLにまずログインします
# mysql -u rootWelcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 510033
Server version: 5.5.14-log MySQL Community Server (GPL) by RemiCopyright (c) 2000, 2010, 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> grant all privileges on *.* to onuki@"example.remote.com" ;
Query OK, 0 rows affected (0.01 sec)
onukiというユーザを全てのデータベース、全てのテーブルにアクセスできるパターンです。
onukiというユーザがexample.remote.comからexample.onuki.comへ接続できるように指定しています。
これで完了です。
example.remote.comからexample.onuki.comへアクセスできるようになっているはずです。
普段のmysqlコマンドで -h オプションで接続先のホストを指定しましょう。
$ mysql -h example.onuki.com -u onuki -P 3306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 510089
Server version: 5.5.14-log MySQL Community Server (GPL) by RemiCopyright (c) 2000, 2010, 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.
OK!
接続確認できました。これで他のサーバからアクセスすることができるようになりました。
Replicationとかもこれでやるんでしょう。実際やったことがないのでわかりませんが・・・
後はHDDの量が少なくて他のサーバにDBを準備したい場合こうゆうことで
DBだけ他のサーバで設定できますね。
これでいろんなことができるようになったので試してみようと思います。
エキスパートのためのMySQL[運用+管理]トラブルシューティングガイド
- 作者: 奥野幹也
- 出版社/メーカー: 技術評論社
- 発売日: 2010/06/12
- メディア: 大型本
- 購入: 16人 クリック: 204回
- この商品を含むブログ (35件) を見る