首页嵌入式 › 移植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版本。进行如下步骤完成移植:

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

2) 安装编译器:用的是4.3.2的交叉编译器。gcc之类的都是ubuntu10.10自带的。

3) 编译PC版本的mysql备用 
  a) 解压mysql-5.1.51到/opt/mysql-5.1.51: tar zxvf mysql-5.1.51.tar.gz

  1.  b) cd mysql-5.1.51
    c) ./configure -prefix=/usr/local/mysql
  2.  d) make 注意,这里无需运行make install,以为主要是为了用pc版本里的gen_lex_hash库。(注意一定要先make后,再去修改文件夹名称)
  3.   e) 将文件夹mysql-5.1.51改名为mysql-5.1.51-pc备用。(将gen_lex_hash单独备份保存一下)
      f) 文档上说这里会出错,但我在编译的过程中没有碰到,唯一的问题是编译了arm版本的,重新通过改文件夹的名字回头编译pc版本的时候会报错。

4) 编译arm版本的ncurses 
 a) 下载ncurses-5.9.tar.gz:ftp://ftp.gnu.org/gnu/ncurses/ncurses-5.9.tar.gz

  1.  b) 解压到/opt/中:tar zxvf ncurses-5.9.tar.gz
  2.  c) cd ncurses-5.6
    d) ./configure –host=arm-linux -prefix=/usr/local/ncurse –enable-static
  3.  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

  1.  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

  1.  e) 修改opt/mysql-5.1.51/sql/sql_parse.cc:在5646行之前添加#define STACK_DIRECTION 1
    如果不修改该语句,则会出现如下错误: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一定要做,原因就是让编译器不要再编译覆盖拷贝过来的文件了。

  1.  g)Make
  2.  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
这一步不知道是不是必须的。但我这样做了。(不需要)

  1.  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 最后一项为我的密码   (设置密码)

  1.  b) mysql -h  127.0.0.1  -u root  -p 或mysql -h  localhost  -u root  -p 这样便可以进入mysql环境。
  2.   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));

 

发表评论