OS: Debian GNU/Linux 6.0 squeeze


# uname -mrsvo
Linux 2.6.32-5-amd64 #1 SMP Thu Nov 3 03:41:26 UTC 2011 x86_64 GNU/Linux

インストール可能なパッケージを検索。


# aptitude search ^mysql
p   mysql-admin              - 直感的な MySQL 管理のための GUI ツール
p   mysql-client             - MySQL データベースクライアント (最新版に依存するメタパッケージ)
v   mysql-client-4.1         - 
p   mysql-client-5.1         - MySQL データベースクライアントバイナリ
p   mysql-common             - MySQL データベース共通ファイル (/etc/mysql/my.cnf など)
v   mysql-common-4.1         - 
p   mysql-gui-tools-common   - Architecture independent files for MySQL GUI Tools
p   mysql-mmm-agent          - Multi-Master Replication Manager for MySQL - agent daemon
p   mysql-mmm-common         - Multi-Master Replication Manager for MySQL - common files
p   mysql-mmm-monitor        - Multi-Master Replication Manager for MySQL - monitoring daemon
p   mysql-mmm-tools          - Multi-Master Replication Manager for MySQL - tools
p   mysql-navigator          - MySQL データベースサーバ用 GUI クライアントプログラム
p   mysql-proxy              - high availability, load balancing and query modification for mysql
p   mysql-query-browser      - MySQL データベース問い合わせ用公式 GUI ツール
p   mysql-server             - MySQL データベースサーバ (最新版に依存するメタパッケージ)
v   mysql-server-5.0         - 
p   mysql-server-5.1         - MySQL データベースサーババイナリおよびシステムデータベースの設定
v   mysql-server-core        - 
v   mysql-server-core-5.0    - 
p   mysql-server-core-5.1    - MySQL データベースサーババイナリ
p   mysqltcl                 - Interface to the MySQL database for the Tcl language
p   mysqltuner               - high-performance MySQL tuning script

MySQL のパッケージをインストール。

インストールの途中で root のパスワードを設定するダイアログが表示されるので、任意のパスワードを設定しておく。


# aptitude install mysql-server
以下の新規パッケージがインストールされます:
  libdbd-mysql-perl{a} libdbi-perl{a} libhtml-template-perl{a} 
  libmysqlclient16{a} libnet-daemon-perl{a} libplrpc-perl{a} 
  mysql-client-5.1{a} mysql-common{a} mysql-server mysql-server-5.1{a} 
  mysql-server-core-5.1{a} 
更新: 0 個、新規インストール: 11 個、削除: 0 個、保留: 8 個。
24.1 MB のアーカイブを取得する必要があります。展開後に 61.1 MB のディスク領域が新たに消費されます。
先に進みますか? [Y/n/?] y
取得:1 http://cdn.debian.net/debian/ squeeze/main mysql-common all 5.1.49-3 [70.9 kB]
取得:2 http://cdn.debian.net/debian/ squeeze/main libnet-daemon-perl all 0.43-1 [46.6 kB]
取得:3 http://cdn.debian.net/debian/ squeeze/main libplrpc-perl all 0.2020-2 [36.0 kB]
取得:4 http://cdn.debian.net/debian/ squeeze/main libdbi-perl amd64 1.612-1 [883 kB]
取得:5 http://cdn.debian.net/debian/ squeeze/main libmysqlclient16 amd64 5.1.49-3 [1,985 kB]
取得:6 http://cdn.debian.net/debian/ squeeze/main libdbd-mysql-perl amd64 4.016-1 [140 kB]
取得:7 http://cdn.debian.net/debian/ squeeze/main mysql-client-5.1 amd64 5.1.49-3 [10.1 MB]
取得:8 http://cdn.debian.net/debian/ squeeze/main mysql-server-core-5.1 amd64 5.1.49-3 [4,124 kB]
取得:9 http://cdn.debian.net/debian/ squeeze/main mysql-server-5.1 amd64 5.1.49-3 [6,598 kB]
取得:10 http://cdn.debian.net/debian/ squeeze/main libhtml-template-perl all 2.9-2 [65.9 kB]
取得:11 http://cdn.debian.net/debian/ squeeze/main mysql-server all 5.1.49-3 [65.4 kB]
24.1 MB を 2秒 秒でダウンロードしました (8,101 kB/s)
パッケージを事前設定しています ...
未選択パッケージ mysql-common を選択しています。
(データベースを読み込んでいます ... 現在 33411 個のファイルとディレクトリがインストールされています。)
(.../mysql-common_5.1.49-3_all.deb から) mysql-common を展開しています...
未選択パッケージ libnet-daemon-perl を選択しています。
(.../libnet-daemon-perl_0.43-1_all.deb から) libnet-daemon-perl を展開しています...
未選択パッケージ libplrpc-perl を選択しています。
(.../libplrpc-perl_0.2020-2_all.deb から) libplrpc-perl を展開しています...
未選択パッケージ libdbi-perl を選択しています。
(.../libdbi-perl_1.612-1_amd64.deb から) libdbi-perl を展開しています...
未選択パッケージ libmysqlclient16 を選択しています。
(.../libmysqlclient16_5.1.49-3_amd64.deb から) libmysqlclient16 を展開しています...
未選択パッケージ libdbd-mysql-perl を選択しています。
(.../libdbd-mysql-perl_4.016-1_amd64.deb から) libdbd-mysql-perl を展開しています...
未選択パッケージ mysql-client-5.1 を選択しています。
(.../mysql-client-5.1_5.1.49-3_amd64.deb から) mysql-client-5.1 を展開しています...
未選択パッケージ mysql-server-core-5.1 を選択しています。
(.../mysql-server-core-5.1_5.1.49-3_amd64.deb から) mysql-server-core-5.1 を展開しています...
man-db のトリガを処理しています ...
mysql-common (5.1.49-3) を設定しています ...
未選択パッケージ mysql-server-5.1 を選択しています。
(データベースを読み込んでいます ... 現在 33787 個のファイルとディレクトリがインストールされています。)
(.../mysql-server-5.1_5.1.49-3_amd64.deb から) mysql-server-5.1 を展開しています...
未選択パッケージ libhtml-template-perl を選択しています。
(.../libhtml-template-perl_2.9-2_all.deb から) libhtml-template-perl を展開しています...
未選択パッケージ mysql-server を選択しています。
(.../mysql-server_5.1.49-3_all.deb から) mysql-server を展開しています...
man-db のトリガを処理しています ...
libnet-daemon-perl (0.43-1) を設定しています ...
libplrpc-perl (0.2020-2) を設定しています ...
libdbi-perl (1.612-1) を設定しています ...
libmysqlclient16 (5.1.49-3) を設定しています ...
libdbd-mysql-perl (4.016-1) を設定しています ...
mysql-client-5.1 (5.1.49-3) を設定しています ...
mysql-server-core-5.1 (5.1.49-3) を設定しています ...
mysql-server-5.1 (5.1.49-3) を設定しています ...
Stopping MySQL database server: mysqld.
120121  8:40:43 [Note] Plugin 'FEDERATED' is disabled.
120121  8:40:43  InnoDB: Started; log sequence number 0 44233
120121  8:40:43  InnoDB: Starting shutdown...
120121  8:40:49  InnoDB: Shutdown completed; log sequence number 0 44233
Starting MySQL database server: mysqld.
Checking for corrupt, not cleanly closed and upgrade needing tables..
libhtml-template-perl (2.9-2) を設定しています ...
mysql-server (5.1.49-3) を設定しています ...

インストールされたパッケージを確認。


# COLUMNS=200 dpkg -l | grep mysql
ii  libdbd-mysql-perl       4.016-1   Perl5 database interface to the MySQL database
ii  libmysqlclient16        5.1.49-3  MySQL database client library
ii  mysql-client-5.1        5.1.49-3  MySQL database client binaries
ii  mysql-common            5.1.49-3  MySQL database common files, e.g. /etc/mysql/my.cnf
ii  mysql-server            5.1.49-3  MySQL database server (metapackage depending on the latest version)
ii  mysql-server-5.1        5.1.49-3  MySQL database server binaries and system database setup
ii  mysql-server-core-5.1   5.1.49-3  MySQL database server binaries

MySQL の設定ファイルは /etc/mysql 以下にある。


# ls -la /etc/mysql
合計 24
drwxr-xr-x  3 root root 4096 2012-01-21 08:40 .
drwxr-xr-x 84 root root 4096 2012-01-21 08:40 ..
drwxr-xr-x  2 root root 4096 2012-01-21 08:40 conf.d
-rwxr-xr-x  1 root root 1198 2010-11-30 18:35 debian-start
-rw-------  1 root root  333 2012-01-21 08:40 debian.cnf
-rw-r--r--  1 root root 3505 2010-11-30 18:35 my.cnf

/etc/mysql/my.cnf を編集する。

編集した部分だけ以下に抜粋。デフォルト文字コードの指定とMySQLサーバへの接続を許可するIPアドレスの設定。


[client]
default-character-set=utf8
 
[mysqld]
default-character-set=utf8
character-set-server=utf8
#bind-address		= 127.0.0.1
 
[mysqldump]
default-character-set=utf8

bind-address の項目をコメントアウトすることでどこからでも接続できるように設定。コメントアウトしないとローカルからのみアクセス可能な状態に。

そして大きいのが、「これまで非推奨とアナウンスされていた構文、変数、オプションなどを一気に削除した」というのが挙げられます。 私も実は MySQL 5.5.3-m3 が起動できずにしばらく悩んだのですが、サーバの "default-character-set" オプションが廃止になっているんですよ。 character-set-server を使用します。 さらに mysql クライアントのオプションとしては character-set-client ではなく default-character-set のままなので注意が必要です。
MySQL 5.5.3-m3, 5.5.4-m3 同時リリース - sakaikの日々雑感~(T)編

デフォルト文字コードの指定は、将来を見越してサーバ側は default-character-set と character-set-server を両方指定してみた (すでに character-set-server だけでOKな気がするけど)。

--default-character-set=charset_name

charset_nameをデフォルトキャラクタセットとして使用します。項4.10.1. 「データおよびソート用キャラクタ セット」を参照してください。文字列が特定されていない場合、mysqldumpはutf8を使用します。

MySQL :: MySQL 5.1 リファレンスマニュアル :: 7.12 mysqldump — データベースバックアッププログラム

それでも一応、 [mysqldump] セクションでも default-character-set を設定しておく。

(ドキュメントが古かったり新しかったりすることがあるので(翻訳版だとタイムラグがあるし、本家だってどうだか…)防御的に設定する感じ)

以下に /etc/mysql/my.cnf 全体を残しておく。


#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
 
# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
default-character-set=utf8
port		= 3306
socket		= /var/run/mysqld/mysqld.sock
 
# Here is entries for some specific programs
# The following values assume you have at least 32M ram
 
# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket		= /var/run/mysqld/mysqld.sock
nice		= 0
 
[mysqld]
#
# * Basic Settings
#
default-character-set=utf8
character-set-server=utf8
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp
language	= /usr/share/mysql/english
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address		= 127.0.0.1
#
# * Fine Tuning
#
key_buffer		= 16M
max_allowed_packet	= 16M
thread_stack		= 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit	= 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf.
#
# Here you can see queries with especially long duration
#log_slow_queries	= /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id		= 1
#log_bin			= /var/log/mysql/mysql-bin.log
expire_logs_days	= 10
max_binlog_size         = 100M
#binlog_do_db		= include_database_name
#binlog_ignore_db	= include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem
 
 
 
[mysqldump]
quick
quote-names
max_allowed_packet	= 16M
default-character-set=utf8
 
[mysql]
#no-auto-rehash	# faster start of mysql but no tab completition
 
[isamchk]
key_buffer		= 16M
 
#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

設定を反映させるために mysql サーバを再起動。


# /etc/init.d/mysql 
Usage: /etc/init.d/mysql start|stop|restart|reload|force-reload|status
 
# /etc/init.d/mysql restart
Stopping MySQL database server: mysqld.
Starting MySQL database server: mysqld.
Checking for corrupt, not cleanly closed and upgrade needing tables..

MySQL の root ユーザーで MySQL monitor へログイン。


# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.1.49-3 (Debian)
 
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

文字コードの設定を確認。


mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

データベースの作成。


mysql> create database foobardb;
Query OK, 1 row affected (0.00 sec)
 
mysql> show create database foobardb;
+----------+-------------------------------------------------------------------+
| Database | Create Database                                                   |
+----------+-------------------------------------------------------------------+
| foobardb | CREATE DATABASE `foobardb` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)

データベースを使用可能なユーザーの作成(パスワード無し)。


mysql> grant all privileges on foobardb.* to hogeuser;
Query OK, 0 rows affected (0.00 sec)
 
mysql> show grants for 'hogeuser';
+--------------------------------------------------------+
| Grants for hogeuser@%                                  |
+--------------------------------------------------------+
| GRANT USAGE ON *.* TO 'hogeuser'@'%'                   |
| GRANT ALL PRIVILEGES ON `foobardb`.* TO 'hogeuser'@'%' |
+--------------------------------------------------------+
2 rows in set (0.00 sec)

新しく作ったユーザー hogeuser を使ってパスワード無しで MySQL monitor へログインして、パスワードを設定する。

「Enter password:」とパスワードを尋ねてくるけど、何も入力せずにエンターキーを押せばログインできる。


# mysql -u hogeuser -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.1.49-3 (Debian)
 
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

set password でパスワードを設定する。


mysql> set password = password('hogepassword');
Query OK, 0 rows affected (0.00 sec)

MySQL monitor からログアウトして、設定したパスワードで再ログインしてデータベースが使用できるか確認。


mysql> exit
Bye
# mysql -u hogeuser -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 38
Server version: 5.1.49-3 (Debian)
 
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> use foobardb;
Database changed

Ref.
- MySQL :: 世界でもっとも普及している、オープン ソース データベース
- MySQL :: MySQL 5.1 リファレンスマニュアル
- MySQL :: MySQL 5.1 リファレンスマニュアル :: 3.3.2 オプションファイルの使用
- [ヅ] Debian GNU/Linux Sarge に MySQL 4.1 をインストールする

tags: mysql debian

Posted by NI-Lab. (@nilab)