Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back up a table with a GEOMETRY column using mysqldump?

Tags:

mysql

geometry

I recently created a MySQL table with a column of type GEOMETRY.

When I back up the table with mysqldump, it outputs my geometry column as a quoted string, containing some escaped characters like \0, and also some characters that look like raw binary bytes in the upper-ASCII range.

When I try to restore the dump to another database it fails with an error:

"Cannot get GEOMETRY object from the data you send to the Geometry field".

I tried adding --hex-blob to my command line but this does not change the output or fix the problem.

I'm sure someone didn't create a data type in MySQL and forget to include a way to back it up. What am I missing?

Thanks.

like image 788
Frank LaRosa Avatar asked Jul 05 '13 03:07

Frank LaRosa


People also ask

Can I restore a single table from a full MySQL Mysqldump file?

Use the sed command on your bash shell to separate the data of the table that you want to restore. For example, if we want to restore only the “film_actor” table to “sakila” database we execute the script below.

How do I export a column in MySQL?

You can do it very easily using MySQL GUI tools like SQLyog, PHPMyAdmin. In SQLyog you just need to select the table, Click on "Export As..." Icon and you will get dialog to select the columns that you want to Export. Then click on "Export Button". Export is done...

Which statement can you use to load data from a file into a table?

The LOAD DATA statement reads rows from a text file into a table at a very high speed. The file can be read from the server host or the client host, depending on whether the LOCAL modifier is given.


2 Answers

In my case, this error appeared specifically with empty geometry values in a non-null geometry column.

In my case, the empty geometries were legitimate cases of unknown geometry, so I addressed this by changing the column to allow null values, and then running UPDATE ... SET geom = NULL WHERE IsEmpty(geom);

After this, I was able to re-run mysqldump and successfully import the resulting sql into a separate database.

(To be honest, I'm not sure how the empty geometry values got there in the first place - I don't even know the syntax to create an empty geometry value)

like image 121
James Avatar answered Nov 15 '22 09:11

James


Frank, this appears to be a long-standing (and still open) bug with mysqldump. See http://bugs.mysql.com/bug.php?id=43544.

As a workaround, you may be able to use the ogr2ogr tool to export the data to a shapefile, and then import it back into the database. See http://www.bostongis.com/PrinterFriendly.aspx?content_name=ogr_cheatsheet

like image 23
lreeder Avatar answered Nov 15 '22 08:11

lreeder