MySQL 5.5 ビルド & インストール
仕事で 5.5 系の MySQL を何度かビルドしているのだが、毎度毎度手順やコンパイルオプションを確認するのも手間なので自宅検証機での MySQL バージョンアップを機に手順をまとめておこうと思う。本家のInstalling MySQL from a Standard Source Distribution も参照。
この記事の前提条件は以下の通り。
OS | CentOS 5.6 final Kernel 2.6.18-238.el5xen |
---|---|
MySQL | MySQL 5.5.15 (Source Distribution) |
インストール先 | /usr/local/mysql |
データディレクトリ | /srv/mysql |
実行アカウント | uid:mysql(901), gid:mysql(901) |
エンジン | InnoDB |
文字セット | UTF-8 |
CentOS 5.6 で MySQL 5.5 をビルドするのに必要なライブラリは以下の通り。
gcc-c++
(yum でインストール)ncurses-devel
(yum でインストール)- CMake
勢いで全て sudo で行っているが、きっと root ユーザで行った方が楽かと思う。
ソースのビルドとインストール
MySQL のダウンロードサイトにある [Source Code] の Generic Linux (Architecture Independent), Compressed TAR Archive を選択し、適当なミラーサイトからダウンロードを行います。
torao@clove$ wget "http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.15.tar.gz/from/http://ftp.iij.ad.jp/pub/db/mysql/" --2011-08-07 00:54:44-- http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.15.tar.gz/from/http://ftp.iij.ad.jp/pub/db/mysql/ dev.mysql.com をDNSに問いあわせています... 213.136.52.29 … torao@clove$ tar zxvf mysql-5.5.15.tar.gz mysql-5.5.15/ mysql-5.5.15/INSTALL-WIN-SOURCE mysql-5.5.15/cmake/ … torao@clove$ cd mysql-5.5.15
5.5 系からは環境設定に configure ではなく cmake を使用するよう変更されていますので、まだ cmake を入れていない場合は先にインストールして下さい。使用する cmake のコンパイルオプションは以下の通り。
CMAKE_INSTALL_PREFIX | インストール先ディレクトリ |
MYSQL_DATADIR | デフォルトのデータディレクトリ |
DEFAULT_CHARSET | デフォルトの文字セット |
DEFAULT_COLLATION | デフォルトの照合順序 |
torao@clove$ cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.5.15 -DMYSQL_DATADIR=/srv/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -- The C compiler identification is GNU -- The CXX compiler identification is GNU -- Check for working C compiler: /usr/bin/gcc … torao@clove$ make Scanning dependencies of target INFO_BIN [ 0%] Built target INFO_BIN Scanning dependencies of target INFO_SRC [ 0%] Built target INFO_SRC … torao@clove$ sudo make install [sudo] password for torao: [ 0%] Built target INFO_BIN [ 0%] Built target INFO_SRC … torao@clove$ cd /usr/local torao@clove$ sudo ln -s mysql-5.5.15 mysql
将来のバージョンアップで切り替えを簡単に行うためシンボリックリンクを使用しています。これで /usr/local/mysql に MySQL がインストールされました。
データベースの構成と初期化
まず MySQL サーバの実行ユーザを作成します。ユーザ名 mysql
(UID:901)、グループ名 mysql
(GID:901) とします。
torao@clove$ sudo /usr/sbin/groupadd -r --gid 901 mysql torao@clove$ sudo /usr/sbin/useradd --base-dir /srv/mysql --home-dir /usr/mysql --comment "MySQL" -M -r --gid mysql --uid 901 mysql
次に mysql_install_db コマンドを使用してデータディレクトリ /srv/mysql を初期化します。既に /srv/mysql がデータディレクトリとして使用されている場合は全てのデータが消えるので注意!
torao@clove$ sudo mkdir /srv/mysql torao@clove$ sudo /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/srv/mysql --user=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/mysql/bin/mysqladmin -u root password 'new-password' /usr/local/mysql/bin/mysqladmin -u root -h clove password 'new-password' Alternatively you can run: /usr/local/mysql/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/mysql ; /usr/local/mysql/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/local/mysql/scripts/mysqlbug script! torao@clove$ sudo chown -R mysql:mysql /srv/mysql
ユーザ指定だけでは root グループのファイルとなってしまっていたため chown で所有者を変更しています。
続いて MySQL の設定ファイルを /etc にコピーし設定を追加します。
torao@clove$ sudo cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf torao@clove$ sudo vim /etc/my.cnf … [mysqld] datadir = /srv/mysql user = mysql …
自動起動の設定
MySQL サーバを自動起動するにはインストールディレクトリの support-files/mysql.server を使用できます。/etc/init.d からこのファイルへシンボリックリンクを作成し、起動が可能であることを確認したら chkconfig で自動起動に設定します。
torao@clove$ sudo ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql torao@clove$ sudo /etc/init.d/mysql start Starting MySQL.. [ OK ] torao@clove$ sudo /sbin/chkconfig mysql on
起動に失敗する場合はデータディレクトリ下の servername.err を参照して下さい。該当ファイルが存在しない場合はデータディレクトリに mysql ユーザの書き込み権限があるか確認して下さい。
MySQL クライアント接続確認
サーバが起動したら mysql
コマンドを使用して MySQL にアクセスし MySQL のバージョン、InnoDB の使用、UTF-8 の使用などを確認します。
torao@clove$ /usr/local/mysql/bin/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-log 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> select version();
+------------+
| version() |
+------------+
| 5.5.15-log |
+------------+
1 row in set (0.01 sec)
mysql> select @@innodb_version;
+------------------+
| @@innodb_version |
+------------------+
| 1.1.8 |
+------------------+
1 row in set (0.00 sec)
mysql> show engines;
+--------------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+------------------------------------------------------------+--------------+------+------------+
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+--------------------+---------+------------------------------------------------------------+--------------+------+------------+
6 rows in set (0.01 sec)
mysql> status
--------------
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.5.15, for Linux (x86_64) using EditLine wrapper
Connection id: 2
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.15-log Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 31 min 36 sec
Threads: 1 Questions: 7 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.003
--------------
ユーザの作成
デフォルトの状態ではローカルからのみアクセス可能な root というユーザが存在するだけですので、リモートからアクセス可能なユーザを作成します。ここでは例として全ての権限を持ちどこからでもアクセス可能な mysql
というユーザ (パスワードも mysql
) を作成します。
mysql> grant all privileges on *.* to 'mysql' identified by 'mysql'; Query OK, 0 rows affected (0.00 sec) mysql> grant all privileges on *.* to 'mysql'@'localhost' identified by 'mysql'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
リモートから mysql
ユーザで接続できたら完了です。おっと、ファイアウォールで MySQL サーバのポート 3306 を開くのを忘れないで下さい。
aquamarine:~ torao$ mysql -h clove -u mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.5.15-log 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>