# Generated by Chef for <%= node["fqdn"] %>.
# Local modifications will be overwritten.

<% if node["platform_family"] == "debian" -%>
# debian-sys-maint user for administration
  <% [['SELECT', 'mysql.user'], ['SHUTDOWN', '*.*']].each do |priv, loc| %>
<%= "GRANT #{priv} ON #{loc} TO '#{@debian_user}'@'localhost';" %>
  <% end -%>
SET PASSWORD FOR '<%= @debian_user %>'@'localhost' = PASSWORD('<%= @debian_password %>');
<% end -%>


<% if node["percona"]["backup"]["configure"] %>
# Grant permissions for the XtraBackup user
# Ensure the user exists, then revoke all grants, then re-grant specific permissions
GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO '<%= node["percona"]["backup"]["username"] %>'@'localhost' IDENTIFIED BY '<%= @backup_password %>';
REVOKE ALL PRIVILEGES, GRANT OPTION FROM '<%= node["percona"]["backup"]["username"] %>'@'localhost';
GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO '<%= node["percona"]["backup"]["username"] %>'@'localhost' IDENTIFIED BY '<%= @backup_password %>';
<% end -%>

# Set the server root password. This should be preseeded by the package installation.
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('<%= @root_password %>');


FLUSH PRIVILEGES;

# Generated by Chef for <%= node["fqdn"] %>.
# Local modifications will be overwritten.

<% if node["platform_family"] == "debian" -%>
# debian-sys-maint user for administration
  <% if node["percona"]["version"] >= "5.7" -%>
CREATE USER '<%= @debian_user %>'@'localhost' IDENTIFIED BY '<%= @debian_password %>';
  <% end -%>
  <% [['SELECT', 'mysql.user'], ['SHUTDOWN', '*.*']].each do |priv, loc| %>
<%= "GRANT #{priv} ON #{loc} TO '#{@debian_user}'@'localhost';" %>
  <% end -%>
SET PASSWORD FOR '<%= @debian_user %>'@'localhost' = PASSWORD('<%= @debian_password %>');
<% end -%>


<% if node["percona"]["backup"]["configure"] %>
# Grant permissions for the XtraBackup user
# Ensure the user exists, then revoke all grants, then re-grant specific permissions
GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO '<%= node["percona"]["backup"]["username"] %>'@'localhost' IDENTIFIED BY '<%= @backup_password %>';
REVOKE ALL PRIVILEGES, GRANT OPTION FROM '<%= node["percona"]["backup"]["username"] %>'@'localhost';
GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO '<%= node["percona"]["backup"]["username"] %>'@'localhost' IDENTIFIED BY '<%= @backup_password %>';
<% end -%>

# Set the server root password. This should be preseeded by the package installation.
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('<%= @root_password %>');


FLUSH PRIVILEGES;

Create the user in MySQL 5.7+ before adding GRANTs
Create the user in MySQL 5.7+ before adding GRANTs
