今回はLAMP環境構築作業の続きでMySQLの設定を行っていきます。
MySQLの設定ファイルを変更します。
[root@centos ~]# vi /etc/my.cnf
viエディタが起動するので設定ファイルを変更します。
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
MySQLの文字コードをUTF-8に変更します
default-character-set = utf8
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
MySQLクライアントの文字コードをUTF-8に変更します
[mysql]
default-character-set = utf8
内容を保存しviエディタを終了します
MySQLのサービス起動と自動起動の設定をします。
[root@centos ~]# /etc/rc.d/init.d/mysqld start
MySQL データベースを初期化中: Installing MySQL system tables…
OK
Filling help tables…
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password ‘new-password’
/usr/bin/mysqladmin -u root -h centos.sa-sa-ki.jp password ‘new-password’
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
[ OK ]
MySQL を起動中: [ OK ]
OS再起動時にMySQLを自動起動するようにします
[root@centos ~]# chkconfig mysqld on
[root@centos ~]# chkconfig –list | grep mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
MySQLのrootユーザにパスワード設定
MySQL起動時にMySQLのrootユーザにパスワードを設定するよう指示が表示されていたのでパスワードを設定します。
[root@centos ~]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: x.x.xx Source distribution
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
MySQLのユーザ、パスワードを確認
mysql> select user,host,password from mysql.user;
+——+——————–+———-+
| user | host | password |
+——+——————–+———-+
| root | localhost | |
| root | centos.sa-sa-ki.jp | |
| root | 127.0.0.1 | |
+——+——————–+———-+
3 rows in set (0.00 sec)
MySQLに接続する時のホスト名がlocalhost、centos.sa-sa-ki.jp、127.0.0.1でユーザ名がrootのパスワードが未登録な事が確認できます。
これら全てにパスワードを設定していきます。
ホスト名がlocalhostのrootユーザにパスワード設定します
mysql> set password for root@localhost=password(’rootパスワード’);
Query OK, 0 rows affected (0.00 sec)
ホスト名がcentos.sa-sa-ki.jpのrootユーザにパスワード設定します
mysql> set password for root@”centos.sa-sa-ki.jp”=password(’rootパスワード’);
Query OK, 0 rows affected (0.00 sec)
ホスト名が127.0.0.1のrootユーザにパスワード設定します
mysql> set password for root@127.0.0.1=password(’rootパスワード’);
Query OK, 0 rows affected (0.00 sec)
MySQLのユーザ、パスワードを再確認します
mysql> select user,host,password from mysql.user;
+——+——————–+——————+
| user | host | password |
+——+——————–+——————+
| root | localhost | ++++++++++++++++ |
| root | centos.sa-sa-ki.jp | ++++++++++++++++ |
| root | 127.0.0.1 | ++++++++++++++++ |
+——+——————–+——————+
3 rows in set (0.00 sec)
MySQLに接続する時のホスト名がlocalhost、centos.sa-sa-ki.jp、127.0.0.1でユーザ名がrootのパスワードが設定された事が確認できました。
MySQLからログアウトします。
mysql> exit
Bye
ログイン確認
パスワードの設定が完了したので、ログインを確認していきます。
[root@centos ~]# mysql -u root -h localhost
ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: NO)
ホスト名がlocalhostのrootユーザでパスワードなしでMySQLサーバーへログインできないことが確認できました。
同様にホスト名centos.sa-sa-ki.jpや127.0.0.1の場合も試してみます。
ホスト名がcentos.sa-sa-ki.jpのrootユーザでパスワードなしでログイン施行
[root@centos ~]# mysql -u root -h ‘centos.sa-sa-ki.jp’
ERROR 1130 (00000): Host ’sa-sa-ki.jp’ is not allowed to connect to this MySQL server
ホスト名が127.0.0.1のrootユーザでパスワードなしでログイン施行
[root@centos ~]# mysql -u root -h 127.0.0.1
ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: NO)
あれ?
ホスト名centos.sa-sa-ki.jpの場合だけエラーメッセージが違いますね。
どうやらDNSサーバーで名前解決すると外部からの接続と認識して外部接続を許可する権限を付与しないとダメなようです。
/etc/hostsにcentos.sa-sa-ki.jpを追加すると他と同じメッセージになったのでひとまずよしとします。
次にパスワードを入力してMySQLにログインできるか確認します。
[root@centos ~]# mysql -u root -h localhost -p
Enter password: 設定したrootユーザのパスワードを入力
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: x.x.xx Source distribution
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
接続できたのでログアウトします。
mysql> exit
Bye
同様にホスト名centos.sa-sa-ki.jpや127.0.0.1の場合も試してみます。
ホスト名がcentos.sa-sa-ki.jpのrootユーザでパスワードありでログイン施行
[root@centos ~]# mysql -u root -h ‘centos.sa-sa-ki.jp’ -p
Enter password: 設定したrootユーザのパスワードを入力
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: x.x.xx Source distribution
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
接続できたのでログアウトします。
mysql> exit
Bye
ホスト名が127.0.0.1のrootユーザでパスワードありでログイン施行
[root@centos ~]# mysql -u root -h 127.0.0.1 -p
Enter password: 設定したrootユーザのパスワードを入力
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23
Server version: x.x.xx Source distribution
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
接続できたのでログアウトします。
mysql> exit
Bye
どのパターンもパスワード認証できました。
testデータベース削除
MySQLにはデフォルトでtestという名前のデータベースが作成されていますが、不要なので削除します。
[root@centos ~]# mysql -u root -h localhost -p
Enter password: 設定したrootユーザのパスワードを入力
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: x.x.xx Source distribution
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
登録データベースを確認します
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| test |
+——————–+
3 rows in set (0.00 sec)
testデータベースを削除します
mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)
再度登録データベースを確認します
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
+——————–+
2 rows in set (0.00 sec)
testデータベースが削除されている事が確認できました
mysql> exit
Bye
MySQLの初期設定は終了です。
これでデータベースと連携したホームページ作りが可能になります。
なおMySQLを使ったシステムを作る場合はライセンスに関して注意が必要です。
MySQLにはGNU General Public License (一般的には「GPL」と呼ばれます) と商用ライセンスがあります。
GPLは無償でMySQLを使ったシステムを構築できる代わりにソースコードを公開する必要があり、他のユーザはそれを自由に改変、統合、頒布することができます。
商用ライセンスは有償ですがMySQLを使ったシステムのソースコードを公開する必要はありません。
ということで、MySQLを使ったシステムを作る場合はライセンスに留意する必要がありますね。
■ 参考サイト ■
データベースサーバー構築(MySQL) – CentOSで自宅サーバー構築
MySQL :: ライセンスに関する FAQ
————————————————————————————————–
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その13 CentOS 5.2 にLAMP環境構築(Apache、PHP編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その12 CentOS 5.2 にDNSサーバーを導入して名前解決(確認編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その11 CentOS 5.2 にDNSサーバーを導入して名前解決(設定編)
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その10 CentOS 5.2 SSHサーバーを構築して外部からリモート接続&リモートファイル転送
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その9 CentOS 5.2 にバッファオーバーフロー攻撃対策を行う
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その8 CentOS 5.2 にrootkit検知ツールを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その7 CentOS 5.2 にファイル改竄検知システムを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その6 CentOS 5.2 にアンチウィルスソフトを導入する
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その5 CentOS 5.2 ファイル転送準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その4 CentOS 5.2 インストール後の初期設定
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その3 インストールしたCentOS 5.2 にリモート接続準備
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その2 CentOS 5.2 をインストールする
flashcast:フリーで働くITエンジニア集団のブログ: 自宅サーバー再構築 その1 導入を検討する



















