MySQL5を入れてみた。

MySQL5の本体からインストール。

こんな感じ。
無線LAN経由のせいなのか、他のせいなのか不明だけど止まったのかと思うぐらい待つ。
ひたすら待つので、完了まで時間がかかっても止めちゃ駄目。

$ sudo port install mysql5 +server
Password:
---> Fetching mysql5
---> Attempting to fetch mysql-5.0.77.tar.gz from http://mysql.oss.eznetsols.org/Downloads/MySQL-5.0/
---> Verifying checksum(s) for mysql5
---> Extracting mysql5
---> Applying patches to mysql5
---> Configuring mysql5
---> Building mysql5
---> Staging mysql5 into destroot
---> Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting mysql5 with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
###########################################################
---> Installing mysql5 @5.0.77_0+server
******************************************************
* In order to setup the database, you might want to run
* sudo -u mysql mysql_install_db5
* if this is a new install
******************************************************
---> Activating mysql5 @5.0.77_0+server
---> Cleaning mysql5
$

5分以上は待ったはず。
入れたのを削除するときには、

$sudo port uninstall mysql5

だそうだ。入れるのと逆だな。特別なことはないっぽい。

MySQLの初期化

どこがどうなるのか調べてないが、初期化をかけるらしい。
win版やLinux版ではやった覚えはないのだが。

$ sudo -u mysql mysql_install_db5
Password:
Installing MySQL system tables...
090224 23:48:00 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
090224 23:48:00 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
OK
Filling help tables...
090224 23:48:02 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
090224 23:48:02 [Warning] option 'max_join_size': unsigned value 18446744073709551615 adjusted to 4294967295
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:
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h xxxxxxxxxx-no-macbook.local password 'new-password'

Alternatively you can run:
/opt/local/lib/mysql5/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 /opt/local ; /opt/local/lib/mysql5/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 /opt/local/lib/mysql5/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
$

MySQLの起動

最初、パスワードを設定しようとしたら、失敗した。
MySQLが動いてないので、アクセスできなかったらしい。

$ /opt/local/lib/mysql5/bin/mysqladmin -u root password 'xxxxxxxxxx'
/opt/local/lib/mysql5/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)'
Check that mysqld is running and that the socket: '/opt/local/var/run/mysql5/mysqld.sock' exists!
$

なので、起動してみた。

$ sudo /opt/local/share/mysql5/mysql/mysql.server start
Starting MySQL
.. SUCCESS!
$

MySQLのユーザにパスワードを設定した

やってみたのは、こんな感じ。
最初にDBへの接続に失敗したのと同じコマンド

$ /opt/local/lib/mysql5/bin/mysqladmin -u root password 'xxxxxxxxxx'
$

再起動せずに、パスワードが有効か試してみた。

$ mysql5 -u root -pEnter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> exit
Bye
$ mysql5 -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
$

上手くいったようだ

自動起動を設定する

必要性は薄いけど、自動起動を設定してみた。

$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
Password:
$

なんの返事もなく完了した。
自動起動から外すのはどうやるんだ?
これは宿題にする。

手動で起動と停止

コマンドはこれのようだ。

$ sudo launchctl stop org.macports.mysql5
Password:
$ sudo launchctl start org.macports.mysql5
$

落ちたり、あがったりしてるっぽい。

MySQL文字コード設定

設定後は再起動をすること。

sudo cp /opt/local/share/mysql5/mysql/my-small.cnf /opt/local/etc/mysql5/my.cnf

文字コードの変更を追記
etcの下に置いたmy.cnfに追記

sudo vi /opt/local/etc/mysql5/my.cnf

viのコマンドが不明の場合は調べること。意外に簡単。

[mysqld]
default-character-set = utf8
default-storage-engine = innodb

書いたら保存。
再起動は忘れずに。
再起動するまでは反映されない。
念のため確認。

mysql> status

                          • -

mysql5 Ver 14.12 Distrib 5.0.77, for apple-darwin9.6.0 (i386) using EditLine wrapper

Connection id: 1
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.0.77 Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /opt/local/var/run/mysql5/mysqld.sock
Uptime: 4 min 52 sec

Threads: 1 Questions: 4 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 3 Queries per second avg: 0.014

                          • -

mysql>

気になるのが二行

Client characterset: latin1
Conn. characterset: latin1

latin1とはなんじゃらほい。
設定した方が良さそうな気がしたから、設定してみる。
再び、etcに置いたmy.cnfを更新して再起動してみた。
my.cnfはこんな感じに、[client]に一行追記した。

[client]
default-character-set = utf8
#password = your_password
port = 3306
socket = /opt/local/var/run/mysql5/mysqld.sock

そしたら、結果はこれ。
めでたしめでたし。

$ mysql5 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> status

                          • -

mysql5 Ver 14.12 Distrib 5.0.77, for apple-darwin9.6.0 (i386) using EditLine wrapper

Connection id: 1
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.0.77 Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /opt/local/var/run/mysql5/mysqld.sock
Uptime: 16 sec

Threads: 1 Questions: 4 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 3 Queries per second avg: 0.250

                          • -

mysql> quit
Bye