libmysqld,嵌入式MySQLserver庫


25.1.1. 嵌入式MySQLserver庫概述

使用嵌入式MySQLserver庫,可以在client應用程序中使用具備所有特性的MySQLserver。 主要長處在於。添加了速度。並使得嵌入式應用程序的管理更簡單。

嵌入式server庫是以MySQL的client/server版本號為基礎的,採用C/C++語言編寫。 其結果是嵌入式server也是用C/C++語言編寫的。 在其它語言中,嵌入式server不可用。

API與嵌入式MySQL版本號和client/server版本號等效。

要想更改舊的線程式應用程序以使用嵌入式庫。正常情況下,僅需加入對下述函數的調用就可以。

函數

何時調用

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()”

嵌入式server和獨立server之間的一項區別在於。對於嵌入式server,默認情況下,連接鑒定是禁止的。

對於嵌入式server,要想使用鑒定功能,可在激活“configure”以配置MySQL分發版時使用“--with-embedded-privilege-control”選項。

25.1.2. 使用libmysqld編譯程序

要想獲得libmysqld庫。應使用--with-embedded-server選項配置MySQL

請參見2.8.2節。“典型配置選項

將你的程序與libmysqld鏈接時。還必須包括系統的pthread庫以及MySQLserver使用的一些庫。

運行“mysql_config --libmysqld-libs”。可獲得庫的完整列表。

對於線程程序的編譯和鏈接,必須使用正確的標志,即使你未在代碼中直接調用不論什么線程函數也相同。

要想編譯C程序以包括必要文件,並將MySQLserver庫嵌入到程序的編譯版本號中,可使用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. 使用嵌入式MySQLserver時的限制

嵌入式server存在下述限制:

·        不支持ISAM表。 (這樣做的主要目的是為了使庫更小)。

·        沒有自己定義函數(UDF)。

·        沒有對核心轉儲的堆棧跟蹤。

·        沒有內部RAID支持。

(因為大多數當前操作系統均支持大文件,通常情況下不須要它)。

·        不能將其設置為“主”或“從”(無復制)。

·        在內存較低的系統上。可能無法使用非常大的結果集。

·        不能使用套接字或TCP/IP從外部進程連接到嵌入式server。 可是。你能夠連接到中間應用程序,隨后,該中間應用程序可代表遠程client或外部進程連接到嵌入式server。

通過編輯“mysql_embed.h”包括文件並又一次編譯MySQL,可更改某些限制。

25.1.4. 與嵌入式server一起使用的選項

對於不論什么可以與mysqld服務器port監督程序一起給定的選項,也可以與嵌入式服務器庫一起使用。在數組中,可將服務器選項作為參量指定給用於初始化服務器的mysql_server_init()。也能在諸如my.cnf的選項文件里給定它們。

要想為C程序指定選項文件,請使用“--defaults-file選項作為函數mysql_server_init()的第2個參量的元素之中的一個。

關於mysql_server_init()函數的很多其它信息,請參見25.2.12.1節,“mysql_server_init()”

使用選項文件,可以簡化client/server應用程序和嵌入了MySQL的應用程序之間的切換。

將經常使用選項置於[server]組。它們可被兩種MySQL版本號讀取。client/server選項應被放在[mysqld]部分。將嵌入式MySQLserver庫的選項放在[embedded]部分。將與應用程序相關的選項放在標記為[ApplicationName_SERVER]的部分。請參見4.3.2節。“使用選項文件”

25.1.5. 嵌入式server中尚需完畢的事項(TODO)

·        我們將提供一些選項以省去MySQL的某些部分,從而使庫變得更小。

·        仍有非常多速度優化工作須要完畢。

·        錯誤將被寫入stderr。我們將添加1個選項為它們指定文件名稱。

·        使用嵌入式版本號時。須要更改InnoDB,使之不再冗長。假設你的數據庫不含InnoDB表。要想抑制相關消息。可為組[libmysqd_server]下的選項文件添加--skip-innodb”選項,或在用mysql_server_init()初始化服務器時加入該選項。

25.1.6. 嵌入式server演示樣例

LinuxFreeBSD系統上,無需更改就能使用以下這兩個演示樣例程序。對於其它操作系統,須要進行小的改動,主要是文件路徑。設計這兩個演示樣例的目的在於,為你提供足夠的細節信息。以便理解問題,它們是實際應用程序的必要組成部份。第1個演示樣例十分直觀。第2個演示樣例採用了一些錯誤檢查功能。略為復雜。在第1個演示樣例的后面,給出了用於編譯程序的命令行條目。

在第2個演示樣例的后面,給出了GNUmake文件,該文件可用於編譯。

演示樣例:1

test1_libmysqld.c

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#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 <mysql.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
 
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 < end_row; ++row)
        (void)printf("%s\t", row ? (char*)*row : "NULL");
      (void)fputc('\n', stdout);
    }
    (void)fputc('\n', stdout);
    mysql_free_result(res);
  }
  else
    (void)printf("Affected rows: %lld\n", mysql_affected_rows(db));
 
  return;
 
err:
  die(db, "db_do_query failed: %s [%s]", mysql_error(db), query);
}

GNUmakefile

# This assumes the MySQL software is installed in /usr/local/mysql
inc      := /usr/local/mysql/include/mysql
lib      := /usr/local/mysql/lib
 
# If you have not installed the MySQL software yet, try this instead
#inc      := $(HOME)/mysql-5.1/include
#lib      := $(HOME)/mysql-5.1/libmysqld
 
CC       := gcc
CPPFLAGS := -I$(inc) -D_THREAD_SAFE -D_REENTRANT
CFLAGS   := -g -W -Wall
LDFLAGS  := -static
# You can change -lmysqld to -lmysqlclient to use the
# client/server library
LDLIBS    = -L$(lib) -lmysqld -lz -lm -lcrypt
 
ifneq (,$(shell grep FreeBSD /COPYRIGHT 2>/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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM