[GIS] QGIS 2.0 without MySQL connector

connectiongdalMySQLogrqgis

On a mac (Mountain Lion), after a major upgrade to GDAL (1.10) and related frameworks from KyngChaos page, QGIS 2.0 doesn't show any MySQL connector:

Add Vector > Database > …

The driver doesn't appear neither in the terminal running the "ogr2ogr –formats" command.
I read (link), at the very end of the post, that this feature has been removed from QGIS 2.0. Anyway, it appears to me that it depends on GDAL/OGR: it seems that the framework has been build without MySQL support.

Best Answer

It doesn't look like it is included in the most recent GDAL-Complete framework. Please file a request with kyngchaos.com.

In the meantime, if you don't mind trying it yourself, you can attempt to build a shared plugin for GDAL 1.10, install it, and see if it works with your data source:

  1. Ensure you have XCode and/or its command line tools installed.

  2. Download and install latest MySQL Community Server (installs to /usr/local/mysql).

  3. Download the GDAL source and un-archive to folder, e.g. gdal-1.10.1, and enter it.

    $ cd /path/to/gdal-1.10.1
    
  4. Edit a single source file and comment-out unsupported symbols in MySQL 5.6+ client

    ogr/ogrsf_frmts/mysql/ogrmysqldatasource.cpp @ line 130

    /* -------------------------------------------------------------------- */
    /*      Use options process to get .my.cnf file contents.               */
    /* -------------------------------------------------------------------- */
        int nPort = 0, i;
        char **papszTableNames=NULL;
        std::string oHost, oPassword, oUser, oDB;
    //     char *apszArgv[2] = { (char*) "org", NULL };
    //     char **papszArgv = apszArgv;
    //     int  nArgc = 1;
    //     const char *client_groups[] = {"client", "ogr", NULL };
    
        my_init(); // I hope there is no problem with calling this multiple times!
    //     load_defaults( "my", client_groups, &nArgc, &papszArgv );
    // 
    //     for( i = 0; i < nArgc; i++ )
    //     {
    //         if( EQUALN(papszArgv[i],"--user=",7) )
    //             oUser = papszArgv[i] + 7;
    //         else if( EQUALN(papszArgv[i],"--host=",7) )
    //             oHost = papszArgv[i] + 7;
    //         else if( EQUALN(papszArgv[i],"--password=",11) )
    //             oPassword = papszArgv[i] + 11;
    //         else if( EQUALN(papszArgv[i],"--port=",7) )
    //             nPort = atoi(papszArgv[i] + 7);
    //     }
    // 
    //     // cleanup
    //     free_defaults( papszArgv );
    
  5. Build shared plugin, in Terminal:

    # make directory for build output
    $ mkdir ogr_plugins
    
    # compile shared plugin, statically linking in libmysqlclient (multi-line, single command)
    $ g++ -Wall -g ogr/ogrsf_frmts/mysql/*.c* \
    -shared -o ogr_plugins/ogr_MySQL.dylib /usr/local/mysql/lib/libmysqlclient.a \
    -Iport -Igcore -Iogr -Iogr/ogrsf_frmts -Iogr/ogrsf_frmts/mysql \
    -I/Library/Frameworks/GDAL.framework/Headers -I/usr/local/mysql/include \
    -framework GDAL -lz -install_name ogr_MySQL.dylib
    
    */ #just closing comment for SE syntax highlighting 
    
    # copy plugin to known GDAL_DRIVER_PATH (sudo needed)
    $ cd ogr_plugins
    $ sudo cp -f ogr_MySQL.dylib /Library/Application\ Support/GDAL/1.10/PlugIns/
    
  6. Verify plugin:

    $ cd /path/to/gdal-1.10.1
    $ otool -L ogr_plugins/ogr_MySQL.dylib
    ogr_plugins/ogr_MySQL.dylib:
            ogr_MySQL.dylib (...)
            /Library/Frameworks/GDAL.framework/Versions/1.10/GDAL (...)
            /usr/lib/libz.1.dylib (...)
            /usr/lib/libstdc++.6.dylib (...)
            /usr/lib/libSystem.B.dylib (...)
    
    $ ogrinfo --formats | grep 'MySQL'
    -> "MySQL" (read/write)
    
  7. Launch QGIS and verify plugin loading in PyQGIS console:

    from osgeo import ogr
    sorted([ogr.GetDriver(drv).name for drv in range(0,ogr.GetDriverCount())])
    
  8. You should be able to connect to a MySQL data source via Add Vector > Database > ... now.

Please note, I don't have any geospatial MySQL data sources, so was unable to test the plugin I built.