MySQL 5.5 インストール メモ

MySQL の導入にかかわるメモがふたつにわかれていたので統合.というわけでちょっとつまづいたお話.

ports

databases/mysql55-serverMySQL 5.5 (2011 年 7 月時点での安定版) がおいてある.ほかにも開発版や古い版などがある.

同様に databases/mysql55-client にはサーバーにアクセスするためのクライアントがおいてある.

いつもどおり ports からインストール.

# pkg_replace -N databases/mysql55-server

起動前の設定

ここから先はこちらも読んだほうがいいとおもいます.

既定では日本語を扱うことを考慮していないため,文字コードの設定を変更する.

ports からインストールした場合,設定ファイルは mysql_dbdir に置かれているファイルを読み込む*1ようになっており*2,既定では /var/db/mysql に設定されている.変えたければ rc.conf で指定してもいい.

/var/db/mysql/my.cnf
[mysqld]
character_set_server = utf8
collation_server = utf8_general_ci
default-character-set=utf8
skip-character-set-client-handshake

default-character-set は非推奨でした.というわけで character_set_server を使いましょう.

skip-character-set-client-handshake はもう mysqld オプションの一覧に載ってないようなので消しておきました.

また,先行するものを除いて,オプション名中のダッシュとアンダースコアは区別されない.

Within option names, dash (“-”) and underscore (“_”) may be used interchangeably. For example, --skip-grant-tables and --skip_grant_tables are equivalent. (However, the leading dashes cannot be given as underscores.)

http://dev.mysql.com/doc/refman/5.5/en/command-line-options.html

起動

まずは rc.conf から.

/etc/rc.conf
mysql_enable="yes"

mysqld_enable じゃない.注意.

マシンを再起動しない場合はとりあえず自分で起動させる.ちなみにこのスクリプトは (初回起動時に) 必要な処理もやってくれるようだ.

# /usr/local/etc/rc.d/mysql-server start
Starting mysql.

起動後の初期設定

初期設定には mysql_secure_installation を使う.

% mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):

mysql_secure_installation では root のパスワードを設定したり,ゲスト ユーザーを削除できる.

初期状態では,どのユーザーにもパスワードが割り当てられていない.そのためスーパー ユーザーのパスワードを設定する*3.斜体部分は置き換えるべし.

3 行目ではホスト名*4を指定する.ローカル ホスト以外からの接続は,ローカル ホストからの接続とは区別されるようだ.

% mysql -u user -p

mysql> select host, user from mysql.user;
mysql> set password for 'root'@'localhost' = password('password');
mysql> set password for 'root'@'host_name' = password('password');

また,匿名アカウントを削除した.

mysql> delete from mysql.user where user = '';
mysql> flush privileges;

*1:--defaults-extra-file

*2:http://svnweb.freebsd.org/ports/branches/RELENG_9_1_0/databases/mysql55-server/files/mysql-server.in?view=log

*3:もっといいクエリ文があれば教えてください

*4:1 行目で調べている