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

<% case node['platform_family'] -%>
<% when "debian" -%>
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '<%= node['mysql']['server_debian_password'] %>' WITH GRANT OPTION;
<% end -%>
# Grant replication for a slave user.
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' identified by '<%= node['mysql']['server_repl_password'] %>';

# Set the server root password. This should be preseeded by the package installation.
<% if node['mysql']['allow_remote_root'] -%>
GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '<%= node['mysql']['server_root_password'] %>' WITH GRANT OPTION;
<% else %>
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
<% end -%>
UPDATE mysql.user SET Password=PASSWORD('<%= node['mysql']['server_root_password'] %>') WHERE User='root';

# Remove anonymous users
<% if node['mysql']['remove_anonymous_users'] -%>
DELETE FROM mysql.user WHERE User='';
<% end -%>

# Remove test database and access to it
<% if node['mysql']['remove_test_database']  -%>
DROP DATABASE test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%';
<% end -%>
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('<%= node['mysql']['server_root_password'] %>');

# allow root to connect from a remote network if root_network_acl is not nil
<% if node['mysql']['root_network_acl'] -%>
SET PASSWORD FOR 'root'@'<%= node['mysql']['root_network_acl'] %>' = PASSWORD('<%= node['mysql']['server_root_password'] %>');
<% end -%>
