简体   繁体   中英

how can i create a mysql table using puppet?

I am trying to create a table in a database I have already created via puppet.

I am using the mysql module from puppet forge and have been searching for hours on how to create a table...the documentation about the module seems to only explain about databases but not about tables.

Other places on the web don't really mention this case, which makes me think that I am missing something very basic: what am I missing?

Tables are part of your data. The schema of your database is not part of configuration per se.

As such, you should not try and manage tables through Puppet.

There are not currently any good puppet modules for managing database schema from puppet, although I agree something like that would be useful.

The closest you can get is to use a separate executable that migrates your database schema and using an exec resource from your puppet manifest to kick it off. There are several schema migration tools that you might use.

For example, if you were to use Flyway you would run something like the following:

exec { 'Migrate Database Schema':
  path    => '/directory/with/flyway',
  cwd     => '/directory/with/migration/scripts',
  command => 'flyway migrate',
  onlyif  => 'flyway validate'
}

EDIT:

The ability to apply schema changes has come up fairly often, so I just published a puppet module ( database_schema ) that uses flybase or liquibase to populate a database as part of a puppet manifest. Since it uses migration tools, it can handle both initial database population as well as applying migration scripts to an existing database.

As an example, if you wanted to use flyway to apply simple sql migration scripts, it would look something like this (where the schema_source is a directory of sql scripts with version prefixes):

include ::database_schema::flyway

database_schema::flyway_migration { 'Migrate TestDB':
  db_username   => root,
  db_password   => password,
  jdbc_url      => 'jdbc:mysql://localhost/testdb',
  schema_source => '/vagrant/tests/data/stage1'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM