blog.plee.me About software, technology and random things

7Dec/170

Rewrite MySQL / MariaDB Database Dump Create View Statements For Current User

Hi!

If you would like to import a database dump file created by MySQL's or MariaDB's mysqldump executable, but it contains statements to create views, the import process may abort with an error when it comes to creating views.

When creating a view, MySQL wants to know who created it, and for that it needs a username and the host. If the user who is executing the import does not have sufficient privileges or the original user referenced in the dump does not exist (for example when importing the dump into a fresh database for a migration), this leads to an error.

Usually when I import a dump, I don't care so much about the "SQL Security Definer", so I just want to set it to the importing user.

You can generate a new, modified SQL dump file very easily with the following shell command:

$ sed -r 's#^(/\*!50013 DEFINER=).+?( SQL SECURITY DEFINER \*/)$#\1CURRENT_USER\2#' input_file.sql > output_file.sql

This command simply scans through the entire dump, looking for the statement created by mysqldump which triggers the database to create the view if it doesn't exist already. It then sets the user information to CURRENT_USER which refers to the user that is currently executing the import.

Please note that because the search pattern is so specific, it will probably require some modification in the future, depending on the version of mysqldump you're using and if / how they change this particular statement. On the upside the chance that it will accidentally modify something it shouldn't is pretty low.

I hope this is helpful to you!

Thanks for reading!

Comments (0) Trackbacks (0)

No comments yet.


Leave a Reply

No trackbacks yet.