【オリジナル】modb機能設計の「一部MySQLクライアントプロトコルをサポート」-1


にある
modb機能設計の「マルチ消費者単一生産者のサポート」
』では、sql文を処理するために個別のスレッドが必要だと述べています.ここでは「
サポート部分MySQLクライアントプロトコル」の第1部で説明します.
最初のアイデアは、rabbitmqクライアントがqueueからsql文を含むメッセージを消費した後、sqlを抽出して分析した後、MySQLプロトコルを通じてデータベースにsql文を実行するように要求することです.これは私のこのdemoがMySQLクライアント側に必要なプロトコル部分を実現しなければならないことを要求しています.これを実現するには、3つの方法があります.
 
  • 会社の既存dbiのライブラリを利用してMySQLアクセス
  • を実現
  • mysqlclientライブラリに基づいてMySQLクライアント側プロトコル
  • を開発実現
  • MySQLクライアント側に必要なプロトコルを独自に実装する
  • 私の選択:
    会社のdbiライブラリを使うつもりはありません.まず、このライブラリは他の会社のライブラリに基づいていますが、私は使いたくありません.次に、このライブラリのパッケージもよくありません.前に4つの崩壊が見つかりました.他に問題があります.最後に、このライブラリもmysqlclientで作られたパッケージで、上の2番目と同じです.だから、最終的に私は第2の方法を選んで、自分で“山の頭”を出します.PS:実は最初は上の3つ目の方法を採用したいと思っていましたが、自分が下からMySQLクライアントプロトコルをどのようにサポートするかを実現し、結果は「ほほほ」で、難易度と仕事量は普通ではありません~~
     
    何に基づいて開発を行うかを決定した後、次の目標を選択します.
  • mysqlclientが提供するAPIを直接使用して、通常のブロック呼び出しを実現することは私の要求を満たすことができますか?
  • libeventに基づいてイベント駆動式の呼び出しモードを実現する必要がありますか?難易度は高いですか? 
  • 適切なオープンソースMySQLクライアント実装は私の考えを啓発しますか?参考にしてくれませんか. 

  • 私が何でも知っていると思ってはいけません.率直に言って、この開発をする前に、私は上記の問題について何も知りませんでしたが、私は自分の学習能力を心配していません.
    そこでいろいろ検索を始めたのですが...
    参照できるクライアントインプリメンテーションには、次のようなものがあります.
    4.5. MySQL Client Programs
    4.5.1. mysql — The MySQL Command-Line Tool
    4.5.2. mysqladmin — Client for Administering a MySQL Server
    4.5.3. mysqlcheck — A Table Maintenance Program
    4.5.4. mysqldump — A Database Backup Program
    4.5.5. mysqlimport — A Data Import Program
    4.5.6. mysqlshow — Display Database, Table, and Column Information
    4.5.7. mysqlslap — Load Emulation Client
    その中でmysql、mysqladmin、mysqlshow、mysqlslapは研究に値するように見えます.
    そこでLinuxの下でC言語APIでMySQLデータベースに接続する方法を研究し始めた.
    MySQL公式サイトで以下の内容を見つけ、MySQL C APIによるクライアントアプリケーションの実装についての説明(個人まとめ)
    ================================
    22.8.1. MySQL C API Implementations
    MySQL C APIは、クライアントアプリケーションに対して、MySQLサーバと通信するために使用されるCベースのAPIを提供する.
    libmysqlclient(クライアントバージョン)とlibmysqld(組み込みサーババージョン)の2つのバージョンのライブラリを使用できます.
    Unix(およびUnix-like)システムでは、静的ライブラリはlibmysqlclientである.a,ダイナミックライブラリはlibmysqlclient.so .
    バンド_r接尾辞のライブラリの意味:MySQL 5.5以前に、r接尾辞のライブラリはthread-safe(re-entrant)のライブラリであり、そうでない場合はそうではありません.MySQL 5.5以降は、r接尾辞はthread-safe(re-entrant)のライブラリなので、理論的にはテープは必要ありません.rのライブラリです.
    22.8.2. Simultaneous MySQL Server and MySQL Connector/C Installations
    これは主にMySQLサーバーとMySQL Connector/Cを同時にインストールする問題で、私がやるべきこととは関係ありません.
    22.8.3. Example C API Client Programs
    これは、クライアント・プログラムを記述するためにC APIを使用する方法を示すはずですが、clientフォルダの下のソースコードを参照できることを示しています.大体スキャンして、参考にすることができて、しかもコードの量が小さいのはmysqlshowです.c .
    22.8.4. Building and Running C API Client Programs
    これは主にC APIのクライアントプログラムの構築と実行方法を説明し、主に3つの部分に分けて説明します.
    22.8.4.1. Building C API Client Programs
    クライアント・プログラムのコンパイル方法の例
    shell> gcc -c `mysql_config --cflags` progname.c
    shell> gcc -o progname progname.o `mysql_config --libs`
    22.8.4.2. Writing C API Threaded Client Programs
    C APIを呼び出すときにスレッドのセキュリティを保証する方法について説明します
    22.8.4.3. Running C API Client Programs
    システムのアップグレードによるヘッダファイルmysqlを避ける.hとライブラリlibmysqlclient.aの不一致問題、問題が発生した場合は新しいヘッダファイルとライブラリを使用して再コンパイルする必要があります.ダイナミックライブラリに依存する場合、libmysqlclient.so.17からlibmysqlclient.so.18などのプライマリ・バージョンの変更が発生した場合は、再コンパイルする必要があります.
    22.8.5. C API Data Structures
    ここでは各種データ構造について説明する(略)
    ================================
    まあ、どうせどちらも私にはよく知らないので、その中のmysqlshowを研究してみましょう.
    まずやるべきことは、自分がmysqlshowのコンパイルに成功することです.
    (...中間過程のピチャピチャを省略...)
    自作工事目録は以下の通りである.
    [root@Betty mysql_client_test]# ll 
       116 
    -rw-r--r-- 1 root root   4130 10-25 14:33 client_priv.h 
    -rw-r--r-- 1 root root   2124 10-25 15:01 my_default.h 
    -rw-r--r-- 1 7161 wheel 25393 10-25 15:03 mysqlshow.c 
    -rw-r--r-- 1 root root   1686 10-25 15:01 welcome_copyright_notice.h 
    [root@Betty mysql_client_test]#  

    コンパイルに成功
    [root@Betty mysql_client_test]# gcc mysqlshow.c -o mysqlshow `mysql_config --cflags` `mysql_config --libs` -DDBUG_OFF  
    [root@Betty mysql_client_test]# ll 
       116 
    -rw-r--r-- 1 root root   4130 10-25 14:33 client_priv.h 
    -rw-r--r-- 1 root root   2124 10-25 15:01 my_default.h 
    -rwxr-xr-x 1 root root  66953 10-25 16:23 mysqlshow 
    -rw-r--r-- 1 7161 wheel 25393 10-25 15:03 mysqlshow.c 
    -rw-r--r-- 1 root root   1686 10-25 15:01 welcome_copyright_notice.h 
    [root@Betty mysql_client_test]#  

    実行
    [root@Betty mysql_client_test]# ./mysqlshow --help 
    ./mysqlshow  Ver 9.10 Distrib 5.6.10, for Linux (x86_64) 
    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 
    
    Oracle is a registered trademark of Oracle Corporation and/or its 
    affiliates. Other names may be trademarks of their respective 
    owners. 
    
    Shows the structure of a MySQL database (databases, tables, and columns). 
    
    Usage: ./mysqlshow [OPTIONS] [database [table [column]]] 
    
    If last argument contains a shell or SQL wildcard (*,?,% or _) then only 
    what's matched by the wildcard is shown. 
    If no database is given then all matching databases are shown. 
    If no table is given, then all matching tables in database are shown. 
    If no column is given, then all matching columns and column types in table 
    are shown. 
    
    Default options are read from the following files in the given order: 
    /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf  
    The following groups are read: mysqlshow client 
    The following options may be given as the first argument: 
    --print-defaults        Print the program argument list and exit. 
    --no-defaults           Don't read default options from any option file, 
                            except for login file. 
    --defaults-file=#       Only read default options from the given file #. 
    --defaults-extra-file=# Read this file after the global files are read. 
    --defaults-group-suffix=# 
                            Also read groups with concat(group, suffix) 
    --login-path=#          Read this path from the login file. 
      --bind-address=name IP address to bind to. 
      -c, --character-sets-dir=name  
                          Directory for character set files. 
      --default-character-set=name  
                          Set the default character set. 
      --count             Show number of rows per table (may be slow for non-MyISAM 
                          tables). 
      -C, --compress      Use compression in server/client protocol. 
      -#, --debug[=name]  Output debug log. Often this is 'd:t:o,filename'. 
      --debug-check       Check memory and open file usage at exit. 
      --debug-info        Print some debug info at exit. 
      --default-auth=name Default authentication client-side plugin to use. 
      -?, --help          Display this help and exit. 
      -h, --host=name     Connect to host. 
      -i, --status        Shows a lot of extra information about each table. 
      -k, --keys          Show keys for table. 
      -p, --password[=name]  
                          Password to use when connecting to server. If password is 
                          not given, it's solicited on the tty. 
      --plugin-dir=name   Directory for client-side plugins. 
      -P, --port=#        Port number to use for connection or 0 for default to, in 
                          order of preference, my.cnf, $MYSQL_TCP_PORT, 
                          /etc/services, built-in default (3306). 
      --protocol=name     The protocol to use for connection (tcp, socket, pipe, 
                          memory). 
      -t, --show-table-type  
                          Show table type column. 
      -S, --socket=name   The socket file to use for connection. 
      -u, --user=name     User for login if not current user. 
      -v, --verbose       More verbose output; you can use this multiple times to 
                          get even more verbose output. 
      -V, --version       Output version information and exit. 
    
    Variables (--variable-name=value) 
    and boolean options {FALSE|TRUE}  Value (after reading options) 
    --------------------------------- ---------------------------------------- 
    bind-address                      (No default value) 
    character-sets-dir                (No default value) 
    default-character-set             auto 
    count                             FALSE 
    compress                          FALSE 
    debug-check                       FALSE 
    debug-info                        FALSE 
    default-auth                      (No default value) 
    host                              (No default value) 
    status                            FALSE 
    keys                              FALSE 
    plugin-dir                        (No default value) 
    port                              0 
    show-table-type                   FALSE 
    socket                            (No default value) 
    user                              (No default value) 
    [root@Betty mysql_client_test]#  

    確かにソースコードコンパイルMySQLで得られたmysqlshowと一致しており、ここでしばらくMySQLクライアントの実現を研究する第一歩を踏み出した.少しまとめてもいいです.
  • MySQL C APIを直接使用してプログラムを記述するのはブロックされていますか?それともブロックされていませんか? 
  • MySQL C APIはlibeventと結合できますか?あるいはどのAPIができますか?
  • libeventベースのMySQLプロトコルを実装する必要がありますか?

  • 上記の問題は総合的な全体設計で結論を出す必要がある(読者自身が先に考える).
    ==未完待機==