MySQLは 高速性が定評の RDBMS(データベース)です
Linuxへのインストールと実際の利用状況をレポートします
ちなみに 64bit環境の検証や IPv6対応検証を兼ねていますが
結論から言って IPv6対応はしていません
|
|
MySQLとは
有名で高性能なデータベースです
MySQL AB社 → SUN → Oracle と買収されて 結局 Oracleの一員となってしまっています
オープンソースですが 企業の巨大データベースなどに使わないのであれば 十分な性能を持っています
日本では PostgreSQLと同じくらい有名です
インストール
ここでのインストール作業は 主に以下の流れを解説しています
- MySQLソースパッケージのダウンロード
- ソースコードからのビルドと MySQLバイナリファイル郡のインストール
- my.cnf環境設定 start/stop起動スクリプトの設定
- データベース領域の新規作成
- mysqldの起動
- MySQL管理者権限のアクセス権限設定
早速 MySQLサイトからソースコードをダウンロードします
上記は 2009年のときのWeb構成です
Communication Server というのが GPL版の MySQL本体となります
OSが RPM系などパッケージシステムであれば 各バイナリパッケージもあるので
そちらを活用したほうがよいでしょう
(今回扱いませんが 商用ライセンスとして Enterprise版 が存在します)
2011年に訪れたときは オープンソース版のバリエーションが拡充され充実したイメージがあります
Enterprise版の表記はなくなっていました (商用サポートを受けられる契約は残っているようです)
Community Server というのが今回ダウンロードする対象です
Clusterは 文字どおりクラスタ構成で可用性を向上させるのに特化したパッケージです
Workbenchは データベースモデリングツールで GUIでテーブル設計 MySQLへのDB化が可能です
Proxyは サーバクライアント間に介在しモニタリング機能やロードバランス機能を提供します
Connectorsは ODBCや JDBCと互換のデータベースドライバです
Installerは 上記のWindows版を 1パックにまとめたものです
これだけ見ても 個人/商用どちらにも耐えられそうなラインナップです
検証環境では Community Serverをソースからインストールしました
GAリリースタブで Platformを Source Codeに設定すると Generic Linux版が見えます
下記は mysql-5.1.41版でのビルド設定ログです
$ tar -xzf mysql-5.1.41.tar.gz
$ cd mysql-5.1.41
$ ls
BUILD Makefile.in configure libmysqld regex tests
CMakeLists.txt README configure.in ltmain.sh scripts unittest
COPYING aclocal.m4 dbug man server-tools vio
ChangeLog client depcomp missing sql win
Docs cmd-line-utils extra mysql-test sql-bench ylwrap
EXCEPTIONS-CLIENT config include mysys sql-common zlib
INSTALL-SOURCE config.guess install-sh netware storage
INSTALL-WIN-SOURCE config.h.in libmysql plugin strings
Makefile.am config.sub libmysql_r pstack support-files
$
$ tar -xzf mysql-5.1.41.tar.gz $ cd mysql-5.1.41 $ ls BUILD Makefile.in configure libmysqld regex tests CMakeLists.txt README configure.in ltmain.sh scripts unittest COPYING aclocal.m4 dbug man server-tools vio ChangeLog client depcomp missing sql win Docs cmd-line-utils extra mysql-test sql-bench ylwrap EXCEPTIONS-CLIENT config include mysys sql-common zlib INSTALL-SOURCE config.guess install-sh netware storage INSTALL-WIN-SOURCE config.h.in libmysql plugin strings Makefile.am config.sub libmysql_r pstack support-files $
通常の configure系のアプリケーションと同じくビルドできそうです
$ ./configure --prefix=/usr/local --with-charset=ujis --with-extra-charsets=utf8 --with-mysqld-user=mysql checking build system type... i686-pc-linux-gnu checking host system type... i686-pc-linux-gnu checking target system type... i686-pc-linux-gnu checking for a BSD-compatible install... /bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /bin/mkdir -p checking for gawk... gawk checking whether make sets $(MAKE)... yes checking "character sets"... default: ujis, collation: ujis_japanese_ci; compiled in: ujis latin1 utf8 utf8 … … config.status: creating include/mysql_version.h config.status: creating plugin/Makefile config.status: creating win/Makefile config.status: creating include/config.h config.status: executing depfiles commands config.status: executing libtool commands /bin/rm: cannot remove `libtoolT': No such file or directory config.status: executing default commands MySQL has a Web site at http://www.mysql.com/ which carries details on the latest release, upcoming features, and other information to make your work or play with MySQL more productive. There you can also find information about mailing lists for MySQL discussion. Remember to check the platform specific part of the reference manual for hints about installing MySQL on your platform. Also have a look at the files in the Docs directory. Thank you for choosing MySQL! $
文字コードは 各自のOSの環境に合わせて設定します
検証の環境では EUC-JP なので ujis を その他として(念のため?) UTF-8 も指定しています
また MySQL専用のユーザアカウント mysql:mysql を指定しています
下記は mysql-5.5.15版でのビルド設定ログです
$ tar -xzf mysql-5.5.15.tar.gz $ cd mysql-5.5.15 $ ls -F BUILD/ VERSION include/ plugin/ support-files/ BUILD-CMAKE client/ libmysql/ regex/ tests/ CMakeLists.txt cmake/ libmysqld/ scripts/ unittest/ COPYING cmd-line-utils/ libservices/ sql/ vio/ Docs/ config.h.cmake man/ sql-bench/ win/ INSTALL-SOURCE configure.cmake mysql-test/ sql-common/ zlib/ INSTALL-WIN-SOURCE dbug/ mysys/ storage/ README extra/ packaging/ strings/ $
mysql-5.5系からは cmakeによる構築が推奨されています
$ cmake -DCMAKE_INSTALL_PREFIX=/usr/mysql -DMYSQL_DATADIR=/var/db/mysql -DSYSCONFDIR=/usr/mysql/etc -DWITH_READLINE=ON . -- The C compiler identification is GNU -- The CXX compiler identification is GNU -- Check for working C compiler: /usr/bin/gcc -- Check for working C compiler: /usr/bin/gcc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ 〜 -- Check size of pthread_t -- Check size of pthread_t - done -- Performing Test HAVE_PEERCRED -- Performing Test HAVE_PEERCRED - Success -- Configuring done -- Generating done -- Build files have been written to: /home/admin/mysql-5.5.15 $
以上で Makefileが生成されました
後は make と make install でインストールされます
configure に –help をつけるか cmake -L で分かりますが
MySQLではデータベースストレージ方式が自由に選択できます 以下一部紹介します
エンジン | 説明 |
---|---|
MyISAM | トランザクションが使えない デフォルトのエンジン |
InnoDB | 元々 Innobase社が開発していたエンジン トランザクションが使える |
Memory | 全部メモリに展開 速そう |
Merge | 複数の MyISAMを 1つのデータベースグループとして扱う |
Federated | 分散配置されたサーバでの利用 分散データベースを実現 |
NDB | クラスタデータベース 耐障害性はよいかも |
CSV | CSVを直接扱う? |
他にも Blackhole(格納されたデータは破棄?)といった 開発向けとしか思えないエンジンなど
ありますが MyISAM以外は未検証です
bin/ には以下のコマンドが追加されます
innochecksum mysql_convert_table_format mysql_zap mysqlhotcopy msql2mysql mysql_find_rows mysqlaccess mysqlimport my_print_defaults mysql_fix_extensions mysqladmin mysqlshow myisam_ftdump mysql_fix_privilege_tables mysqlbinlog mysqlslap myisamchk mysql_install_db mysqlbug mysqltest myisamlog mysql_secure_installation mysqlcheck perror myisampack mysql_setpermission mysqld_multi replace mysql mysql_tzinfo_to_sql mysqld_safe resolve_stack_dump mysql_client_test mysql_upgrade mysqldump resolveip mysql_config mysql_waitpid mysqldumpslow
libexec/ には次のサーバプログラムが追加されます
mysqld mysqlmanager
lib/ 以下には以下のように データベースエンジンのライブラリが追加されます
/usr/local/lib/mysql/: libdbug.a libmysqlclient.la libmysqlclient_r.la libmysys.a libheap.a libmysqlclient.so libmysqlclient_r.so libvio.a libmyisam.a libmysqlclient.so.16 libmysqlclient_r.so.16 plugin libmyisammrg.a libmysqlclient.so.16.0.0 libmysqlclient_r.so.16.0.0 libmysqlclient.a libmysqlclient_r.a libmystrings.a /usr/local/lib/mysql/plugin: ha_archive.a ha_example.so.0.0.0 ha_innodb_plugin.so.0 ha_archive.la ha_federated.a ha_innodb_plugin.so.0.0.0 ha_archive.so ha_federated.la libdaemon_example.a ha_archive.so.0 ha_federated.so libdaemon_example.la ha_archive.so.0.0.0 ha_federated.so.0 libdaemon_example.so ha_blackhole.a ha_federated.so.0.0.0 libdaemon_example.so.0 ha_blackhole.la ha_innodb.a libdaemon_example.so.0.0.0 ha_blackhole.so ha_innodb.la mypluglib.a ha_blackhole.so.0 ha_innodb.so mypluglib.la ha_blackhole.so.0.0.0 ha_innodb.so.0 mypluglib.so ha_example.a ha_innodb.so.0.0.0 mypluglib.so.0 ha_example.la ha_innodb_plugin.a mypluglib.so.0.0.0 ha_example.so ha_innodb_plugin.la ha_example.so.0 ha_innodb_plugin.so
また mysql-test/ sql-bench/ といった
性能評価に関するディレクトリが追加されます
続いて my.cnf 設定ファイルを作成します次の設定を行うためです
- データベースディレクトリの場所を /usr/local/var/mysql/ から /var/db/mysql/ にカスタマイズ
- PID情報ファイルの置き場所を /usr/local/var/ホスト名.pid から /var/run/mysql.pid にカスタマイズ
- エラーログファイルの置き場所を /usr/local/var/ホスト名.err から /var/log/mysql.err にカスタマイズ
- 一時ディレクトリの場所も /tmp/ 以下にカスタマイズ
/etc/my.cnf を以下の内容で作成しました
[mysqld] user=mysql datadir=/var/db/mysql socket=/tmp/mysql.sock tmpdir=/tmp/mysql pid-file=/var/run/mysql.pid log-error=/var/log/mysql max_connections=10 thread_concurrency=1 query_cache_size=0 key_buffer=32M sort_buffer=512K myisam_sort_buffer_size=64M
バッファ(メモリ)サイズの設定も行っていますが 最小限度の設定です
業務や Web公開などで MySQLを本格的に使うには さらにカスタマイズする必要あります
tmpdir のカスタマイズには注意点があります
指定したディレクトリが 存在しなかったり mysql権限でアクセスできないと不具合が出ます
また データベースの初期化に失敗したり SELECT等の DDL処理に失敗します
ディレクトリの場所を変更したため 以下のように tmpdir を作成します
# mkdir /tmp/mysql # chown mysql /tmp/mysql # chmod 700 /tmp/mysql #
続いて データベースを初期化します root権限で実施します
# mysql_install_db --user=mysql --datadir=/var/db/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/local/bin/mysqladmin -u root password 'new-password' /usr/local/bin/mysqladmin -u root -h myhost.mydomain password 'new-password' Alternatively you can run: /usr/local/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr/local ; /usr/local/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/local/bin/mysqlbug script! The latest information about MySQL is available at http://www.mysql.com/ Support MySQL by buying support/licenses from http://shop.mysql.com/ # ls -R /var/db/mysql/ /var/db/mysql/: mysql test /var/db/mysql/mysql: columns_priv.MYD help_relation.frm slow_log.CSV columns_priv.MYI help_topic.MYD slow_log.frm columns_priv.frm help_topic.MYI tables_priv.MYD db.MYD help_topic.frm tables_priv.MYI db.MYI host.MYD tables_priv.frm db.frm host.MYI time_zone.MYD event.MYD host.frm time_zone.MYI event.MYI ndb_binlog_index.MYD time_zone.frm event.frm ndb_binlog_index.MYI time_zone_leap_second.MYD func.MYD ndb_binlog_index.frm time_zone_leap_second.MYI func.MYI plugin.MYD time_zone_leap_second.frm func.frm plugin.MYI time_zone_name.MYD general_log.CSM plugin.frm time_zone_name.MYI general_log.CSV proc.MYD time_zone_name.frm general_log.frm proc.MYI time_zone_transition.MYD help_category.MYD proc.frm time_zone_transition.MYI help_category.MYI procs_priv.MYD time_zone_transition.frm help_category.frm procs_priv.MYI time_zone_transition_type.MYD help_keyword.MYD procs_priv.frm time_zone_transition_type.MYI help_keyword.MYI servers.MYD time_zone_transition_type.frm help_keyword.frm servers.MYI user.MYD help_relation.MYD servers.frm user.MYI help_relation.MYI slow_log.CSM user.frm /var/db/mysql/test: #
この作業で データ格納領域の初期化が完了します
領域はディレクトリ /var/db/mysql/ を指定しています
mysql-5.5系では mysql_initall_db は scripts/ の中に入っているので
# cd /usr/mysql # scripts/mysql_install_db --user=mysql --datadir=/var/db/mysql
とする必要があります
mysqlデータベース (MySQLのシステムテーブルが入る) と testデータベース(空) が確認できます
ディレクトリには 3種類のファイルが データベーステーブル毎に作成されています
.frm はテーブル定義 .MYD はデータ内容 .MYI はインデックスデータ を保持します
(これらファイルは ISAMデータベース形式によるもの)
ちなみに データベース領域の削除方法は
mysqld が停止した状態で ディレクトリ削除(rm -r)するだけです
いよいよ データベースサーバ(mysqld)の起動です
ここでは 主に mysql-5.5.15 (2011年9月検証)での動作状況を記載します
mysqld を起動/停止できる start/stopスクリプトが support-files/mysql.server
として準備されてるので 必要なら OS起動時に start引数付で呼ばれるようにします
検証の環境の例では Gentooベースなので
以下の内容で /etc/init.d/mysql (root:root 0755) を作成しました
#!/sbin/runscript # Copyright 1999-2004 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # $Header$ depend() { need net.eth0 after apache provide mysql } start() { ebegin "Starting MySQL" /usr/local/share/mysql/mysql.server start eend $? } stop() { ebegin "Stopping MySQL" /usr/local/share/mysql/mysql.server stop eend $? } restart() { ebegin "Restarting MySQL" /usr/local/share/mysql/mysql.server restart eend $? } status() { /usr/local/share/mysql/mysql.server status exit $? }
mysqldの環境設定は etc/my.cnf で設定済です
(mysql.server の呼び出しでオプション –pid-file= が使えるらしいですが
検証環境では 効果が確認できませんでした)
start/stopスクリプトの準備ができたので 間違いなく動くか確認します
# /etc/init.d/mysql start * Starting MySQL ... Starting MySQL.. SUCCESS! [ ok ] # /etc/init.d/mysql status * status: started SUCCESS! MySQL running (22777) # /etc/init.d/mysql stop * Stopping MySQL ... Shutting down MySQL. SUCCESS! [ ok ] # /etc/init.d/mysql status * status: stopped ERROR! MySQL is not running # /etc/init.d/mysql start * Starting MySQL ... Starting MySQL.. SUCCESS! [ ok ] #
次にアクセス権限の設定を行います
まず次の操作を試してみて アクセス権限の有無が調査できます
$ mysql -u root -e 'select Host,User,Password,Select_priv from mysql.user;' +-----------+------+----------+-------------+ | Host | User | Password | Select_priv | +-----------+------+----------+-------------+ | localhost | root | | Y | | phenom | root | | Y | | 127.0.0.1 | root | | Y | | ::1 | root | | Y | | localhost | | | N | | phenom | | | N | +-----------+------+----------+-------------+ $ mysql -u root -e 'select Host,Db,User,Select_priv from mysql.db;' +------+---------+------+-------------+ | Host | Db | User | Select_priv | +------+---------+------+-------------+ | % | test | | Y | | % | test\_% | | Y | +------+---------+------+-------------+ $
アクセス権限は mysqlデータベース内のシステムテーブルで管理されています
上記は その一部のテーブルの内容を表示してみたものです
mysql.user は 「どの接続元から どのアカウントでアクセスしてきたとき どのパスワードで どんなデータベース管理操作を許可するか」
mysql.db は 「どのアカウントに対して どのデータベーステーブルへの どんな操作を許可するか」
を管理しています
上記のように当初から設定させれている権限は mysql_install_db時に自動設定されたものです
User が空白になっているのは 「アカウント指定なしでの接続 もしくは 間違った指定がされた」場合に該当します
ここでの注意点は
- 他ホストからの接続について 許可設定がないので接続できない
- rootアカウントに対して パスワードが設定されてない
- testデータベースに対して アカウントが省略された状態でも 自由にデータ操作ができてしまう
- Hostに phenom(IPv6ホスト名) や ::1 があるのでIPv6対応を期待させるが 実は対応していない
IPv6については mysqldが IPv6のプロトコルでlistenしていません 下記の通りです
$ netstat -ln -Ainet6 Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp6 0 0 :::389 :::* LISTEN tcp6 0 0 :::139 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN $
MySQLのサービスポートである tcp6/3306 がそもそも開いていません
(ただし IPv4射影IPv6を有効にすると tcp46/3306 が開いたため
IPv4射影IPv6で環境が作れるかもしれません 時間が作れれば実験してみます)
アカウントについては実際の運用で rootアカウントに対してパスワードを設定する必要があるのと
testデータベースが 無アカウント に対して自由に操作できてしまう部分を検討する必要があります
$ mysqladmin -u root password 'root' $ mysql -u root -p -e 'select Host,User,Password,Select_priv from mysql.user;' Enter password: +-----------+------+-------------------------------------------+-------------+ | Host | User | Password | Select_priv | +-----------+------+-------------------------------------------+-------------+ | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | Y | | phenom | root | | Y | | 127.0.0.1 | root | | Y | | ::1 | root | | Y | | localhost | | | N | | phenom | | | N | +-----------+------+-------------------------------------------+-------------+ $
パスワード設定後 rootアカウントで接続する場合はパスワード入力が必須となるので
以後mysqlに -p オプションをつけて パスワードも通知する必要がでてきます
mysql.userテーブルで rootパスワードが反映されているのは Host=’localhost’ and User=’root’ の行のみです
127.0.0.1 の行は 内部で 'localhost'として扱われてしまっていて
(localhost=127.0.0.1 と扱われるのは MySQLの仕様なのか Linuxの仕様なのかは分かりません)
mysql -h 127.0.0.1 -u root で接続しようとしても localhost のパスワードが使われます
従ってこの行自体 存在する必要性はありません
また phenom や ::1 もそれぞれ IPv6のホスト名とアドレスなので
これも現時点では必要性はありません
データベース利用
下記内容を具体的に実践してみます
- アカウントの作成
- データベースの作成
- サンプルデータの投入
- 投入されたデータの確認
- Webとの連携
アカウントの作成
gamer というアカウント を gamer というパスワードで作成し
games というデータベースに TVゲームのデータを格納するのが目的です
通常のアカウントの作成方法は
$ mysql -p -u root Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 37 Server version: 5.5.15 Source distribution Copyright (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 select,update,insert,delete,create,drop on test.* to gamer; Query OK, 0 rows affected (0.00 sec) mysql> set password for gamer = password ( 'gamer' ); Query OK, 0 rows affected (0.00 sec) mysql>
ですが 別の方法も紹介します SQL文だけで同等のアカウント権限処理ができます
$ mysql -p -u root Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 40 Server version: 5.5.15 Source distribution Copyright (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> insert into mysql.user ( host, user, password ) values ( 'localhost', 'gamer', password ( 'gamer' ) ); Query OK, 1 row affected, 3 warnings (0.00 sec) mysql> insert into mysql.db ( host, db, user, select_priv, insert_priv, update_priv, delete_priv, create_priv, drop_priv, references_priv, index_priv, alter_priv, create_tmp_table_priv, lock_tables_priv, create_view_priv, show_view_priv, create_routine_priv, alter_routine_priv, execute_priv, event_priv, trigger_priv ) values ( '%', 'games', 'gamer', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y' ); Query OK, 1 row affected (0.00 sec) mysql >
上記は 直接システムテーブルを修正することで grant文と同等の権限付与を行っています
結果として MySQLのシステムテーブルは下記の通り gamerエントリが追加されています
mysql> select host, user, password, select_priv, insert_priv, update_priv, delete_priv from mysql.user; +-----------+-------+-------------------------------------------+-------------+ | Host | User | Password | Select_priv | +-----------+-------+-------------------------------------------+-------------+ | localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | Y | | phenom | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | Y | | 127.0.0.1 | root | | Y | | ::1 | root | | Y | | localhost | | | N | | phenom | | | N | | localhost | gamer | *2EFB46DA4D8A878D81274BD79E0830422C170CC1 | N | +-----------+-------+-------------------------------------------+-------------+ 7 rows in set (0.00 sec) mysql> select host, db, user, select_priv, insert_priv, update_priv, delete_priv from mysql.db; +------+---------+-------+-------------+-------------+-------------+-------------+ | host | db | user | select_priv | insert_priv | update_priv | delete_priv | +------+---------+-------+-------------+-------------+-------------+-------------+ | % | test | | Y | Y | Y | Y | | % | test\_% | | Y | Y | Y | Y | | % | games | gamer | Y | Y | Y | Y | +------+---------+-------+-------------+-------------+-------------+-------------+ 3 rows in set (0.00 sec) mysql> \q $ mysqladmin -p -u root flush-privileges Enter password: $
最後に mysqladmin flush-privileges で権限関連の設定をシステムに反映させる必要があります
これで localhostから gamerアカウントで接続が可能になっています
1つ目の insertだけでも mysql mysqlshow mysqladmin の接続が可能になります
(mysql.userテーブルの設定一覧で 全デフォルト操作が禁止されていますが
testデータベースへはアクセスできます)
2つ目の insertが長いですが 上記 gamerに対して gamesデータベースで GRANT REVOKE 以外の操作を許可しています
(mysql.dbテーブルの設定一覧で user=” の行で
testデータベースへのアクセス許可設定が入っているので
gamerアカウントでも testへのアクセスが可能です)
最後に アクセス権限に関するデータを直接修正しているので
mysqladmin flush-privileges コマンドで データの内容を MySQLに反映させる必要があります
データベースの作成
事前確認として mysqlshowコマンドを使って 現在のデータベースを一覧表示してみます
$ mysqlshow -p -u gamer Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+ $
information_schema は MySQLの動作ステータス情報をデータベース化したものです
MEMORYエンジンのため MySQLのサーバ終了と共に消えます
mysql や performance_schemaデータベースが一覧にないのは アクセス権限がないためです
gamerアカウントには既に gamesデータベースへの create drop 権限が設定されているので
$ mysql -p -u gamer Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 68 Server version: 5.5.15 Source distribution Copyright (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> create database games default character set utf8; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | games | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> \q Bye $
games データベースの存在が確認できました
showコマンドは 標準のSQL構文ではなく MySQL独自の文法です
またcreate database をする際に default character set utf8 をつけないと
文字化けする可能性があります
(show variables; で表示される下記 文字コードパラメータが不一致だと
mysqlが文字コード変換をかけようとするため注意が必要です
character_set_connection
character_set_database )
次に テーブルを作りますが その前にテーブルのフォーマットを検討するために
サンプルデータ tvgames.zip を用意しました (EUC-JPのテキストです)
サンプルデータの先頭 10行を確認します
$ unzip tvgames.zip Archive: tvgames.zip inflating: tvgames.txt $ head -n 10 tvgames.txt ドンキーコング,FC,任天堂,4500,1983-07-15 ドンキーコングJR.,FC,任天堂,4500,1983-07-15 ポパイ,FC,任天堂,4500,1983-07-15 五目ならべ [連珠],FC,任天堂,4500,1983-08-27 麻雀,FC,任天堂,4500,1983-08-27 マリオブラザーズ,FC,任天堂,4500,1983-09-09 ポパイの英語遊び,FC,任天堂,4500,1983-11-22 ベースボール,FC,任天堂,4500,1983-12-07 ドンキーコングJR. の算数遊び,FC,任天堂,4500,1983-12-12 テニス,FC,任天堂,4500,1984-01-14 $
テレビゲームの歴代タイトルが納められたテキストです ファミコン以外にもあります
テキストは カンマ区切りで タイトル,プラットホーム,メーカー,値段,発売日 となっています
(サンプルデータは インターネット上の某所から拾ったものですが どこからかは忘れてしまいました)
テーブルもこのフォーマットに合わせて作ることにしますが
その前に このテキストを変換します sedコマンドを使って正規表現による置換を行います
$ sed "s/'/\\\\\\\\'/g" tvgames.txt | sed "s/^\\(.*\\),\\(.*\\),\\(.*\\),\\(.*\\),\\(.*\\)$/insert into tvgames ( name, plathome, company, price, release_date ) values ( '\\1', '\\2', '\\3', \\4, '\\5' );/" | iconv -f euc-jp -t utf-8 > tvgame_insert.txt $ head -n 10 tvgame_insert.txt insert into tvgames ( name, plathome, company, price, release_date ) values ( 'ドンキーコング', 'FC', '任天堂', 4500, '1983-07-15' ); insert into tvgames ( name, plathome, company, price, release_date ) values ( 'ドンキーコングJR.', 'FC', '任天堂', 4500, '1983-07-15' ); insert into tvgames ( name, plathome, company, price, release_date ) values ( 'ポパイ', 'FC', '任天堂', 4500, '1983-07-15' ); insert into tvgames ( name, plathome, company, price, release_date ) values ( '五目ならべ [連珠]', 'FC', '任天堂', 4500, '1983-08-27' ); insert into tvgames ( name, plathome, company, price, release_date ) values ( '麻雀', 'FC', '任天堂', 4500, '1983-08-27' ); insert into tvgames ( name, plathome, company, price, release_date ) values ( 'マリオブラザーズ', 'FC', '任天堂', 4500, '1983-09-09' ); insert into tvgames ( name, plathome, company, price, release_date ) values ( 'ポパイの英語遊び', 'FC', '任天堂', 4500, '1983-11-22' ); insert into tvgames ( name, plathome, company, price, release_date ) values ( 'ベースボール', 'FC', '任天堂', 4500, '1983-12-07' ); insert into tvgames ( name, plathome, company, price, release_date ) values ( 'ドンキーコングJR. の算数遊び', 'FC', '任天堂', 4500, '1983-12-12' ); insert into tvgames ( name, plathome, company, price, release_date ) values ( 'テニス', 'FC', '任天堂', 4500, '1984-01-14' ); $
作成した tvgame_insert.txt を流し込む形でテーブルを作成します
$ mysql -p -u gamer Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 69 Server version: 5.5.15 Source distribution Copyright (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> use games Database changed mysql> create table tvgames ( name text, plathome text, company text, price int, release_date date ); Query OK, 0 rows affected (0.00 sec) mysql> show columns from tvgames; +--------------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+---------+------+-----+---------+-------+ | name | text | YES | | NULL | | | plathome | text | YES | | NULL | | | company | text | YES | | NULL | | | price | int(11) | YES | | NULL | | | release_date | date | YES | | NULL | | +--------------+---------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql> \\. tvgame_insert.txt Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) … mysql>
うまくいけば 総数 11429レコード分のデータが投入されているはずです
投入したデータはいろいろな SQL文で検索が可能となります
mysql> select * from tvgames limit 10; +------------------------------+----------+---------+-------+--------------+ | name | plathome | company | price | release_date | +------------------------------+----------+---------+-------+--------------+ | ドンキーコング | FC | 任天堂 | 4500 | 1983-07-15 | | ドンキーコングJR. | FC | 任天堂 | 4500 | 1983-07-15 | | ポパイ | FC | 任天堂 | 4500 | 1983-07-15 | | 五目ならべ [連珠] | FC | 任天堂 | 4500 | 1983-08-27 | | 麻雀 | FC | 任天堂 | 4500 | 1983-08-27 | | マリオブラザーズ | FC | 任天堂 | 4500 | 1983-09-09 | | ポパイの英語遊び | FC | 任天堂 | 4500 | 1983-11-22 | | ベースボール | FC | 任天堂 | 4500 | 1983-12-07 | | ドンキーコングJR. の算数遊び | FC | 任天堂 | 4500 | 1983-12-12 | | テニス | FC | 任天堂 | 4500 | 1984-01-14 | +------------------------------+----------+---------+-------+--------------+ 10 rows in set (0.00 sec) mysql>
データベース上の先頭から 10件
ファミコンが発売された当時 ドンキーコング と ドンキーコングJr. のクオリティの高さに驚きました
mysql> select * from tvgames where release_date between '1995-4-1' and '1995-5-1'; +-------------------------------------------------------------+--------------------+------------------------------------------+-------+--------------+ | name | plathome | company | price | release_date | +-------------------------------------------------------------+--------------------+------------------------------------------+-------+--------------+ | EMIT Vol.2〜命がけの旅〜 | SS | 光栄 | 8800 | 1995-04-01 | | EMIT Vol.3〜私にさよならを〜 | SS | 光栄 | 8800 | 1995-04-01 | | デイトナUSA | SS | セガ | 6800 | 1995-04-01 | | レディーストーカー〜過去からの挑戦〜 | SFC | タイトー | 9980 | 1995-04-01 | | Dの食卓 | 3DO | 三栄書房 | 8800 | 1995-04-01 | | 蒼き伝説シュート! | GB | バンプレスト | 3980 | 1995-04-07 | | THE クイズ ギアファイト! | GG | セガ | 3800 | 1995-04-07 | | クイズ キング・オブ・ファイターズ | NG(CD) | ザウルス | 5800 | 1995-04-07 | | 得点王3 | NG | SNK | 29800 | 1995-04-07 | | フォーメーションサッカー95 della セリエA | PCE(SCDR2)(AC対応) | ヒューマン | 9800 | 1995-04-07 | | 全日本プロレス2 3・4武闘館 | SFC | 日本コンピュータシステム | 10800 | 1995-04-07 | | ドラえもん 友情伝説 | 3DO | 小学館 | 7800 | 1995-04-07 | | SD飛龍の拳外伝 | GB | カルチャーブレーン | 4700 | 1995-04-14 | | 勝馬予想 競馬貴族EX'95 | GB | キングレコード | 6900 | 1995-04-14 | | ワールドヒーローズ2 | NG(CD) | ADK | 5800 | 1995-04-14 | | パチ夫くん パチンコランド大冒険 | PS | ココナッツジャパン | 6800 | 1995-04-14 | | 柿木将棋 | SS | アスキー | 7800 | 1995-04-14 | | ぱずるんでス! | SFC | 日本物産 | 8980 | 1995-04-14 | | RES ARCANA | SFC | ココナッツジャパン | 9500 | 1995-04-14 | | EMIT Vol.1 時の迷子 | 3DO | 光栄 | 8800 | 1995-04-14 | | デビルズコース | 3DO | 松下電器 | 8800 | 1995-04-14 | | パドックノート’95 | 3DO | フジテレビジョン | 8800 | 1995-04-14 | | MYST | 3DO | マイクロキャビン | 7800 | 1995-04-14 | | Jリーグ ライブ'95 | GB | エレクトロニック・アーツ・ビクター | 5980 | 1995-04-21 | | カオティクス | 32X | SEGA | 7800 | 1995-04-21 | | 餓狼伝説3 ROAD TO THE FINALVICTORY | NG | SNK | 32000 | 1995-04-21 | | ギャラクシーファイト UNIVERSAL WARRIORS | NG(CD) | サンソフト | 7800 | 1995-04-21 | | サイバーリップ | NG(CD) | SNK | 5800 | 1995-04-21 | | ファイヤースープレックス | NG(CD) | SNK | 5800 | 1995-04-21 | | ベースボールスターズプロフェッショナル | NG(CD) | SNK | 4800 | 1995-04-21 | | ロボアーミー | NG(CD) | SNK | 5800 | 1995-04-21 | | 金沢将棋'95 | PS | セタ | 7900 | 1995-04-21 | | ママレード・ボーイ | SFC | バンダイ | 9800 | 1995-04-21 | | 魔法陣グルグル | SFC | エニックス | 10800 | 1995-04-21 | | タクティカルサッカー | SFC | エレクトロニックアーツビクター | 9800 | 1995-04-21 | | なつきクライシスバトル | SFC | エンジェル | 10800 | 1995-04-21 | | 真・聖刻 | SFC | ユタカ | 9800 | 1995-04-21 | | 真SD戦国伝 大将軍列伝 | SFC | ベック | 9800 | 1995-04-21 | | スーパーリアル麻雀P5 パラダイス オールスター4人打ち | SFC | セタ | 9800 | 1995-04-21 | | リジョイス〜アレサ王国の彼方〜 | SFC | やのまん | 9900 | 1995-04-21 | | 宮路社長のパチンコファン勝利宣言2 | SFC | POW | 9800 | 1995-04-21 | | スーパートランプコレクション | SFC | ボトムアップ | 8900 | 1995-04-21 | | 紺碧の艦隊 | 3DO | 徳間書店 | 8800 | 1995-04-21 | | ぼのぐらし | 3DO | アミューズ/バンダイビジュアル | 8800 | 1995-04-21 | | ポリスノーツ パイロットディスク | 3DO | コナミ | 2980 | 1995-04-21 | | パズルボブル | NG(CD) | タイトー | 6800 | 1995-04-27 | | 赤ずきんチャチャ | GB | トミー | 3900 | 1995-04-28 | | 空想科学少年ガリバーボーイ 空想科学パズル プリッとポン | GB | バンダイ | 3980 | 1995-04-28 | | チキチキ天国 | GB | J・ウイング | 3900 | 1995-04-28 | | パチ夫くん3 | GB | ココナッツジャパン | 4800 | 1995-04-28 | | 魔法陣グルグル 勇者とククリの大冒険 | GB | タカラ | 3980 | 1995-04-28 | | TEMPO Jr. | GG | セガ | 3800 | 1995-04-28 | | テイルスのスカイパトロール | GG | セガ | 3800 | 1995-04-28 | | テレビアニメスラムダンク 強豪真っ向対決! | MD | バンダイ | 8800 | 1995-04-28 | | トゥルーライズ | MD | アクレイムジャパン | 7800 | 1995-04-28 | | 餓狼伝説3 ROAD TO THE FINALVICTORY | NG(CD) | SNK | 8800 | 1995-04-28 | | スロット勝負師 | PCE(SCDR2) | 日本物産 | 8500 | 1995-04-28 | | ガンナーズ ヘブン | PS | ソニー・コンピュータエンタテインメント | 5800 | 1995-04-28 | | ぐっすんおよよ | PS | エクシング | 6800 | 1995-04-28 | | ジャンピングフラッシュ!アロハ男爵ファンキー大作戦の巻 | PS | ソニー・コンピュータエンタテインメント | 5800 | 1995-04-28 | | ミスランド まちがい探しゲーム | PS | アルトロン | 5800 | 1995-04-28 | | ヴァーチャル ハイドライド | SS | セガ | 5800 | 1995-04-28 | | アイルトン・セナ パーソナルトーク〜Message for the future〜 | SS | セガ | 8800 | 1995-04-28 | | 輝水晶伝説アスタル | SS | セガ | 5800 | 1995-04-28 | | 三國志4 | SS | 光栄 | 14800 | 1995-04-28 | | スーパーボンバーマン3 | SFC | ハドソン | 8900 | 1995-04-28 | | スーパーパチンコ大戦 | SFC | バンプレスト | 6900 | 1995-04-28 | | 牌勢麻雀 凌駕 | SFC | アスキー | 12800 | 1995-04-28 | | プラネットチャンプ TG3000 | SFC | ケムコ | 9500 | 1995-04-28 | | Jリーグエキサイトステージ'95 | SFC | エポック社 | 9800 | 1995-04-28 | | 川のぬし釣り2 | SFC | パックインビデオ | 10800 | 1995-04-28 | | シミュレーションプロ野球 | SFC | ヘクト | 12800 | 1995-04-28 | | トゥルーライズ | SFC | アクレイムジャパン | 10900 | 1995-04-28 | | 3次元格闘ボールズ | SFC | メディアリング | 9800 | 1995-04-28 | | タロットミステリー | SFC | ヴィジット | 9800 | 1995-04-28 | | 初段位認定 初段プロ麻雀 | SFC | ギャップス | 9800 | 1995-04-28 | | バーチャルカメラマンPart3 | 3DO | ナグザット | 8800 | 1995-04-28 | | ワールドカップ スーパースタジアム | 3DO | テレビ東京/ギャガ・コミュニケーションズ | 6800 | 1995-04-28 | +-------------------------------------------------------------+--------------------+------------------------------------------+-------+--------------+ 78 rows in set (0.01 sec) mysql>
1995年4月1日〜5月1日 の間に発売されたソフトです (78本も)
当時はちょうど 新旧ハードの世代交替の時期であり 多種のハードが入り混じっています
mysql> select plathome, count( name ) from tvgames group by plathome; +------------------------+---------------+ | plathome | count( name ) | +------------------------+---------------+ | 32X | 18 | | 3DO | 215 | | 64DD | 7 | | ARCADE CARD専用 | 12 | | FC | 1042 | | FCD | 195 | | FC周辺ソフト | 9 | | GB | 767 | | GBC | 142 | | GB&GBC | 201 | | GG | 196 | | MCD | 110 | | MD | 416 | | N64 | 200 | | NG | 117 | | NG(CD) | 99 | | PCE | 229 | | PCE(CDR2) | 92 | | PCE(CDR2)(S対応) | 12 | | PCE(SCDR2) | 235 | | PCE(SCDR2)(AC対応) | 16 | | PCE(SG) | 4 | | PS | 4439 | | SFC | 1391 | | SFC(NP) | 33 | | SS | 1212 | | VB | 19 | | ※バーコードボーイ同梱 | 1 | +------------------------+---------------+ 28 rows in set (0.14 sec) mysql>
SQLの group by 構文で集計を行えます
上記は プラットホーム毎のソフト本数の集計です
PlayStationが圧倒的です
最後に tvgamesテーブルを 2つ結合して検索をかける複雑な検索の例です
名称の先頭部分が一致することを条件にして 5作以上シリーズ化されているゲームを抜き出そうとしてます
例えば「ドラゴンクエスト」をベース名として「ドラゴンクエスト2」「ドラゴンクエスト3」…
を抜き出そうとしています
mysql> select gbase.name, gelement.name, gelement.release_date, gelement.plathome, gelement.price from ( select distinct name from tvgames ) as gbase, tvgames gelement where octet_length( gbase.name ) > 3 and left( gelement.name, char_length( gbase.name ) ) = gbase.name and ( select count( name ) from tvgames gsub where gbase.name != gsub.name and left( gsub.name, char_length( gbase.name ) ) = gbase.name ) >= 4 order by gbase.name, gelement.release_date; 〜 〜 | 高橋名人の冒険島 | 高橋名人の冒険島3 | 1993-02-26 | GB | 3800 | | 高橋名人の冒険島 | 高橋名人の冒険島4 | 1994-06-24 | FC | 5800 | | 魔導物語 | 魔導物語 1 3つの魔導球 | 1993-12-03 | GG | 5500 | | 魔導物語 | 魔導物語 2 アルル16歳 | 1994-05-20 | GG | 5500 | | 魔導物語 | 魔導物語 3 究極女王様 | 1994-11-25 | GG | 5500 | | 魔導物語 | 魔導物語A ドキドキばけ〜しょん | 1995-11-24 | GG | 5500 | | 魔導物語 | 魔導物語〜はなまる大幼稚園児〜 | 1996-01-12 | SFC | 9900 | | 魔導物語 | 魔導物語I 炎の卒園児 | 1996-12-13 | ARCADE CARD専用 | 7800 | | 魔導物語 | 魔導物語 | 1998-07-23 | SS | 5800 | | 麻雀 | 麻雀 | 1983-08-27 | FC | 4500 | | 麻雀 | 麻雀 | 1986-02-21 | FCD | 4900 | | 麻雀 | 麻雀悟空 プロフェッショナル | 1986-12-25 | FCD | 2900 | | 麻雀 | 麻雀家族 | 1987-08-04 | FCD | 2980 | | 麻雀 | 麻雀大会 | 1989-10-31 | FC | 7800 | | 麻雀 | 麻雀学園 東間宗四郎登場 | 1989-11-24 | PCE | 7980 | | 麻雀 | 麻雀刺客列伝 麻雀ウォーズ | 1990-02-01 | PCE | 5400 | | 麻雀 | 麻雀学園MILD | 1990-06-29 | PCE | 7980 | | 麻雀 | 麻雀倶楽部永田町 総裁戦 | 1991-04-25 | FC | 9700 | | 麻雀 | 麻雀狂列伝 西日本編 | 1991-07-01 | NG | 15800 | | 麻雀 | 麻雀覇王伝カイザークエスト | 1992-02-28 | PCE | 7200 | | 麻雀 | 麻雀大戦 | 1992-05-20 | FC | 6400 | | 麻雀 | 麻雀飛翔伝 哭きの龍 | 1992-12-25 | SFC | 9800 | | 麻雀 | 麻雀クリニックスペシャル | 1993-09-24 | PCE(SCDR2) | 7800 | | 麻雀 | 麻雀オンザビーチ | 1993-09-30 | PCE(SCDR2) | 7800 | | 麻雀 | 麻雀レモンエンジェル | 1994-02-25 | PCE(SCDR2) | 8800 | | 麻雀 | 麻雀悟空 天竺 | 1994-06-25 | 3DO | 7800 | | 麻雀 | 麻雀狂時代 AVギャル制服編 | 1994-07-20 | 3DO | 9800 | | 麻雀 | 麻雀悟空 天竺 | 1994-08-19 | SFC | 9800 | | 麻雀 | 麻雀狂列伝 西日本編 | 1994-09-09 | NG(CD) | 4800 | | 麻雀 | 麻雀戦国物語 | 1994-09-23 | SFC | 9300 | | 麻雀 | 麻雀大会2 | 1994-09-30 | SFC | 9800 | | 麻雀 | 麻雀悟空 天竺 | 1994-11-22 | SS | 5800 | | 麻雀 | 麻雀悟空 天竺 | 1994-12-03 | PS | 5800 | | 麻雀 | 麻雀ステーションMAZIN〜麻神〜 | 1994-12-03 | PS | 6000 | | 麻雀 | 麻雀倶楽部 | 1994-12-22 | SFC | 4980 | | 麻雀 | 麻雀巌流島 | 1995-03-10 | SS | 6800 | | 麻雀 | 麻雀巌流島 | 1995-07-07 | PS | 6800 | | 麻雀 | 麻雀繁盛記 | 1995-07-28 | SFC | 6800 | | 麻雀 | 麻雀海岸物語〜麻雀狂時代 セクシーアイドル編〜 | 1995-08-04 | SS | 6800 | | 麻雀 | 麻雀狂時代 コギャル放課後編 | 1995-10-18 | 3DO | 9800 | | 麻雀 | 麻雀飛翔伝 真 哭きの巻 | 1995-10-27 | SFC | 8900 | | 麻雀 | 麻雀狂時代 コギャル放課後編 | 1996-01-12 | SS | 8800 | | 麻雀 | 麻雀ハイパーリアクションR | 1996-03-08 | SS | 6800 | | 麻雀 | 麻雀天使エンジェルリップス | 1996-03-29 | SS | 6800 | | 麻雀 | 麻雀同級生Special | 1996-03-29 | SS | 5800 | | 麻雀 | 麻雀同級生Special | 1996-03-29 | SS | 8800 | | 麻雀 | 麻雀狂時代 Cebu Island'96 | 1996-07-19 | SS | 8800 | | 麻雀 | 麻雀四姉妹 若草物語 | 1996-09-27 | SS | 8800 | | 麻雀 | 麻雀大会2 Special | 1996-10-04 | SS | 6800 | | 麻雀 | 麻雀大会2Special | 1996-11-29 | PS | 6800 | | 麻雀 | 麻雀 MASTER | 1996-12-20 | N64 | 9800 | | 麻雀 | 麻雀64 | 1997-04-04 | N64 | 7800 | | 麻雀 | 麻雀放浪記CLASSIC | 1997-08-01 | N64 | 7900 | | 麻雀 | 麻雀学園祭 | 1997-11-06 | SS | 6800 | | 麻雀 | 麻雀学園祭 | 1997-11-06 | SS | 8800 | | 麻雀 | 麻雀幼稚園 たまご組 | 1998-03-12 | PS | 5800 | | 麻雀 | 麻雀倶楽部 | 1998-04-09 | PS | 4800 | | 麻雀 | 麻雀学園祭DX〜前日にまつわる奮戦記〜 | 1998-09-23 | SS | 5800 | | 麻雀 | 麻雀クエスト | 1998-12-23 | GB&GBC | 3980 | | 麻雀 | 麻雀やろうぜ! | 1999-04-28 | PS | 3800 | | 麻雀 | 麻雀悟空 天竺 99 | 1999-06-03 | PS | 1800 | | 麻雀 | 麻雀幼稚園たまご組R | 2000-01-13 | PS | 1500 | | 麻雀 | 麻雀鳥頭紀行 | 2000-02-24 | PS | 5800 | | 麻雀 | 麻雀女王 | 2000-04-28 | GB&GBC | 3980 | | 麻雀 | 麻雀幼稚園たまご組2 0からの麻雀 | 2000-09-28 | PS | 3800 | | 麻雀 | 麻雀占い フォルトゥーナ 〜月の女神達〜 | 2001-02-01 | PS | 3800 | +-------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+--------------------+-------+ 1750 rows in set (2 min 18.77 sec) mysql>
1万本 × 1万本 の全組み合わせを計算しにかかるので 相当な検索負荷となります
クアッドコア 2.5GHzの検証システムでは 2分20秒ほどかかりました
シングルコア 2GHzの時では 5分近くかかったのを考えるとシステム処理能力に応じた結果です
トラブルシューティング
実際に遭遇したトラブルです
「Access Denied」とやたら出て データベース操作ができない
1. tmpdir が存在しないか確認
/etc/my.cnf の内容から tmpdir の場所を確認します
例えば tmpdir=/tmp/mysql となっていた場合
/tmp/mysql/ ディレクトリが mysql(MySQL管理者権限)でアクセス可能か調べてください
$ ls -l /tmp | grep mysql
drwx------ 2 mysql users 4096 12月 13 14:04 mysql
srwxrwxrwx 1 mysql mysql 0 12月 13 14:04 mysql.sock
$
tmpdirディレクトリが存在しなかったり アクセス権限が不適切だと
mysql_install_db 時にいろいろ文句を言われているはずです
tmpdirディレクトリが未作成だった場合
一旦 mysqld を停止させて tmpdirディレクトリを作成すれば回復すると思います
2. MySQLでの権限設定に失敗していた
必要なアクセス権限許可が削除されていた場合などです
既にデータベースには重要なデータが入っていて mysql_install_db しなおすのは面倒な場合
次の方法で アクセス権限を設定してみてください
2-a. 現行の mysqld を停止させる
2-b. mysqld を以下の方法で起動させる
$ su
# /usr/local/libexec/mysqld --skip-grant
プロンプトが帰ってきませんが フォアグラウンドで mysqldが起動しているためです
認証が無効化されていて セキュリティ上危険な状態ですので 権限設定を素早く行います
別の端末から
$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.15 Source distribution
Copyright (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>
これで接続ができるので mysql.user や mysql.db などへ必要な修正を行います
(mysql-5.5系は –skip-grant 中に set password 命令が使えなくなっています)
終わったら 素早く mysqldを shutdownします (mysqladmin shutdown を使う)
フォアグラウンドで動いていた mysqldが終了してプロンプトが帰ってくるはずです
3. ローカルホストでは接続できる(-hオプションなし) のに TCP接続(-h 自ホスト名) で接続できない
これは mysql.user テーブルの権限設定ミスです
この場合 mysql.user テーブル上では host が 「myhost.mydomain」となっているのに
MySQLへの接続時のエラーメッセージが 「ERROR 1130 (HY000): Host ‘myhost’ is not allowed 〜」
と 「.mydomain」 の有無で食い違っていることで確認できます
以下の作業で mysql.user や mysql.db テーブルを更新することで
自ホストへ TCP接続できるようになります
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.15 Source distribution
Copyright (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> update mysql.user set host = 'myhost' where host='myhost.mydomain'
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> update mysql.db set host = 'myhost' where host = &aposmyhost.mydomain'
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> quit
Bye
$ mysqladmin -u root -p flush-privileges
Enter password:
$
mysqladmin flush-privileges により mysqlデータベースの更新が反映されます
「Access Denied」とやたら出て データベース操作ができない
1. tmpdir が存在しないか確認
/etc/my.cnf の内容から tmpdir の場所を確認します
例えば tmpdir=/tmp/mysql となっていた場合
/tmp/mysql/ ディレクトリが mysql(MySQL管理者権限)でアクセス可能か調べてください
$ ls -l /tmp | grep mysql drwx------ 2 mysql users 4096 12月 13 14:04 mysql srwxrwxrwx 1 mysql mysql 0 12月 13 14:04 mysql.sock $
tmpdirディレクトリが存在しなかったり アクセス権限が不適切だと
mysql_install_db 時にいろいろ文句を言われているはずです
tmpdirディレクトリが未作成だった場合
一旦 mysqld を停止させて tmpdirディレクトリを作成すれば回復すると思います
2. MySQLでの権限設定に失敗していた
必要なアクセス権限許可が削除されていた場合などです
既にデータベースには重要なデータが入っていて mysql_install_db しなおすのは面倒な場合
次の方法で アクセス権限を設定してみてください
2-a. 現行の mysqld を停止させる
2-b. mysqld を以下の方法で起動させる
$ su # /usr/local/libexec/mysqld --skip-grant
プロンプトが帰ってきませんが フォアグラウンドで mysqldが起動しているためです
認証が無効化されていて セキュリティ上危険な状態ですので 権限設定を素早く行います
別の端末から
$ mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.15 Source distribution Copyright (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>
これで接続ができるので mysql.user や mysql.db などへ必要な修正を行います
(mysql-5.5系は –skip-grant 中に set password 命令が使えなくなっています)
終わったら 素早く mysqldを shutdownします (mysqladmin shutdown を使う)
フォアグラウンドで動いていた mysqldが終了してプロンプトが帰ってくるはずです
3. ローカルホストでは接続できる(-hオプションなし) のに TCP接続(-h 自ホスト名) で接続できない
これは mysql.user テーブルの権限設定ミスです
この場合 mysql.user テーブル上では host が 「myhost.mydomain」となっているのに
MySQLへの接続時のエラーメッセージが 「ERROR 1130 (HY000): Host ‘myhost’ is not allowed 〜」
と 「.mydomain」 の有無で食い違っていることで確認できます
以下の作業で mysql.user や mysql.db テーブルを更新することで
自ホストへ TCP接続できるようになります
$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.15 Source distribution Copyright (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> update mysql.user set host = 'myhost' where host='myhost.mydomain' Query OK, 2 rows affected (0.01 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> update mysql.db set host = 'myhost' where host = &aposmyhost.mydomain' Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 mysql> quit Bye $ mysqladmin -u root -p flush-privileges Enter password: $
mysqladmin flush-privileges により mysqlデータベースの更新が反映されます