blog/content/post/2009/11/08/2009-11-08-00001263.md

216 lines
12 KiB
Markdown

---
title: Ubuntu の mysql サーバーを外部からアクセス可能にする
author: kazu634
date: 2009-11-08
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4907;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- mysql
- ubuntu
---
<div class="section">
<p>
Ubuntu の設定ではまったので、書いておきます。
</p>
<h4>
参考にしたサイト
</h4>
<ul>
<li>
<a href="http://www.ktsuge.com/blog/2008/01/19/mysql-server-%E3%81%B8%E5%A4%96%E9%83%A8%E3%83%9B%E3%82%B9%E3%83%88%E3%81%8B%E3%82%89%E3%81%AE%E6%8E%A5%E7%B6%9A%E8%A8%B1%E5%8F%AF/" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.ktsuge.com/blog/2008/01/19/mysql-server-%E3%81%B8%E5%A4%96%E9%83%A8%E3%83%9B%E3%82%B9%E3%83%88%E3%81%8B%E3%82%89%E3%81%AE%E6%8E%A5%E7%B6%9A%E8%A8%B1%E5%8F%AF/', 'MySQL Server へ外部ホストからの接続許可');" target="_blank">MySQL Server へ外部ホストからの接続許可</a>
</li>
<li>
<a href="http://www.avant-tokyo.com/linux/mysql_host.html" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.avant-tokyo.com/linux/mysql_host.html', 'MySQL 外部ホストからアクセスする');" target="_blank">MySQL 外部ホストからアクセスする</a>
</li>
</ul>
<h4>
設定
</h4>
<p>
Ubuntu側で設定します。設定には、
</p>
<ol>
<li>
mysql サーバ自身に外部からアクセスできるようにする設定
</li>
<li>
mysql 上で設定したユーザーがデーターベースに接続できるようにする設定
</li>
</ol>
<p>
の二種類があります。
</p>
<h4>
mysql サーバーを外部からアクセス可能にする
</h4>
<p>
参考にしたサイトにはこんなことが書いてありました:
</p>
<blockquote title="404 Not Found" cite="http://www.ktsuge.com/blog/2008/01/19/mysql-server-%E3%81%B8%E5%A4%96%E9%83%A8%E3%83%9B%E3%82%B9%E3%83%88%E3%81%8B%E3%82%89%E3%81%AE%E6%8E%A5%E7%B6%9A%E8%A8%B1%E5%8F%AF/">
<p>
調べてみると、以下の箇所を修正する必要がある様だ。
</p>
<ol>
<li>
MySQL Serverの設定ファイルmy.cnfを開き、[mysqld]の項目以下の、bind-addressを探す。
</li>
<li>
bind-addressを、コメントアウトする。
</li>
</ol>
<p>
修正し終わったら、MySQL Serverを再起動すると、外部ホストからも接続可能となる。
</p>
<p>
<cite><a href="http://www.ktsuge.com/blog/2008/01/19/mysql-server-%E3%81%B8%E5%A4%96%E9%83%A8%E3%83%9B%E3%82%B9%E3%83%88%E3%81%8B%E3%82%89%E3%81%AE%E6%8E%A5%E7%B6%9A%E8%A8%B1%E5%8F%AF/" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.ktsuge.com/blog/2008/01/19/mysql-server-%E3%81%B8%E5%A4%96%E9%83%A8%E3%83%9B%E3%82%B9%E3%83%88%E3%81%8B%E3%82%89%E3%81%AE%E6%8E%A5%E7%B6%9A%E8%A8%B1%E5%8F%AF/', '404 Not Found');" target="_blank">404 Not Found</a></cite>
</p>
</blockquote>
<p>
というわけで、 /etc/mysql にある my.cnf を編集します。
</p>
<pre class="syntax-highlight">
kazu634@srv634% sudo vi my.cnf /etc/mysql <span class="synStatement">[</span><span class="synConstant">7304</span><span class="synStatement">]</span>
kazu634@srv634% diff my.cnf_20091108 my.cnf /etc/mysql <span class="synStatement">[</span><span class="synConstant">7305</span><span class="synStatement">]</span>
57c57
<span class="synStatement">&#60;</span> <span class="synStatement">bind</span>-address <span class="synStatement">=</span> <span class="synConstant">127</span>.<span class="synConstant"></span>.<span class="synConstant"></span>.<span class="synConstant">1</span>
---
<span class="synStatement">&#62;</span><span class="synComment"> # bind-address = 127.0.0.1</span>
</pre>
<h4>
mysql 上で設定したユーザーがデーターベースにアクセスできるようにする
</h4>
<p>
とりあえずローカルなネットワークはこのような構成です:
</p>
<p>
<center>
</center>
</p>
<p>
<a href="http://flickr.com/photos/42332031@N02/4083965175/" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://flickr.com/photos/42332031@N02/4083965175/', '');" title="network configuration"><img src="http://farm3.static.flickr.com/2542/4083965175_48e188704a.jpg" /></a>
</p></p>
<p>
というわけで、192.168.11のネットワークからアクセスできるように設定します。
</p>
<pre class="syntax-highlight">
kazu634@srv634% mysql -uroot -pxxxxxxxx ~ [<span class="synConstant">7291</span>]
Welcome <span class="synSpecial">to</span> the MySQL monitor. Commands <span class="synSpecial">end</span> <span class="synSpecial">with</span> ; <span class="synStatement">or</span> \g.
Your MySQL connection id <span class="synSpecial">is</span> <span class="synConstant">398</span>
Server version: <span class="synConstant">5.0</span>.<span class="synConstant">75</span>-0ubuntu10.<span class="synConstant">2</span> (Ubuntu)
<span class="synSpecial">Type</span> <span class="synConstant">'help;'</span> <span class="synStatement">or</span> <span class="synConstant">'\h'</span> <span class="synSpecial">for</span> help. <span class="synSpecial">Type</span> <span class="synConstant">'\c'</span> <span class="synSpecial">to</span> clear the buffer.
mysql&#62; use mysql;
Reading <span class="synSpecial">table</span> information <span class="synSpecial">for</span> completion <span class="synSpecial">of</span> <span class="synSpecial">table</span> <span class="synStatement">and</span> <span class="synSpecial">column</span> names
You can turn off this feature <span class="synSpecial">to</span> get a quicker startup <span class="synSpecial">with</span> -A
Database changed
mysql&#62; <span class="synStatement">select</span> <span class="synSpecial">user</span>, host <span class="synSpecial">from</span> <span class="synSpecial">user</span>;
+<span class="synComment">------------------+-----------+</span>
| <span class="synSpecial">user</span> | host |
+<span class="synComment">------------------+-----------+</span>
| redmine | % |
| root | <span class="synConstant">127.0</span>.<span class="synConstant">0.1</span> |
| debian-sys-maint | localhost |
| root | localhost |
| root | srv634 |
+<span class="synComment">------------------+-----------+</span>
<span class="synConstant">5</span> <span class="synSpecial">rows</span> <span class="synStatement">in</span> <span class="synStatement">set</span> (<span class="synConstant"></span>.<span class="synConstant">02</span> sec)
mysql&#62; show databases;
+<span class="synComment">--------------------+</span>
| Database |
+<span class="synComment">--------------------+</span>
| information_schema |
| money |
| mysql |
| phone |
| redmine |
| shop |
| words |
+<span class="synComment">--------------------+</span>
<span class="synConstant">7</span> <span class="synSpecial">rows</span> <span class="synStatement">in</span> <span class="synStatement">set</span> (<span class="synConstant"></span>.<span class="synConstant">00</span> sec)
mysql&#62; <span class="synStatement">grant</span> <span class="synStatement">all</span> <span class="synSpecial">privileges</span> <span class="synSpecial">on</span> shop.* <span class="synSpecial">to</span> root@<span class="synConstant">&#34;192.168.11.%&#34;</span>
-&#62; <span class="synSpecial">identified</span> <span class="synSpecial">by</span> <span class="synConstant">'musashi'</span> <span class="synSpecial">with</span> <span class="synStatement">grant</span> <span class="synSpecial">option</span> ;
Query OK, <span class="synConstant"></span> <span class="synSpecial">rows</span> affected (<span class="synConstant"></span>.<span class="synConstant">07</span> sec)
mysql&#62; exit
Bye
</pre>
<h4>
最後に
</h4>
<p>
mysql サーバーを再起動します:
</p>
<pre class="syntax-highlight">
kazu634@srv634% sudo /etc/init.d/mysql <span class="synStatement">restart</span> /etc/mysql <span class="synStatement">[</span><span class="synConstant">7308</span><span class="synStatement">]</span>
* Stopping MySQL database server mysqld <span class="synStatement">[</span> OK <span class="synStatement">]</span>
* Starting MySQL database server mysqld <span class="synStatement">[</span> OK <span class="synStatement">]</span>
* Checking <span class="synStatement">for</span> corrupt, not cleanly closed and upgrade needing tables.
</pre>
<h4>
テスト
</h4>
<p>
アクセスできました♪
</p>
<pre class="syntax-highlight">
~ on kazu634 <span class="synStatement">[</span><span class="synConstant">517</span><span class="synStatement">]</span> $: hostname
musashi-kazuhiro-no-iMac.<span class="synStatement">local</span>
~ on kazu634 <span class="synStatement">[</span><span class="synConstant">518</span><span class="synStatement">]</span> $: mysql <span class="synSpecial">-h</span> srv634 <span class="synSpecial">-uroot</span> <span class="synSpecial">-p</span>
-bash: mysql: command not found
~ on kazu634 <span class="synStatement">[</span><span class="synConstant">519</span><span class="synStatement">]</span> $: mysql5 <span class="synSpecial">-h</span> srv634 <span class="synSpecial">-uroot</span> <span class="synSpecial">-p</span>
Enter password:
Welcome to the MySQL monitor. Commands end with <span class="synStatement">;</span> or \g.
Your MySQL connection id is <span class="synConstant">35</span>
Server version: <span class="synConstant">5</span>.<span class="synConstant"></span>.<span class="synConstant">75</span>-0ubuntu10.<span class="synConstant">2</span> <span class="synStatement">(</span>Ubuntu<span class="synStatement">)</span>
Type <span class="synStatement">'</span><span class="synConstant">help;</span><span class="synStatement">'</span> or <span class="synStatement">'</span><span class="synConstant">\h</span><span class="synStatement">'</span> <span class="synStatement">for</span> <span class="synStatement">help</span>. Type <span class="synStatement">'</span><span class="synConstant">\c</span><span class="synStatement">'</span> to <span class="synStatement">clear</span> the current input statement.
mysql<span class="synStatement">&#62;</span> use shop<span class="synStatement">;</span>
Reading table information <span class="synStatement">for</span> completion of table and column names
You can turn off this feature to get a quicker startup with <span class="synStatement">-A</span>
Database changed
mysql<span class="synStatement">&#62;</span> show tables<span class="synStatement">;</span>
+----------------+
<span class="synStatement">|</span> Tables_in_shop <span class="synStatement">|</span>
+----------------+
<span class="synStatement">|</span> Starbucks <span class="synStatement">|</span>
<span class="synStatement">|</span> renoir <span class="synStatement">|</span>
+----------------+
<span class="synConstant">2</span> rows <span class="synStatement">in</span> <span class="synStatement">set </span><span class="synIdentifier">(0.00</span> <span class="synIdentifier">se</span>c)
mysql<span class="synStatement">&#62;</span> <span class="synStatement">select</span> * from renoir\G
*************************** <span class="synConstant">1</span>. row ***************************
id: <span class="synConstant">1</span>
shopname: 喫茶室ルノアール 池袋東口店
address: 東京都豊島区東池袋<span class="synConstant">1-40-2</span>
tel: <span class="synConstant">03-3980-6352</span>
shophour: 年中無休全日:<span class="synConstant">7</span>:<span class="synConstant">30-23</span>:<span class="synConstant">00</span>
accomodation: 禁煙席:<span class="synConstant">38</span>席喫煙席:<span class="synConstant">32</span>
access: JR池袋駅東口出て明治通り左並びの東池袋<span class="synConstant">1</span>丁目交差点ヤマダ電機隣
lat: <span class="synConstant">35</span>.<span class="synConstant">7320862</span>
lng: <span class="synConstant">139</span>.<span class="synConstant">7138367</span>
</pre>
</div>