mysql\_database

January 13, 2026 ยท View on GitHub

Manage MySQL databases and execute SQL queries on them. It works by establishing a control connection to the MySQL server using the MySQL client (be sure it is installed before using this resource).

Actions

  • create - (default) to create a named database
  • drop - to drop a named database
  • query - to execute a SQL query

Properties

NameTypesDescriptionDefaultRequired?
database_nameStringthe name of the database to managename if not specifiedno
hostStringhost to connect tolocalhostno
portIntegerport of the host to connect to3306no
userStringthe username of the control connectionrootno
passwordStringpassword of the user used to connect tono
socketStringUnix socket path for local connectionsno
encodingStringdefault character set for the databaseutf8no
collationStringdefault collation for the databaseutf8_general_cino
sqlStringthe SQL query to execute (for :query action)no
instanceStringthe mysql service instance namedefaultno

When host has the value localhost, it will try to connect using a Unix socket, or TCP/IP if no socket is defined.

Examples

# Create a database
mysql_database 'wordpress-cust01' do
  host '127.0.0.1'
  user 'root'
  password node['wordpress-cust01']['mysql']['initial_root_password']
  action :create
end

# Drop a database
mysql_database 'baz' do
  action :drop
end

# Query a database
mysql_database 'flush the privileges' do
  sql 'flush privileges'
  action :query
end

The query action will NOT select a database before running the query, nor return the actual results from the SQL query.