[GIS] Shapefile to Mysql error using ogr2ogr

gdalhomebrewMySQLogr2ogrshapefile

I'm following this tutorial. I installed the “GDAL Complete” Framework from kyngchaos.com and I tested the installation on Mac. I have GDAL 1.11.2, released 2015/02/10.

When I am trying to run this command:

ogr2ogr -f MySQL MySQL:dbname,user=root,password=password \
      -nln world_borders -nlt MULTIPOLYGON \
      -update -overwrite \
      -lco ENGINE=MyISAM \
      -lco MYSQL_FID=ogr_fid -lco GEOMETRY_NAME=geometry \
      /path/to/TM_WORLD_BORDERS-0.3.shp

With my settings it would be:

ogr2ogr -f MySQL MySQL:mapas,user=xxxx,password=xxxx \
      -nln test -nlt MULTIPOLYGON \
      -update -overwrite \
      -lco ENGINE=MyISAM \
      -lco MYSQL_FID=ogr_fid -lco GEOMETRY_NAME=geometry \
      /Users/Seph/Downloads/TM_WORLD_BORDERS-0.3/TM_WORLD_BORDERS-0.3.shp

I got this error:

Unable to find driver `MySQL'.
The following drivers are available:
-> `ESRI Shapefile'
-> `MapInfo File'
-> `UK .NTF'
-> `SDTS'
-> `TIGER'
-> `S57'
-> `DGN'
-> `VRT'
-> `REC'

I have created the table on my database before running the command.

UPDATE
I installed Homebrew I and ran this command:

brew install gdal --with-mysql

Now I have the supported format: -> "MySQL" (read/write)

But now I have this error:

Macbook-pro:~ Seph$ ogr2ogr -f MySQL MySQL:mapas,user=xxxx,password=xxxx\
      -nln test -nlt MULTIPOLYGON \
      -update -overwrite \
      -lco ENGINE=MyISAM \
      -lco MYSQL_FID=ogr_fid -lco GEOMETRY_NAME=geometry \
       /Users/Seph/Downloads/TM_WORLD_BORDERS-0.3/TM_WORLD_BORDERS-0.3.shp
      ERROR 1: MySQL connect failed for: mapas,user=xxxx,password=xxxx
      Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
      ERROR 1: MySQL connect failed for: mapas,user=xxxx,password=xxxx
      Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
      ERROR 1: MySQL connect failed for: mapas,user=xxxx,password=xxxx
      Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
      ERROR 1: MySQL driver doesn't currently support database creation.
      Please create database before using.
      MySQL driver failed to create MySQL:mapas,user=xxxx,password=xxxx

I am using phpMyAdmin from Xammp

Best Answer

I know it's been 5 months since this question was posted, but I thought I'd add in my $.02. I, too, had the problem of ogr2ogr not being able to connect.

In my case, it turned out that ogr2ogr expects the mysql.sock file to be in /tmp/. If you're running MAMP, your sock file is actually located in /Applications/MAMP/tmp/mysql/mysql.sock. Creating a symlink fixed the issue for me:

ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/

Also, if you're using Postgres.app AND gdal, be aware that you'll have two versions of ogr2ogr installed. Updating my .bash_profile to look for the version installed via brew install gdal --with-mysql did the trick:

alias ogr2ogr=/usr/local/bin/ogr2ogr

Place that above the PATH directive so it overrides the /Applications/Postgres.app/Contents/Versions/9.3/bin path include that should be in your PATH variable.

I mention this because @jmcboom asked you to run ogrinfo. If, like me, you did that and got no MySQL driver, it is probably because the ogrinfo you're running is looking at the Postgres.app version, not your brew version.

I hope this helps.

Cheers, Steve