How do I dump view schemas for a MySQL database?

43,335

Solution 1

I think you might either be passing some other options to mysqldump, or using a version of mysqldump that doesn't understand views (perhaps it's too old). When I run mysqldump --no-data, it does dump out the view definitions. See the below:

/*!50001 CREATE ALGORITHM=UNDEFINED */
/*!50013 DEFINER=`root`@`localhost` SQL SECURITY DEFINER */
/*!50001 VIEW `t` AS select 1 AS `1` */;

Solution 2

Use --opt option while making the dump:

mysqldump -hserver -uUser -ppasswd -no-data --opt export > export.sql

Solution 3

Mysqldump won't have any options to dump only on views.The below command will help you to take the backup of only views.

mysql -uroot -pPassword INFORMATION_SCHEMA --skip-column-names -e "select table_name from tables where table_type = 'VIEW' and table_schema = 'sakila'" | xargs mysqldump -u root -pPassword sakila > only_views.sql

Share:
43,335

Related videos on Youtube

Chad Johnson
Author by

Chad Johnson

Updated on September 18, 2022

Comments

  • Chad Johnson
    Chad Johnson over 1 year

    I have a MySQL database for which I wish to dump schemas for views. How do I do this? I tried mysqldump with --no-data, but that only dumps table schemas.

  • Thomas Berger
    Thomas Berger over 12 years
    maybe the questioner didn't have a look in the mysqldump? To dump the views commented out is a very bad style by MySQL. There should at least a hint in the docs, but there is none.
  • Chad Johnson
    Chad Johnson over 12 years
    Nevermind, I was wrong. It did dump the views. Thank god. Cool.
  • mdpc
    mdpc over 11 years
    Although for security reasons, I'd not put the password on the command line, I'd let the program prompt for it.
  • Gary
    Gary over 10 years
    These are conditional comments that execute depending on your version of MySQL, see: dev.mysql.com/doc/refman/5.6/en/comments.html
  • Nam G VU
    Nam G VU about 7 years
    I think we need to clarify that export in the command is a specific schema/database name i.e. NOT a mysqldump syntax