Tips

MySQL セキュリティ強化 【初級編 第3回】

MySQL セキュリティ強化 【初級編 第3回】

セキュリティを強化

MySQLでのパスワードの設定と、外部接続用のユーザの作成方法を記述する

パスワードの設定

しかしrootユーザにはパスワードが設定されていないので設定しないとセキュリティが弱い。
パスワードを設定するコマンドはいろいろあって下記のような方法がある。

  • 方法1. Shellからパスワードを設定する場合
    # mysqladmin -u root password パスワード
  • 方法2. mysqlにログインしてから設定する(現在接続しているユーザのパスワードを設定する)
    mysql> set password = password(‘パスワード’)
  • 方法3. mysqlにログインしてから設定する(ユーザを指定してのパスワードを設定する)
    mysql> set password for ‘ユーザ名’ = password(‘パスワード’)

パスワード設定後の接続のしかた

パスワードを設定したら、以下のコマンドで接続が可能である。
# mysql -u root -p
Enter password: ここに設定したパスワードを入力する

一応SQLコマンドを打ってみる。
作成されているデータベースを確認してみる。
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| sample |
| test |
+——————–+
4 rows in set (0.03 sec)

外部ホストからDBへ接続できるようにする

外部ホストからDBへ接続できるようにするにはユーザを作成する必要がある。(この例では他に mysqlコマンドをたたける外部ホストがいなかったのでローカルIPで試してみた)
MySQLをインストールしているサーバ自身のIPを調べてみる。
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
inet addr:192.168.200.1 Bcast:192.168.100.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1430753 errors:0 dropped:0 overruns:0 frame:0
TX packets:395665 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:945005883 (901.2 MiB) TX bytes:32814278 (31.2 MiB)
Interrupt:185

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:3071 errors:0 dropped:0 overruns:0 frame:0
TX packets:3071 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3325310 (3.1 MiB) TX bytes:3325310 (3.1 MiB)

[root@localhost ~]#
[root@localhost ~]#

IPを調べて下記のコマンドで接続してみるがエラーになる。
[root@localhost ~]# mysql -u root -h 192.168.200.1
ERROR 1130 (HY000): Host ‘wordpress’ is not allowed to connect to this MySQL server

[root@localhost ~]#
[root@localhost ~]#

再び localhost経由で接続して、外部接続可能なユーザを作成する
[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 13
Server version: 5.1.58 MySQL Community Server (GPL) by Utter Ramblings

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.

以下が rootで外部接続可能なユーザを作成するコマンドである(allで管理者権限を与えている)
mysql> grant all on *.* to ‘root’@’192.168.200.1’;
Query OK, 0 rows affected (0.00 sec)

作成したユーザで接続しなおしてみると接続できるようになっていることが分かる
mysql> quit
Bye
[root@localhost ~]# mysql -u root -h 192.168.200.1
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 14
Server version: 5.1.58 MySQL Community Server (GPL) by Utter Ramblings

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the current input statement.

mysql>

以上です。

SQLが学べる 関連連載リンク

データベースの基礎が学べるSQL基礎講座
SQL基礎 連載

練習問題を通じてSQL理解度アップの人気連載!
SQL練習問題集

Recent News

Recent Tips

Tag Search