移植mysql到嵌入式ARM平臺

                                                                    移植MySQL到嵌入式ARM平臺 
 
MySQL沒有專門針對ARM的版本,移植到ARM沒有官方文檔可參考,所以,暫時參考這樣一篇文檔: http://blog.chinaunix.net/space.php?uid=9701860&do=blog&id=285428,由於MySQL5.5以後,編譯是用的cmake再也不使用./configure,所以,只好倒回支持./configure的版原本用,這裏使用了文檔上的5.1.51版本。進行以下步驟完成移植:php

1) 下載mysql5.1.51: 
    http://www.mirrorservice.org/sites/ftp.mysql.com/Downloads/MySQL-5.1/mysql-5.1.51.tar.gz node

2) 安裝編譯器:用的是4.3.2的交叉編譯器。gcc之類的都是ubuntu10.10自帶的。mysql

3) 編譯PC版本的mysql備用 
    a) 解壓mysql-5.1.51到/opt/mysql-5.1.51: tar zxvf mysql-5.1.51.tar.gz linux

    b) cd mysql-5.1.51 
    c) ./configure -prefix=/usr/local/mysql sql

    d) make 注意,這裏無需運行make install,覺得主要是爲了用pc版本里的gen_lex_hash庫。(注意必定要先make後,再去修改文件夾名稱) shell

    e) 將文件夾mysql-5.1.51更名爲mysql-5.1.51-pc備用。(將gen_lex_hash單獨備份保存一下) 
    f) 文檔上說這裏會出錯,但我在編譯的過程當中沒有碰到,惟一的問題是編譯了arm版本的,從新經過改文件夾的名字回頭編譯pc版本的時候會報錯。 ubuntu


4) 編譯arm版本的ncurses 
    a) 下載ncurses-5.9.tar.gz:ftp://ftp.gnu.org/gnu/ncurses/ncurses-5.9.tar.gz  服務器

    b) 解壓到/opt/中:tar zxvf ncurses-5.9.tar.gz 測試

    c) cd ncurses-5.6 
    d) ./configure –host=arm-linux -prefix=/usr/local/ncurse –enable-static ui

    e) make 
    f) make install之因此安裝這個,是由於對mysql的交叉編譯過程須要該庫的支持

      (此步在用sudo make install時出錯,緣由是環境變量和原來不一樣了,解決辦法:sudo -i;make install) 

 

5) 編譯arm版本的mysql 
    a) tar zxvf mysql-5.1.51.tar.gz 

    b) cd mysql-5.1.51 
    c) 修改配置文件:打開configure,可使用gedit configure 分別在第26453行、 48175行、 48282行、 48485行附近有相似代碼: 
        if test "$cross_compiling" = yes; then 
        { { $as_echo "$as_me:$LINENO: error: in \`$ac_pwd':" >&5 $as_echo "$as_me: error: in \`$ac_pwd':" >&2;} 
        { { $as_echo "$as_me:$LINENO: error: cannot run test program while cross 

        compiling See \`config.log' for more details." >&5 

        $as_echo "$as_me: error: cannot run test program while cross compiling See \`config.log' for more details." >&2;} 
        { (exit 1); exit 1; }; }; } 

        Else 

        將這些代碼改成: 
        if test "$cross_compiling" = yes;  then 

        echo 「skip …..!」 

        #{ { $as_echo "$as_me:$LINENO: error: in \`$ac_pwd':" >&5 #$as_echo "$as_me: error: in \`$ac_pwd':" >&2;} 
        #{ { $as_echo "$as_me:$LINENO: error: cannot run test program while cross compiling See \`config.log' for more details." >&5 
        #$as_echo "$as_me: error: cannot run test program while cross compiling See \`config.log' for more details." >&2;} 
        #{ (exit 1); exit 1; }; }; } 

        Else 

        必定注意,這樣的代碼有4部分,要所有改掉。 
    d) 配置,直接套用了人家的配置方式:

      ./configure --host=arm-linux --enable-static --with-named-curses-libs=/usr/local/ncurse/lib/libncurses.a --prefix=/usr/local/mysql --without-debug --without-docs --without-man --without-bench --with-charset=gb2312 --with-extra-charsets=ascii,latin1,utf8

    e) 修改opt/mysql-5.1.51/sql/sql_parse.cc:在5646行以前添加#define STACK_DIRECTION 1 
        若是不修改該語句,則會出現以下錯誤:sql_parse.cc:5646:21: operator '<' has no left operand,緣由是宏變量STACK_DIRECTION沒有定義初值,arm中定義STACK_DIRECTION爲1。 
        注意:這裏的「#define STACK_DIRECTION 1」一句,不能隨便加在sql_parse.cc的開頭處,而應該根據出錯信息的提示添加在相應的行上,我所遇到的行號和別人文檔上所遇到的行號並不相同。 
    f) 複製PC版本的gen_lex_hash文件到當前文件夾:

       cp  /opt/mysql-5.1.51-pc/sql/gen_lex_hash sql/ 

       touch –m sql/gen_lex_hash  
       cp  /opt/mysql-5.1.51-pc/sql/ lex_hash.h sql/ 

       touch –m sql/ lex_hash.h 
       不然會出現錯誤: 
       make[2]: Leaving directory `/opt/mysql-5.5.3-m3/sql' ./gen_lex_hash > lex_hash.h-t 
       /bin/sh: ./gen_lex_hash: cannot execute binary file 由於arm版的沒法在pc上運行。

       注意:別人的文檔上說只要拷貝gen_lex_hash便可,但我試了好屢次,都仍然會出現上面的報錯信息,把lex_hash.h也拷貝過來後,就再也不報錯了。另外,touch必定要作,緣由就是讓編譯器不要再編譯覆蓋拷貝過來的文件了。

   g) Make 

   h) Make install 


6) 移植相應文件到ARM平臺 
   a) 拷貝pc的/usr/local/mysql到開發板的相同目錄 
       我使用了nfs調試,因此須要使用以下指令: 
       cp -r /usr/local/mysql /opt/EmbedSky/root_nfs/usr/local/mysql  
   b) 把編譯出的arm的mysql庫打包備份一下,考到主機的目錄裏: 
       tar –zcvf mysql-arm-5.1.51.tar.gz mysql 
   c) 到源碼中拷貝配置文件模版  Copies files from one location to another. 配置文件模版) 

       cp /opt/mysql-5.1.51/support-files/my-medium.cnf /opt/EmbedSky/root_nfs/etc/my.cnf (這裏的目錄指的是nfs調試的路徑設置),這裏的my.cnf存放的路徑是按照手冊上的建議,前面編譯pc版本的MySQL         中所述的路徑並不是全局配置。該文檔的註釋中說:「# You can copy this file to /etc/my.cnf to set global options, mysql-data-dir/my.cnf to set server-specific options (in this installation this directory is   /usr/local/mysql/var) or ~/.my.cnf to set user-specific options.」該配置文件的修改詳見mysql5.1的英文手冊的4.2.3.3. Using Option Files節中的敘述。 

       數據目錄是在:/var/lib/mysql (默認)

       安裝目錄是在:/usr/local/mysql (默認)
       試圖反註釋了關於InnoDB的一些配置,其它沒有動。可是修改了這些設置後,報錯,因而又改了回來。 
    d) 運行mysql_install_db(參見手冊的2.13. Post-Installation Setup and Testing) 
       cd /usr/local/mysql/bin(開發板路徑),運行mysql_install_db -u root  結果出現了以下錯誤:Neither host 'EmbedSky' nor 'localhost' could be looked up with /usr/local/mysql/bin/resolveip Please configure the 'hostname' command to return a correct hostname. If you want to solve this at a later stage, restart this script with the --force option 這主要的緣由是開發板環境中的hostname是EmbedSky,而不是一般的Federa14等,因此mysql自動認爲可能在該操做系統中的運行會不兼容,有兩種辦法解決: 
         第一種,運行hostname fedora14,就是欺騙一下hostname;

         第二種,運行mysql_install_db -u root –force 

         我使用了第二種方式: bin/mysql_install_db --user=root --force --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var/lib/mysql (我修改了datadir的路徑) 
         中間出現過一次錯誤:150713 21:06:39 [ERROR] /usr/local/mysql/libexec/mysqld: unknown variable 'innodb_data_home_dir=/usr/local/mysql/var/',查明緣由是my.cnf中反註釋了和InnoDB相關的配置。 
    e) 手動創建mysqld/mysqld.pid,手工創建: (這一步不須要,制定到/tmp/mysqld.pid就行)
        mkdir /usr/local/mysql/var/run/mysqld 
        touch /usr/local/mysql/var/run/mysqld/mysqld.pid 
        這一步不知道是否是必須的。但我這樣作了。(不須要)

    f) 到源碼中拷貝啓動文件 
        cp /opt/mysql-5.1.51/support-files/mysql.server /opt/EmbedSky/root_nfs/etc/init.d/mysqld 

       修改該mysqld 

       詳見手冊中4.3.1. mysqld — The MySQL Server的敘述 
       加上了basedir和datadir,

       還有pid-file=/tmp/mysqld.pid 

       還有service-pid-file=/tmp/mysqld.pid 

       修改完後,要給新的mysqld附以足夠的權限: Chmod +x mysqld 
    g) 在開發板開啓MySQL服務 
       開發板不支持service指令,因此service mysql start無效。

       採用的方法是運行./etc/init.d/mysqld start 
       但最初運行該指令後出現下面的錯誤: 
       Starting MySQL... ERROR! Manager of pid-file quit without updating file. 
       困擾我很久,到開發板目錄/var/lib/mysql下查閱錯誤日誌文件[hostname].err,在個人系統中該錯誤日誌文件爲EmbedSky.err,從中看到下面的記錄: 
      150713 21:04:49 [ERROR] Fatal error: Can't change to run as user 'mysql' ;  Please check that the user exists! 
      可能的緣由是:在arm的linux上沒法執行groupadd mysql,所以須要採用以下方法解決該問題: cd /usr/local/mysql/var/lib/mysql 
      ls –la能夠看到裏面的屬性中沒有mysql,因而使用下面的命令: adduser mysql 
      chown mysql:mysql -R /var/lib/mysql 
      而後開啓mysql服務,仍是出現了ERROR! Manager of pid-file quit without updating file.又查看EmbedSky.err日誌,其中多了一條: 
      150714  2:48:04 [ERROR] Can't start server: Bind on TCP/IP port: Address already in use 
      150714  2:48:04 [ERROR] Do you already have another mysqld server running on port: 3306 ? 
      很顯然是由於已經有mysql的進程嘗試打開3306端口,所以就被佔用了,須要殺進程,索性重啓開發板,而後運行./etc/init.d/mysqld start,能夠完美打開。 

  (我使用的時候,還有另外的一個問題,因爲客戶端和服務器都要訪問/tmp目錄,因此每次開機都要chmod 777 /tmp  ,以便於都能訪問)
    h) 設置軟鏈接使mysql,  mysqldump,  mysqladmin這三個命令能在開發板的shell中直接運行 
       ln -s /usr/local/mysql/bin/mysql /usr/bin 
       ln -s /usr/local/mysql/bin/mysqldump /usr/bin 

       ln -s /usr/local/mysql/bin/mysqladmin /usr/bin

       其餘的還有:連接庫文件

       ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.16 /lib

7) 測試ARM平臺下的MySQL 
    a) mysqladmin -u  root   password   hahaha 最後一項爲個人密碼   (設置密碼) 

    b) mysql -h  127.0.0.1  -u root  -p 或mysql -h  localhost  -u root  -p 這樣即可以進入mysql環境。 

    c) mysql>show databases;

        mysql>create databases at91;

        mysql>use at91;

        mysql>create table node (id int(5) auto_increment not null primary key, node_ID char (40), param_ID_values varchar(900));

相關文章
相關標籤/搜索