LIBMYSQLD, embedded MySQLServer Library

Source: Internet
Author: User
Tags goto mysql functions mysql version stack trace server port

25.1.1. Embedded MySQLServer Library Overview

With the embedded MySQLServer library, you can use MySQLServer with all the features in the client application. The main strength is. Added the speed. and makes the management of embedded applications easier.

The embedded server library is based on the Client/server version number of MySQL, which is written in the C + + language. The result is that the embedded server is also written in C + + language. In other languages, embedded server is not available.

The API is equivalent to the embedded MySQL version number and the Client/server version number.

To change the old line program application to use the embedded library. Normally, you just need to add a call to the following function.

Function

When to call

Mysql_server_init ()

Should be called before any other MySQL function is called. It is best to call it in the main () function.

Mysql_server_end ()

Should be called before the program exits.

Mysql_thread_init ()

Should be called in every thread that you create to access MySQL.

Mysql_thread_end ()

Should be called before Pthread_exit () is called.

You must then link your code with the libmysqld.a. Rather than LIBMYSQLCLIENT.A.

also included in Libmysqlclient.a is the mysql_server_xxx() function, which uses these functions to link the application to the appropriate library, You can switch between the embedded version number and the client/server version number. Please see section 25.2.12.1. "Mysql_server_init ()".

One difference between embedded server and standalone server is that. For embedded servers, connection authentication is prohibited by default.

For embedded server, to use the authentication feature, you can use the--with-embedded-privilege-control when activating "Configure" To configure the MySQL distribution version. "option.

25.1.2. Compiling a program with LIBMYSQLD

To get the libmysqld Library. You should configure MySQLwith the "--with-embedded-server" option.

Please see section 2.8.2. "Typical configuration options ".

When you link your program with Libmysqld. You must also include the Pthread Library of the system and some libraries used by MySQLServer.

Run "mysql_config--libmysqld-libs". A complete list of libraries can be obtained.

For the compilation and linking of threads, you must use the correct flags, even if you do not invoke them directly in your code, regardless of the thread function.

To compile a C program to include the necessary files and embed the MySQLServer library in the program's compiled version number, you can use the GNU C compiler (gcc). The compiler needs to know the location of the various files and how to compile the instructions for the program. In the following demo sample, you describe how to compile a program from the command line:

GCC Mysql_test.c-o mysql_test-lz \
'/usr/local/mysql/bin/mysql_config--include--libmysqld-libs '

The name of the C program file is not compiled immediately after the GCC command. Next, the given "-o" option is indicated. The file name behind it is the name of the compiler that will output the file, which is the compiled program. In the next line of code, tell the compiler to get the location of the file and library, and other settings for the system on which it was compiled. The "-lz" option (compression) has been added here because of a problem with "mysql_config". The "mysql_config" section is included in the backticks , not in the single-lead.

25.1.3. Limitations when using embedded MySQLServer

Embedded server has the following limitations:

· ISAM tables are not supported . (The main purpose of this is to make the library smaller).

· There is no self-defined function (UDF).

· There is no stack trace for the core dump.

· No internal raid support.

(because most of the current operating system supports large files, it is not normally required).

· It cannot be set to "primary" or "from" (No replication).

· on a system with low memory. A very large result set may not be available.

· You cannot connect to an embedded server from an external process using sockets or TCP/IP. But. You can connect to an intermediate application, which can then connect to the embedded server on behalf of a remote client or external process.

You can change some of the limitations by editing "mysql_embed.h" including files and compiling MySQL again.

25.1.4. Options for use with embedded server

It can also be used with an embedded server library, regardless of the options that can be given with the mysqld Server Port Supervision program. In the array, you can assign the server option as a reference to the Mysql_server_init ()that is used to initialize the server. They can also be given in an option file such as my.cnf .

To specify an option file for a C program, use the "--defaults-file" option as part of the function mysql_server_init () 2 One of the elements of a reference.

For a lot of other information about the mysql_server_init () function, see section 25.2, 12.1, "Mysql_server_init ()".

Using the options file, you can simplify the switch between the Client/server application and the MySQL -embedded application.

The frequently used option is placed in the [Server] group. They can be read by two MySQL version numbers. The Client/server option should be placed in the [mysqld] section. Place the options for the embedded MySQLServer Library in the [embedded] section. Place the application-related options in the section labeled [Applicationname_server] . please see section 4.3.2. "Use Options File".

25.1.5. What's still needed in embedded server (TODO)

· We'll provide some options to save some parts of MySQL , making the library smaller.

· There is still a lot of speed to optimize the work to complete.

· The error will be written to stderr. We will add 1 options to specify a file name for them.

· When using the embedded version number. The InnoDBneeds to be changed so that it is no longer lengthy. Suppose your database does not contain a InnoDB table. To suppress related messages. You can add the "--skip-innodb" option for the options file under Group [Libmysqd_server] or in mysql_server_ This option is added when Init () initializes the server.

25.1.6. Embedded Server Demo sample

On Linux or FreeBSD systems, you can use the following two demo routines without changes. For other operating systems, minor changes are required, primarily the file path. The purpose of designing these two demo samples is to provide you with enough detail information. To understand the problem, they are a necessary part of the actual application. The first 1 Demo sample is very intuitive. The 2 Demo sample uses a number of error checking functions. Somewhat complicated. A command line entry for compiling the program is given after the 1 Demo sample.

After the first 2 Demo sample, the gnumake file is given, which can be used for compiling.

Demo Sample: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;
}

The following command-line commands are given to compile the above program:

GCC Test1_libmysqld.c-o test1_libmysqld-lz \
'/usr/local/mysql/bin/mysql_config--include--libmysqld-libs '

Demo Sample:2

You want to test the demo sample. Create a test2_libmysqld folder that is sibling to the MySQL source folder.

Save the test2_libmysqld.c source files and gnumakefile to the folder. and execute GNU make under the test2_libmysqld folder .

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 is called before any other MySQL
* Functions.
*
* 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
* the 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 stubs that does nothing.
*/
Mysql_server_init (argc, argv, (char * *) server_groups);
one = Db_connect ("test");
both = Db_connect (NULL);
Db_do_query (One, "SHOW TABLE STATUS");
Db_do_query (both, "SHOW DATABASES");
Mysql_close (both);
Mysql_close (one);
/* This must is 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 the client and server use separate group names.
* This was 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 had 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
# 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

LIBMYSQLD, embedded MySQLServer Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.