OS X編譯安裝Mariadb

山河永寂發表於2015-07-22

MariaDB資料庫管理系統是MySQL的一個分支,主要由開源社群在維護,採用GPL授權許可 MariaDB的目的是完全相容MySQL,包括API和命令列,使之能輕鬆成為MySQL的代替品。

很多開發環境需要我們安裝資料庫,隨著mysql的日漸衰弱,更多的開發環境使用mariadb作為資料庫,而mariadb資料庫目前沒有OS X的release版本,所以我們需要手動編譯安裝

修改程式碼

由於mariadb本身使用了tokudb的上游程式碼,而tokudb目前尚未有支援OS X的計劃,所以常規安裝方式是不成功的,我們需要對程式碼本身進行修改。
由於是tokudb引擎導致的問題,所以只需要修改兩個檔案
storage/tokudb/ft-index/cmake_modules/TokuSetupCompiler.cmake

-set(CMAKE_C_FLAGS "-Wall -Werror ${CMAKE_C_FLAGS}")
-set(CMAKE_CXX_FLAGS "-Wall -Werror ${CMAKE_CXX_FLAGS}")
+set(CMAKE_C_FLAGS "-Wall ${CMAKE_C_FLAGS}")
+set(CMAKE_CXX_FLAGS "-Wall ${CMAKE_CXX_FLAGS}")

storage/tokudb/ha_tokudb.cc

  • extern const char * const tokudb_hton_name;

  • extern const char *tokudb_hton_name;

筆者猜測最根本的原因是tokudb沒有對OS X編譯工具鏈做編譯測試,OS X的編譯工具是LLVM和BSD工具,而並非是gcc和GNU工具,所以在一些細節上有一些差別。如果沒有改程式碼,會導致以下資訊報錯

[ 80%] Building CXX object storage/tokudb/ft-index/locktree/CMakeFiles/locktree_static.dir/locktree.cc.o
In file included from /Users/tangjiacheng/Development/server/storage/tokudb/ft-index/locktree/locktree.cc:99:
In file included from /Users/tangjiacheng/Development/server/storage/tokudb/ft-index/locktree/locktree.h:94:
/Users/tangjiacheng/Development/server/storage/tokudb/ft-index/buildheader/db.h:323:1: error: 
empty struct has size 0 in C, size 1 in C++ [-Werror,-Wextern-c-compat]
struct __toku_db_lsn {
^
1 error generated.
make[2]: *** [storage/tokudb/ft-index/locktree/CMakeFiles/locktree_static.dir/locktree.cc.o] Error 1
make[1]: *** [storage/tokudb/ft-index/locktree/CMakeFiles/locktree_static.dir/all] Error 2
make: *** [all] Error 2

In file included from /Users/tangjiacheng/Development/server/storage/tokudb/ha_tokudb.cc:8293:
/Users/tangjiacheng/Development/server/storage/tokudb/hatoku_hton.cc:262:13: error: 
redefinition of `tokudb_hton_name` with a different type: `const char *`
vs `const char *const`
const char *tokudb_hton_name = "TokuDB";
^
/Users/tangjiacheng/Development/server/storage/tokudb/ha_tokudb.cc:382:31: note: 
previous definition is here
extern const char * const tokudb_hton_name;

注:至於一些朋友說homebrew install mariadb可以安裝mariadb的說法,其實是因為homebrew本身修改過mariadb的程式碼,增加了一個cmake選項禁用了tokudb,而tokudb本身程式碼是沒有問題的,因噎廢食的做法筆者不敢苟同,所以還是原生編譯安裝更加實在。Homebrew.mariadb

Mariadb增加Jemalloc記憶體管理

Jemalloc在OS X下有一個很奇怪的問題,jemalloc安裝在/usr/local目錄的開發包無法被Mariadb識別,幾經嘗試,暫時發現了一個解決方法。
Mariadb本身使用cmake作為跨平臺編譯解決方案,但是最終在OS X上還是使用LLVM來編譯,所以我們直接編譯好jemalloc以後直接使用-L和-l引數來手動指定庫檔案位置。

Jemalloc編譯

還是三板斧
./configure --prefix=/path/to & make & make install

/path/to即為安裝目錄,隨便指定即可。

然後我們來到安裝目錄的lib目錄下,所有的動態靜態連結庫都在這裡,我們直接刪除到只剩下jemalloc_pic.a這個靜態庫檔案,因為gcc預設發現了動態庫檔案就會使用動態庫檔案來連線,而我們需要靜態連線。
然後這次我選擇了mariadb-10.1.8這個10.1系列第一個stable(GA)版本。

cd mariadb-10.1.8
cmake . -DBUILD_CONFIG=mysql_release -DCMAKE_EXE_LINKER_FLAGS="-L/path/to -ljemalloc_pic" -DWITH_SAFEMALLOC=OFF
make

注意:/path/to是要到libjemalloc_pic.a目錄的
然後。。。就發現,神奇的報錯了,報錯資訊如下

[ 55%] Building CXX object storage/mroonga/vendor/groonga/lib/CMakeFiles/libgroonga.dir/dat.cpp.o
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:191:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:222:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:423:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:452:5: error: cannot use `try` with exceptions disabled
    try {
    ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:465:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:552:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:576:5: error: cannot use `try` with exceptions disabled
    try {
    ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:592:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:617:5: error: cannot use `try` with exceptions disabled
    try {
    ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:616:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:652:5: error: cannot use `try` with exceptions disabled
    try {
    ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:651:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:703:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:811:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:864:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:921:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:969:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:1007:3: error: cannot use `try` with exceptions disabled
  try {
  ^
/Users/tangjiacheng/Downloads/mariadb-10.1.8/storage/mroonga/vendor/groonga/lib/dat.cpp:1101:3: error: cannot use `try` with exceptions disabled
  try {
  ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

查了一下,原來是Mroonga引擎搞的鬼,幸好homebrew已經有了解決方案,我們直接修改
storage/mroonga/vendor/groonga/CMakeLists.txt

@@ -192,6 +192,10 @@ if(CMAKE_COMPILER_IS_GNUCXX)
   check_build_flag("-Wno-clobbered")
 endif()
 
+if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_ID STREQUAL "Clang")
+  MY_CHECK_AND_SET_COMPILER_FLAG("-fexceptions")
+endif()
+
 if(NOT DEFINED CMAKE_C_COMPILE_OPTIONS_PIC)
   # For old CMake
   if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANGCXX)

然後再編譯就成功了

相關文章