sous-chefs / mysql

Development repository for the mysql cookbook
https://supermarket.chef.io/cookbooks/mysql
Apache License 2.0
338 stars 690 forks source link

Initial Records Not Created when data_dir matches MySQL default location #592

Closed mruhlin closed 4 years ago

mruhlin commented 5 years ago

Cookbook version

8.5.2

Chef-client version

13.12.14

Platform Details

Ubuntu 16.04, running in Vagrant and AWS

Scenario:

We're upgrading an existing environment that has been running mysql 5.5 and using the default data location, /var/lib/mysql. We have various scripts that expect the data to be in that location, so would like to keep it there.

[What you are trying to achieve and you can't?]

When setting the data_dir directory, we find that initial records aren't being created. This isn't necessarily a problem in production, since we already have data to put there. But it is a problem when testing the script locally in a dev environment. When the initial records script isn't run, initial_root_password is not set (even to the supposed default of ilikerandompasswords). Instead, MySQL 5.7+ reverts to its default behavior of generating a random password, causing subsequent setup scripts to fail.

Steps to Reproduce:

mysql_service 'companyname' do port '3306'

bind_address mysql_connection_info[:host]

data_dir '/var/lib/mysql' include_dir '/etc/mysql-companyname/conf.d' pid_file '/var/run/mysqld/mysqld.pid' socket '/var/run/mysqld/mysqld.sock'

initial_root_password mysql_connection_info[:password]

version '5.7' action [:create, :start] end

- Add some arbitrary command that tries to log in as root, using the specified password:

Load timezone data

bash 'mysql_tzinfo_to_sql' do code "mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql --user=root --password=\"#{mysql_connection_info[:password]}\" mysql" sensitive true end


- Provision a new Ubuntu 16.04 VM
- Run chef-client

### Expected Result:
database should be created and `mysql_tzinfo_to_sql` script should succeed

### Actual Result:
chef fails while running `mysql_tzinfo_to_sql`
Impossible to login to mysql as root without resetting password. 

Commenting out the `data_dir` line or specifying any other location works.
mruhlin commented 5 years ago

Investigation I've done so far:

mysql_service_base checks for the existence of #{new_resource.data_dir}/mysql/user.frm and only generates initial records if that file doesn't exist.


      def initialize_database
        # initialize database and create initial records
        bash "#{new_resource.name} initial records" do
          code init_records_script
          umask '022'
          returns [0, 1, 2] # facepalm
          not_if "/usr/bin/test -f #{new_resource.data_dir}/mysql/user.frm"
          action :run
        end
      end

Regardless of what data_dir is specified, something earlier on in this recipe creates a default database in /var/lib/mysql, meaning that file always exists.

I haven't yet tracked down where that gets created, but hopefully there is an alternate approach that doesn't need to do that.

github-actions[bot] commented 4 years ago

Marking stale due to inactivity. Remove stale label or comment or this will be closed in 7 days. Alternatively drop by the #sous-chefs channel on the Chef Community Slack and we'll be happy to help! Thanks, Sous-Chefs.

github-actions[bot] commented 4 years ago

Closing due to inactivity. If this is still an issue please reopen or open another issue. Alternatively drop by the #sous-chefs channel on the Chef Community Slack and we'll be happy to help! Thanks, Sous-Chefs.