Skip to content

run mysql_install_db if datadir is set annd mysql database is missing #405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

run mysql_install_db if datadir is set annd mysql database is missing #405

wants to merge 1 commit into from

Conversation

vicinus
Copy link

@vicinus vicinus commented Dec 26, 2013

If the datadir is changed in the my.cnf, then mysql_install_db must be run to create the basic mysql database etc. in the new datadir. This change checks if datadir is set and if the directory mysql is missing in the datadir, if so mysql_install_db is run.

@@ -6,4 +6,17 @@
name => $mysql::server::package_name,
}

# run mysql_install_db if datadir is set and datadir doesn't contain the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment here says the same thing as the code, one of the two is unnecessary ;)
Perhaps you should instead comment on the "why"

@vicinus
Copy link
Author

vicinus commented Jan 3, 2014

I can change the comment if it is that obvious what the code is doing. But for me it was more obvious, that the mysql server without the mysql database won't start ;-)

@igalic
Copy link
Contributor

igalic commented Jan 3, 2014

So a proper comment may be:

# mysql server won't start without this:

(:

@vicinus
Copy link
Author

vicinus commented Jan 3, 2014

I would rather change it to:

# setup mysql database, if missing:

because "mysql server won't start without this" is in most cases (datadir isn't changed) false.

@igalic
Copy link
Contributor

igalic commented Jan 3, 2014

anyway, ignoring my pedantry with regard to comments, is there a way to add tests for this change?

@vicinus
Copy link
Author

vicinus commented Jan 3, 2014

I appreciate any feedback, even if I disagree, because normally it improves the code and my understanding of it.

I also thought about a test for this change. But I couldn't come up with a good test. This could be because I'm not very familiar with the puppet test environment. Partly because they won't run on my main computer, because of ruby version issues.

The only test I came up with was something like:

describe 'mysql::server::install class' do
  puppet_apply(%{
    class { 'mysql::server': 
      override_options => {
        mysqld => {
          datadir => /tmp
        }
      }
    }
  })

  describe file('/tmp/mysql') do
    it { should be_directory }
  end
end

I can add this test, but I'm not sure if /tmp is the correct directory to use or if this test is really useful and will work at all. So feedback would be appreciated.

@igalic
Copy link
Contributor

igalic commented Jan 6, 2014

@apenney what'd you think?

@apenney
Copy link
Contributor

apenney commented Jan 6, 2014

In terms of tests I would do something like:

1: run apply_manifest("class { 'mysql::server': datadir => '/tmp/test' }", :catch_failures => true) to get a baseline.
2: Delete /tmp/test
3: run the same apply_manifest
4: Check if /tmp/test exists and check if /tmp/test/mysql.whatever file exists too. Basically when it recreates those files it's always $datadir/mysql.something, so we can check that.
5: Check the service is running

Those checks combined should be enough to ensure it's able to create properly. Failing that you can just run it for the first time with /tmp/test and then immediately check the /tmp/test/mysql* files exist. Make sense?

@vicinus
Copy link
Author

vicinus commented Jan 9, 2014

If I'm not mistaken, then this kind of test must be an acceptance tests. I have found no way to run them, because 'rake spec' doesn't seem to work. I found some help how to run them with vagrant, but that seems overly complicated, just to check if the test is working as intended.

Is there some documentation how to run the acceptance tests, possibly without having to setup vagrant or other vm solutions?

@igalic
Copy link
Contributor

igalic commented Jan 10, 2014

Unfortunately, in order to actually test this there's no choice but actually run it.
The way we run the tests is with under Vagrant.

@dgolja
Copy link
Contributor

dgolja commented Jan 11, 2014

@vicinus I had the same issue ages ago. Just install https://rvm.io/ and update the "local" copy of ruby to 1.9.3 or something.

@bradleyfrank
Copy link

Hello,

I've been working on installing MariaDB to an alternate location using this mysql module (version 2.1), and I came across your commit to install.pp, which resolved the biggest hurdle I was facing. However, I had to add two lines to your exec statement to make it work:

  path      => '/bin:/sbin:/usr/bin:/usr/sbin',
  require   => Package['mysql-server'],

We are still using Puppet 2.7, and this was applied to a CentOS 6.4 box. If you're still working on making this module install to an alternate datadir, I had to create an additional "wrapper" class to take care of additional pre-install setup, and I'd be happy to share what I've done. But thank you for contributing this code, it saved me a lot of work.

@igalic
Copy link
Contributor

igalic commented Mar 19, 2014

@vicinus would you like to incorporate @bradleyfrank and @apenney's comments, or do you want someone to take over?

@igalic
Copy link
Contributor

igalic commented Apr 14, 2014

@vicinus any progress here?

@DeathBorn
Copy link

It is very annoing that $datadir option is just ignored and database is install into default /var/lib/mysql directory....

@bradleyfrank
Copy link

DeathBorn, it's the exact problem I was running into. I need to install to /srv/database but the mysql module installs to /var/lib/mysql first. The change I added above re-installs the default DB into the new datadir, but the original stays intact, and "remove_default_accounts" doesn't clean it up. My solution/work-around is to make /var/lib/mysql a symlink of /srv/database so everything goes to the right place initially. I ended up not needed vicinus' changes at all. If it helps, here's what I wrote, note that I'm using MariaDB, but you can easily substitute MySQL back in: http://pastebin.com/srjdwtcf

@igalic
Copy link
Contributor

igalic commented Apr 16, 2014

@bradleyfrank there's nothing wrong adding the code here, do you mind if I copy the pastebin in the comment?

class hmdc_mariadb::datadir inherits hmdc_mariadb {

  Exec {
    path => $exec_path,
  }

  exec { 'fcontext-mariadb':
    command => "semanage fcontext -a -t ${mariadb_context} \"${semanage_path}\"",
    unless  => "semanage fcontext -l | grep \"^${semanage_path}.*:${mariadb_context}:\"",
  }

  exec { 'restorecon-mariadb':
    command => "restorecon -R ${datadir}",
    unless  => "matchpathcon -V ${datadir} | grep verified",
  }

  file { $datadir:
    ensure => 'directory',
    group  => 'mysql',
    mode   => '0755',
    owner  => 'mysql',
  }

  file { "${datadir}/lost+found":
    ensure => 'absent',
    force  => true,
  }

  file { '/var/lib/mysql':
    ensure => link,
    target => $datadir,
  }

  # From the MariaDB-server RPM package:
  # /usr/sbin/groupadd -g 27 -o -r mysql
  group { 'mysql':
    ensure  => 'present',
    allowdupe => true,    # -o
    gid       => '27',    # -g 27
    system    => true,    # -r
  }

  # From the MariaDB-server RPM package:
  # /usr/sbin/useradd -M -N -g mysql -o -r -d $datadir -s /bin/bash
  #   -c "MySQL Server" -u 27 mysql
  user { 'mysql':
    ensure     => 'present',
    allowdupe  => true,             # -o
    comment    => 'MariaDB Server', # -c "MySQL Server"
    home       => $datadir,         # -d $datadir
    gid        => 'mysql',          # -g mysql
    managehome => false,            # -M
    require    => Group['mysql'],   #
    shell      => '/bin/bash',      # -s /bin/bash
    system     => true,             # -r
    uid        => '27',             # -u 27
  }

  User['mysql'] ->
  File[$datadir] ->
  File['/var/lib/mysql'] ->
  Exec['fcontext-mariadb'] ->
  Exec['restorecon-mariadb']


class hmdc_mariadb (
  $datadir = $hmdc_mariadb::params::datadir,
) inherits hmdc_mariadb::params {

  require 'mariadb_repo'
  include hmdc_mariadb::datadir


  package { 'MariaDB-shared':
    ensure => 'present',
  }

  class { '::mysql::server':
    databases               => $databases,
    override_options        => $override_options,
    grants                  => $grants,
    package_name            => 'MariaDB-server',
    remove_default_accounts => true,
    restart                 => true,
    service_enabled         => true,
    service_name            => 'mysql',
    users                   => $users,
  }

  class { '::mysql::client':
    package_ensure => 'present',
    package_name   => 'MariaDB-client'
  }

  Class['hmdc_mariadb::datadir'] ->
  Package['MariaDB-shared'] ->
  Class['::mysql::client'] ->
  Class['::mysql::server']

@bradleyfrank
Copy link

Sure, no problem! It was just lengthy and I wasn't sure what the etiquette was. Hopefully it can help someone else with the same problem!

@DeathBorn
Copy link

I thought about using symlink or just mounting a directory and chose the second option.Thank you, bradleyfrank, for providing configuration.

@kid-icarus
Copy link

Any progress here?

@apenney
Copy link
Contributor

apenney commented Jul 24, 2014

@vicinus Any chance of a rebase here?

@underscorgan
Copy link
Contributor

Fixed in #536

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants