Provision Vagrant machine with mysql Puppet module - mysql

I am trying to provision a Vagrant machine using this puppet module.
The Hello world is simple, its something like this:
class { '::mysql::server':
root_password => 'strongpassword',
remove_default_accounts => true
}
But my main objective is, when you do vagrant up the first time, the vagrant machine will have a mysql server ready for external access and be able to accept external connections from the host with a specific user.
This is what I tried:
class { '::mysql::server':
root_password => 'strongpass',
remove_default_accounts => false,
override_options => {
mysqld => { bind-address => '0.0.0.0'} //allow entry connections from any ip
}
}
//create a database called `mydb`, a user and a password
mysql::db { 'mydb':
user => 'admin',
password => 'secret',
host => '192.168.33.1',
}
//assign it all the privileges to that user
mysql_grant { 'admin#192.168.33.1/*.*':
ensure => 'present',
options => ['GRANT'],
privileges => ['ALL'],
table => '*.*',
user => 'admin#192.168.33.1',
}
This is the test I do in order to see if it worked:
Destroy the vagrant machine if already exists: vagrant destroy
Create the vagrant machine: vagrant up
Try to connect with MySQLWorkbench.
Question
The weird thing is, when I try to do a connection, it is just not possible, but if I do a vagrant reload --provision then I am able to connect it with MySQLWorkbech. What am I doing wrong?

Related

Docker with Laravel 4.2, connecting to DB throws error: SQLSTATE[HY000] [2002] No such file or directory

I need to get an old version of a Laravel app working.
It is using Laravel version 4.2.2.
I have a docker setup:
version: '3.5'
services:
laravel:
depends_on:
- database
...
database:
image: mysql:5
hostname: database
environment:
MYSQL_ROOT_PASSWORD: mypass
MYSQL_DATABASE: mydb
MYSQL_USER: myuser
MYSQL_PASSWORD: mypass
I added database data to config (Laravel 4.2)
'connections' => array (
'mysql' => array (
'driver' => 'mysql',
'host' => 'database', // as name of db container
'database' => 'mydb', // this database exists, I can see in PhpMyAdmin
'port' => '3306',
'username' => 'root', // I can login with these credentials in PhpMyAdmin and CLI
'password' => 'mypass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
...
I can login to a phpmyadmin container with these credentials, and I also can login from the CLI of laravel container with mysql -h database -u root -p.
If I dump the connection in Laravel Illuminate/Database/Connectors/Connector.php class, I see that the correct config is being used.
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
// dd($dsn);
// dd($config);
// dd($username); dd($password);
return new PDO($dsn, $username, $password, $options);
}
Gives:
string(59) "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=mydb"
array(11) { ["driver"]=> string(5) "mysql" ["host"]=> string(8) "database" ["port"]=> string(4) "3306" ["database"]=> string(6) "mydb" ["username"]=> string(4) "root" ["password"]=> string(21) "mypass" ["charset"]=> string(4) "utf8" ["collation"]=> string(15) "utf8_unicode_ci" ["prefix"]=> string(0) "" ["unix_socket"]=> string(27) "/var/run/mysqld/mysqld.sock" ["name"]=> string(5) "mysql" }
root` (username)
mypass (password)
Why do I still get the error?
I found the problem.
If you look at the $dsn-value in Illuminate/Database/Connectors/Connector.php createConnection:
string(59) "mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=mydb"
You see that host=database is simply missing, even though I added it in laravel config.
Adding it manually solves this problem:
$dsn= "mysql:unix_socket=/var/run/mysqld/mysqld.sock;host=database;dbname=mydb";
Even though I added the host in the config, I found out that in another database-config there was a unix_socket-value added to mysql configuration. Laravel takes either the socket or the host information. My host-value has not been overriden, but the presence of unix_socket-value in the other configuration prevented connection via host.
So I simply removed the unix_socket in another config.
The possible reasons for SQLSTATE[HY000] [2002] is as follows
MYSQL is not running
Incorrect database settings
Insufficient server resources
Based on the analysis of your scenario, the possible reasons is the incorrect database settings
Possible recommendation from my side is to find the right database settings in Laravel.
Make sure the host is correct
Make sure the username is correct
Make sure the password is correct
In most of the cases the problem lies on the host resolution in docker for that I recommend you to find the right hostname and port that the laravel container can able to connect with the mysql container
Following the steps described in this tutorial or verifying the steps with your existing system may solve your problem.

Puppet MySQL disable root user login without password

I have been over several threads for issues related to puppetlabs-mysql module in last few hours to get a workaround on my issue.
MySQL installation on my agent node with this module is enabling the root user login without any password. But I see the credentials has been set for the root user, and can login using those credentials also.
What difference should I make in my manifest to disable MySQL password-less root login?
Manifest applied,
class { '::mysql::server':
root_password => 'rootpassword',
override_options => { 'mysqld' => { 'max_connections' => '1024' } }
}
I had the same problem and for me, creating a simple ~root/.my.cnf file fixed the problem. Add the following to the root .my.cnf file:
[client]
password="mysql root user password here"
This allows puppet to connect to mysql again. Puppet will then overwrite this file with its own generated configuration.
What you are referring to as passwordless login is most probably .my.cnf saving the password in an optionfile.
So by setting $mysql::server::create_root_my_cnf to false you would disable this.
class { '::mysql::server':
root_password => 'rootpassword',
create_root_my_cnf => false,
override_options => { 'mysqld' => { 'max_connections' => '1024' } }
}

How to create mysql database on existing DB server in puppet class

My puppet agent machine has already installed mysql server. I want to create database on that server. My puppet class is as follows
class dbtest {
mysql::db { 'mydb':
user => 'root',
password => 'malintha',
host => 'localhost',
}
This gives me error Error: Failed to apply catalog: Could not find dependency Class[Mysql::Server] for Mysql_database[mydb]
How can I sortout this issue ? How can I import mysql server dependancy class
I could fix this issue addingclass { '::mysql::server':} to my class
class dbtest {
class { '::mysql::server':}
mysql::db { 'mydb1':
user => 'root',
password => 'malintha',
host => 'localhost',
sql => '/tmp/source.sql',
require => File['/tmp/source.sql'],
}

Site Resilient MediaWiki Install

I am trying to set up a MediaWiki site at work. I want it to be resilient across multiple sites, so what I am planning is 1 master database to take writes, and then a local slave database for each web install. This will mean I will have:
Server 1: MySQL Master
Server 2: MySQL Slave, Apache + MW
Server 3: MySQL Slave, Apache + MW
...
Server N: MySQL Slave, Apache + MW
What I want to happen, is if a site "goes dark", they will still have a local copy of the MW running in Read-Only mode, until it is able to contact the Master MySQL server again. I have set up the below configuration file on 1 of the slave hosts, which works fine. However, once I turn off the Master MySQL server to simulate a loss of connection, MW comes up with a DB error instead of just becoming Read-Only.
$wgDBservers = array(
array('host' => "10.10.10.10",
'dbname' => "db",
'user' => "####",
'password' => "####",
'type' => "mysql",
'flags' => DBO_DEFAULT,
'load' => 0),
array('host' => "localhost",
'dbname' => "db",
'user' => "####",
'password' => "####",
'type' => "mysql",
'flags' => DBO_DEFAULT,
'load' => 1)
);
Have I missed something from the configuration and this is something that I am able to do, or does it not work because it is not intended to work that way? Any help is appreciated.
This is not in any way "best practice" but in lieu of any other answer, I am posting it for anyone needing a quick work-around.
It is a PHP script that sits directly in the LocalSettings.php file and checks the status of the Master Database. The "proper" way to do it would probably be with an extension, but I don't have time to learn how to make one. You need to ensure that the user account that you are using to connect to MySQL, has Read-Only access to the slave database.
$wgDBSite_Master = array('host' => "<master ip>", 'dbname' => "<database>", 'user' => "<user>", 'password' => "<pass>", 'type' => "mysql", 'flags' => DBO_DEFAULT, 'load' => 0);
$Site_MasterTest = mysql_connect($wgDBSite_Master['host'], $wgDBSite_Master['user'], $wgDBSite_Master['password']);
if (!$Site_MasterTest) {
$wgReadOnly = 'Wiki failed to contact the Master server. Your site is currently running Dark. No edits will be saved.<br>Last Error: '.mysql_error();
$wgSiteNotice = $wgReadOnly;
$wgDBservers = array();
}
else {
$wgSiteNotice = 'Everything running normally, Captain';
$wgDBservers = array( $wgDBSite_Master );
}
$wgDBservers[] = array('host' => "localhost", 'dbname' => "<database>", 'user' => '<user>', 'password' => "<pass>", 'type' => "mysql", 'flags' => DBO_DEFAULT, 'load' => 1);

Import a dump.sql into mysql database with puppetlabs-mysql

Using puppet as a Vagrant provider and the puppetlabs-mysql module (2.2.3) I'm not able to import a sql dump on a db with root user and no password.
This is the puppet code I'm using:
class { '::mysql::server': }
mysql::db { 'foo':
user => 'root',
password => '',
host => 'localhost',
sql => '/vagrant/dump.sql',
}
This is the box:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 12.04.3 LTS
Release: 12.04
Codename: precise
$ puppet --version
3.3.2
This is the error:
Error: Could not prefetch mysql_grant provider 'mysql': Execution of '/usr/bin/mysql -NBe SELECT CONCAT(User, '#',Host) AS User FROM mysql.user' returned 1: ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Don't use root user for creating the database, the mysql::db type is not meant for this. Instead, specify a new user/password, you can always use root for accessing the database later:
class { '::mysql::server': }
mysql::db { 'foo':
user => 'foo',
password => 'bar',
host => 'localhost',
sql => '/vagrant/dump.sql',
}