この記事は2022年10月以前に公開し、一度消失してしまったものですが、Wayback Machineのキャッシュデータから復元しました。当時の備忘録として再掲載します。
今回はLAMP環境構築作業の続きでMySQLの設定を行っていきます。
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に変更します
default-character-set = utf8
※内容を保存しviエディタを終了します
MySQLのサービス起動と自動起動設定
■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ユーザにパスワードを設定するよう指示が表示されていたのでパスワードを設定します。
■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
ログイン確認
パスワードの設定が完了したので、ログインを確認していきます。
■ホスト名がlocalhostのrootユーザでパスワードなしでログイン施行
[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にログインできるか確認します。
■ホスト名がlocalhostのrootユーザでパスワードありでログイン施行
[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という名前のデータベースが作成されていますが、不要なので削除します。
■MySQLへrootでログインします
[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を使ったシステムを作る場合はライセンスに留意する必要がありますね。