UbuntuにMariaDBをインストールしてみる

Ubuntu 12.04
MariaDB 5.5.31

以前、OS X上にテスト的にMariaDBの環境構築をしました。

【そのときのログ】
http://d.hatena.ne.jp/unk_pizza/20130613/p1

この度、Ubuntu上にMariaDBの環境を構築する必要が生じたので、OS Xの作業ログを頼りにして、UbuntuMariaDB 5.5をインストールしてみました。CMakeのインストールまではサーバー管理者の人がやってくれていたので、今回の私の作業は、CMakeでのmakefileの作成からです。

<今回の環境>
MariaDB実行UNIXユーザー:maria
MariaDBインストール先ディレクトリ:/usr/local/mariadb
データ格納ディレクトリ:/var/db/mariadb/data

インストールは、sudo権限を持つ私の個人アカウントで行います。
ある程度環境が整って、sudoが必要なくなってきたら、mariaユーザーでの作業に切り替えます。


CMake Round 1

Fight!

$ sudo cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb-5.5.31 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

... (省略)

CMake Error at cmake/readline.cmake:85 (MESSAGE):
  Curses library not found.  Please install appropriate package,

      remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
  cmake/readline.cmake:210 (FIND_CURSES)
  CMakeLists.txt:262 (MYSQL_CHECK_READLINE)

どうやら、Debian/Ubuntuでは依存パッケージのインストールが必要っぽいです。

$ sudo apt-get install libncurses5-dev

CMake Round 2

Fight!

$ rm CMakeCache.txt
$ sudo cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb-5.5.31 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

... (省略)

CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found.   Please set CMAKE_CXX_COMPILER to a valid compiler path or name.

どうやら、g++がインストールされていなかったようです。
前に少しMariaDBのソースを見たときには、Cのソースだけだったように見えましたが、g++が必要というのは、MariaDB本体のソースとは無関係に、cmakeがg++を必要としているということなのでしょうか。

$ sudo apt-get install g++

CMake Round 3

Fight!

$ rm CMakeCache.txt
$ sudo cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb-5.5.31 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

... (省略)

Warning: Bison executable not found in PATH

どうやら、Bisonという実行ファイルが必要っぽいですね。

$ sudo apt-get install bison

CMake Round 4

Fight!

$ rm CMakeCache.txt
$ sudo cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb-5.5.31 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

... (省略)

-- Configuring done
-- Generating done
-- Build files have been written to: /home/pizza/foo/bar/mariadb-5.5.31

You win!

ようやくmakefileができました・・・。
やはり、作業ログがあったとしても、環境が違うとすんなりとは行かないものですね。


make

続けてコンパイルします。

$ sudo make

... (省略)

Linking CXX static library libsql.a
[ 99%] Built target sql
Scanning dependencies of target mysqld
[ 99%] Building CXX object sql/CMakeFiles/mysqld.dir/main.cc.o
Linking CXX executable mysqld
[ 99%] Built target mysqld
Scanning dependencies of target udf_example
[ 99%] Building C object sql/CMakeFiles/udf_example.dir/udf_example.c.o
Linking C shared module udf_example.so
[ 99%] Built target udf_example
Scanning dependencies of target my_safe_process
[100%] Building CXX object mysql-test/lib/My/SafeProcess/CMakeFiles/my_safe_process.dir/safe_process.cc.o
Linking CXX executable my_safe_process
[100%] Built target my_safe_process

サーバーがしょぼいのもあって、さすがにコンパイルには時間がかかりました。
(20分くらい)
いくつか警告が発生しましたが、無事コンパイルできました。

警告の内容はすべて、

警告: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

でした。これは、type-punning(型もじり)というやつで、int変数に対してshort*のポインタで参照するような場合に警告されるそうです。これをやられると、コンパイラが演算の依存関係を追えなくなり、最適化の結果、本来のセマンティクスと異なるコードが吐かれてしまう可能性があるそうな。

参考:http://up-cat.net/gcc%2Boption.html

「この変数は厳密な別名規約に則っていないので最適化しないでね」とすることもできるみたいですが、それにはソースの多くの箇所を修正しなければならないので、今回は(たぶん今後も)このままインストールしてしまうことにしました。


インストール

インストールとデータディレクトリの構成などを行います。

$ sudo make install
... (省略)
$ sudo ln -s /usr/local/mariadb-5.5.31 /usr/local/mariadb
$ sudo chown -R maria /usr/local/mariadb-5.5.31
$ sudo mkdir -p /var/db/mariadb/data
$ sudo chown -R maria /var/db/mariadb
$ sudo /usr/local/mariadb/scripts/mysql_install_db --basedir=/usr/local/mariadb --datadir=/var/db/mariadb/data --user=maria
Installing MariaDB/MySQL system tables in '/var/db/mariadb/data' ...
990231 00:00:00 [Warning] Ignoring user change to 'maria' because the user was set to 'mysql' earlier on the command line


Installation of system tables failed!  Examine the logs in
/var/db/mariadb/data for more information.

The problem could be conflicting information in an external
my.cnf files. You can ignore these by doing:

    shell> /scripts/mysql_install_db --defaults-file=~/.my.cnf

You can also try to start the mysqld daemon with:

    shell> /usr/local/mariadb/bin/mysqld --skip-grant --general-log &

and use the command line tool /usr/local/mariadb/bin/mysql
to connect to the mysql database and look at the grant tables:

    shell> /usr/local/mariadb/bin/mysql -u root mysql
    mysql> show tables

Try 'mysqld --help' if you have problems with paths.  Using
--general-log gives you a log in /var/db/mariadb/data that may be helpful.

The latest information about mysql_install_db is available at
http://kb.askmonty.org/v/installing-system-tables-mysql_install_db.
MariaDB is hosted on launchpad; You can find the latest source and
email lists at http://launchpad.net/maria

Please check all of the above before mailing us!  And remember, if
you do mail us, you should use the /usr/local/mariadb/scripts/mysqlbug script!

む・・・失敗してしまいました。
どうも、このUbuntuには、以前MySQLがインストールされていたようで、そちらの設定が先に読み込まれてしまっているようですね。

<参考>
http://anothermysqldba.blogspot.jp/2013/05/warning-because-user-was-set-to-mysql.html
http://www.goofoo.jp/2011/04/999

なるほど、調べてみると、/etc/mysql/my.cnfが存在していました。
こいつのせいで、
[Warning] Ignoring user change to 'maria' because the user was set to 'mysql' earlier on the command line
が出ていたようです。


設定ファイルを適用する

DBインスタンスの初期化に失敗してしまったので、先に設定ファイルを有効にすることにしました。
今回はmediumの設定ファイルテンプレートをグローバルに適用することとして、/etc/my.cnfに設定ファイルを配置することにしました。

$ sudo cp /usr/local/mariadb/support-files/my-medium.cnf /etc/my.cnf
$ sudo chown maria /etc/my.cnf
$ sudo su - maria
$ vim /etc/my.cnf
[mysqld]
user            = maria    # 実行ユーザーを指定
port            = 3306
socket          = /tmp/mysql.sock

設定ファイルを明示的に指定して初期化コマンドを実行します。

$ /usr/local/mariadb/scripts/mysql_install_db --basedir=/usr/local/mariadb --datadir=/var/db/mariadb/data --defaults-file=/etc/my.cnf --user=maria

Installing MariaDB/MySQL system tables in '/var/db/mariadb/data' ...
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 MariaDB root USER !
To do so, start the server, then issue the following commands:

'/usr/local/mariadb/bin/mysqladmin' -u root password 'new-password'
'/usr/local/mariadb/bin/mysqladmin' -u root -h XXXX password 'new-password'

Alternatively you can run:
'/usr/local/mariadb/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 MariaDB Knowledgebase at http://kb.askmonty.org or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '/usr/local/mariadb' ; /usr/local/mariadb/bin/mysqld_safe --datadir='/var/db/mariadb/data'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/local/mariadb/mysql-test' ; perl mysql-test-run.pl

Please report any problems with the '/usr/local/mariadb/scripts/mysqlbug' script!

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Support MariaDB development by buying support/new features from
Monty Program Ab. You can contact us about this at sales@montyprogram.com.
Alternatively consider joining our community based development effort:
http://kb.askmonty.org/en/contributing-to-the-mariadb-project/


よし。
エラーは出ていないので、初期化には成功したっぽいです。


DBを起動する

$ cd /usr/local/mariadb/bin
$ /usr/local/mariadb/bin/mysqld_safe --datadir='/var/db/mariadb/data' &
990231 00:00:00 mysqld_safe Can't log to error log and syslog at the same time.  Remove all --log-error configuration options for --syslog to take effect.
990231 00:00:00 mysqld_safe Logging to '/var/log/mysql/error.log'.
touch: `/var/log/mysql/error.log' に touch できません: 許可がありません
chmod: `/var/log/mysql/error.log' にアクセスできません: 許可がありません
990231 00:00:00 mysqld_safe Starting mysqld daemon with databases from /var/db/mariadb/data
/usr/local/mariadb/bin/mysqld_safe: 132: /usr/local/mariadb/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
/usr/local/mariadb/bin/mysqld_safe: 1: eval: cannot create /var/log/mysql/error.log: Permission denied
990231 00:00:00 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
/usr/local/mariadb/bin/mysqld_safe: 132: /usr/local/mariadb/bin/mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied

あらあら・・・。
/var/log/mysqlにログが書けなくて困っているようですね。しかしながら、そもそもなぜそんなところにログを出力しようとしているのでしょうか。設定ファイルには、/var/log/mysqlにログ出力をするような指定はなかったはずですが。

ああ、そうか!
ここでも/etc/mysql/my.cnfが読み込まれているんですね。
どうせMariaDBMySQLを同時に稼働させるようなことも想定していないし、もう邪魔くさいので、古い設定ファイルを削除してしまいます。

$ exit
$ sudo mv /etc/mysql/my.cnf /etc/mysql/backup_my.cnf
$ sudo su - maria
$ cd /usr/local/mariadb/bin
$ /usr/local/mariadb/bin/mysqld_safe --datadir='/var/db/mariadb/data' &
990231 00:00:00 mysqld_safe Logging to '/var/db/mariadb/data/XXXX.err'.
990231 00:00:00 mysqld_safe Starting mysqld daemon with databases from /var/db/mariadb/data

$ /usr/local/mariadb/bin/mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.31-MariaDB-log Source distribution

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

よしよし。
起動できました。


rootにパスワードをつけていったん終了

$ mysqladmin -u root password ********
$ mysqladmin shutdown -u root -p
$ ps -ef | grep mysql
maria     xxxx  xxxx  0 00:00 pts/0    00:00:00 grep mysql

ふう、疲れた・・・。


だいぶ苦労しましたが、これで、OS XUbuntuへのMariaDB環境構築実績を得られました。
そのうちRedHat系のLinux、おそらくCentOSあたりにMariaDBをインストールすることになりそうなので、そのときはまた、この作業ログを頼りに頑張ってみようかと思います。


それでは。
楽しい時間でした!
またの機会を!