bitscn.com
25.1.1. 嵌入式mysql服务器库概述使用嵌入式mysql服务器库,能够在客户端应用程序中使用具备全部特性的mysql服务器。 主要优点在于,增加了速度,并使得嵌入式应用程序的管理更简单。
嵌入式服务器库是以mysql的客户端/服务器版本为基础的,采用c/c++语言编写。 其结果是嵌入式服务器也是用c/c++语言编写的。 在其他语言中,嵌入式服务器不可用。
api与嵌入式mysql版本和客户端/服务器版本等效。 要想更改旧的线程式应用程序以使用嵌入式库,正常情况下,仅需添加对下述函数的调用即可。
函数
何时调用
mysql_server_init()
应在调用任何其他mysql函数之前调用,最好是在main()函数中调用。
mysql_server_end()
应在程序退出前调用。
mysql_thread_init()
应在你所创建的、用于访问mysql的每个线程中调用。
mysql_thread_end()
应在调用pthread_exit()之前调用。
随后,必须将你的代码与libmysqld.a链接起来,而不是libmysqlclient.a。
在libmysqlclient.a中还包含mysql_server_xxx()函数,使用这类函数,通过将应用程序链接到恰当的库,即可在嵌入式版本和客户端/服务器版本之间切换。 请参见25.2.12.1节,“mysql_server_init()”。
嵌入式服务器和独立服务器之间的一项差别在于,对于嵌入式服务器,默认情况下,连接鉴定是禁止的。 对于嵌入式服务器,要想使用鉴定功能,可在激活“configure”以配置mysql分发版时使用“--with-embedded-privilege-control”选项。
25.1.2. 使用libmysqld编译程序要想获得libmysqld库,应使用“--with-embedded-server”选项配置mysql。 请参见2.8.2节,“典型配置选项”。
将你的程序与libmysqld链接时,还必须包含系统的pthread库以及mysql服务器使用的一些库。 执行“mysql_config --libmysqld-libs”,可获得库的完整列表。
对于线程程序的编译和链接,必须使用正确的标志,即使你未在代码中直接调用任何线程函数也同样。
要想编译c程序以包含必要文件,并将mysql服务器库嵌入到程序的编译版本中,可使用gnu c编译器(gcc)。 编译器需要知道各种文件的位置,并需了解如何编译程序的指令。 在下面的示例中,介绍了如何从命令行编译程序的方法:
gcc mysql_test.c -o mysql_test -lz /
`/usr/local/mysql/bin/mysql_config --include --libmysqld-libs`
在gcc命令后紧跟着未编译c程序文件的名称。 接下来,给定的“-o”选项指明,它后面的文件名是编译器将输出文件的名称,即编译后的程序。 在下一行的代码中,通知编译器获取包含文件和库的位置,以及在其上进行编译的系统的其他设置。 由于“mysql_config”存在的问题,在此添加了“-lz”选项(压缩)。 “mysql_config”部分包含在backticks中,而不是单引号内。
25.1.3. 使用嵌入式mysql服务器时的限制嵌入式服务器存在下述限制:
· 不支持isam表。 (这样做的主要目的是为了使库更小)。
· 没有自定义函数(udf)。
· 没有对核心转储的堆栈跟踪。
· 没有内部raid支持。 (由于大多数当前操作系统均支持大文件,通常情况下不需要它)。
· 不能将其设置为“主”或“从”(无复制)。
· 在内存较低的系统上,可能无法使用很大的结果集。
· 不能使用套接字或tcp/ip从外部进程连接到嵌入式服务器。 但是,你可以连接到中间应用程序,随后,该中间应用程序可代表远程客户端或外部进程连接到嵌入式服务器。
通过编辑“mysql_embed.h”包含文件并重新编译mysql,可更改某些限制。
25.1.4. 与嵌入式服务器一起使用的选项对于任何能够与mysqld服务器端口监督程序一起给定的选项,也可以与嵌入式服务器库一起使用。在数组中,可将服务器选项作为参量指定给用于初始化服务器的mysql_server_init()。也能在诸如my.cnf的选项文件中给定它们。要想为c程序指定选项文件,请使用“--defaults-file”选项作为函数mysql_server_init()的第2个参量的元素之一。关于mysql_server_init()函数的更多信息,请参见25.2.12.1节,“mysql_server_init()”。
使用选项文件,能够简化客户端/服务器应用程序和嵌入了mysql的应用程序之间的切换。将常用选项置于[server]组。它们可被两种mysql版本读取。客户端/服务器选项应被放在[mysqld]部分。将嵌入式mysql服务器库的选项放在[embedded]部分。将与应用程序相关的选项放在标记为[applicationname_server]的部分。请参见4.3.2节,“使用选项文件”。
25.1.5. 嵌入式服务器中尚需完成的事项(todo)· 我们将提供一些选项以省去mysql的某些部分,从而使库变得更小。
· 仍有很多速度优化工作需要完成。
· 错误将被写入stderr。我们将增加1个选项为它们指定文件名。
· 使用嵌入式版本时,需要更改innodb,使之不再冗长。如果你的数据库不含innodb表,要想抑制相关消息,可为组[libmysqd_server]下的选项文件增加“--skip-innodb”选项,或在用mysql_server_init()初始化服务器时添加该选项。
25.1.6. 嵌入式服务器示例在linux或freebsd系统上,无需更改就能使用下面这两个示例程序。对于其他操作系统,需要进行小的修改,主要是文件路径。设计这两个示例的目的在于,为你提供足够的细节信息,以便理解问题,它们是实际应用程序的必要组成部份。第1个示例十分直观。第2个示例采用了一些错误检查功能,略为复杂。在第1个示例的后面,给出了用于编译程序的命令行条目。在第2个示例的后面,给出了gnumake文件,该文件可用于编译。
示例:1
test1_libmysqld.c
#include
#include
#include
#include mysql.h
mysql *mysql;
mysql_res *results;
mysql_row record;
static char *server_options[] = { mysql_test, --defaults-file=my.cnf };
int num_elements = sizeof(server_options)/ sizeof(char *);
static char *server_groups[] = { libmysqld_server, libmysqld_client };
int main(void)
{
mysql_server_init(num_elements, server_options, server_groups);
mysql = mysql_init(null);
mysql_options(mysql, mysql_read_default_group, libmysqld_client);
mysql_options(mysql, mysql_opt_use_embedded_connection, null);
mysql_real_connect(mysql, null,null,null, database1, 0,null,0);
mysql_query(mysql, select column1, column2 from table1);
results = mysql_store_result(mysql);
while((record = mysql_fetch_row(results))) {
printf(%s - %s /n, record[0], record[1]);
}
mysql_free_result(results);
mysql_close(mysql);
mysql_server_end();
return 0;
}
下面给出了编译上述程序的命令行命令:
gcc test1_libmysqld.c -o test1_libmysqld -lz /
`/usr/local/mysql/bin/mysql_config --include --libmysqld-libs`
示例:2
要想检验该示例,创建一个与mysql源目录同级的test2_libmysqld目录。将test2_libmysqld.c源文件和gnumakefile保存到该目录,并在test2_libmysqld目录下运行gnumake。
test2_libmysqld.c
/*
* a simple example client, using the embedded mysql server library
*/
#include
#include
#include
#include
mysql *db_connect(const char *dbname);
void db_disconnect(mysql *db);
void db_do_query(mysql *db, const char *query);
const char *server_groups[] = {
test2_libmysqld_server, embedded, server, null
};
int
main(int argc, char **argv)
{
mysql *one, *two;
/* mysql_server_init() must be called before any other mysql
* functions.
*
* you can use mysql_server_init(0, null, null), and it
* initializes the server using groups = {
* server, embedded, null
* }.
*
* in your $home/.my.cnf file, you probably want to put:
[test2_libmysqld_server]
language = /path/to/source/of/mysql/sql/share/english
* you could, of course, modify argc and argv before passing
* them to this function. or you could create new ones in any
* way you like. but all of the arguments in argv (except for
* argv[0], which is the program name) should be valid options
* for the mysql server.
*
* if you link this client against the normal mysqlclient
* library, this function is just a stub that does nothing.
*/
mysql_server_init(argc, argv, (char **)server_groups);
one = db_connect(test);
two = db_connect(null);
db_do_query(one, show table status);
db_do_query(two, show databases);
mysql_close(two);
mysql_close(one);
/* this must be called after all other mysql functions */
mysql_server_end();
exit(exit_success);
}
static void
die(mysql *db, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
(void)putc('/n', stderr);
if (db)
db_disconnect(db);
exit(exit_failure);
}
mysql *
db_connect(const char *dbname)
{
mysql *db = mysql_init(null);
if (!db)
die(db, mysql_init failed: no memory);
/*
* notice that the client and server use separate group names.
* this is critical, because the server does not accept the
* client's options, and vice versa.
*/
mysql_options(db, mysql_read_default_group, test2_libmysqld_client);
if (!mysql_real_connect(db, null, null, null, dbname, 0, null, 0))
die(db, mysql_real_connect failed: %s, mysql_error(db));
return db;
}
void
db_disconnect(mysql *db)
{
mysql_close(db);
}
void
db_do_query(mysql *db, const char *query)
{
if (mysql_query(db, query) != 0)
goto err;
if (mysql_field_count(db) > 0)
{
mysql_res *res;
mysql_row row, end_row;
int num_fields;
if (!(res = mysql_store_result(db)))
goto err;
num_fields = mysql_num_fields(res);
while ((row = mysql_fetch_row(res)))
{
(void)fputs(>> , stdout);
for (end_row = row + num_fields; row /dev/null))
# freebsd
ldflags += -pthread
else
# assume linux
ldlibs += -lpthread
endif
# this works for simple one-file test programs
sources := $(wildcard *.c)
objects := $(patsubst %c,%o,$(sources))
targets := $(basename $(sources))
all: $(targets)
clean:
rm -f $(targets) $(objects) *.core
bitscn.com