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

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'],
}

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' } }
}

Provision Vagrant machine with mysql Puppet module

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?

Laravel failing to connect to DB after 4.2 upgrade

I just upgraded my Laravel install from 4.1.(something) to 4.2.7 using the steps recommended here: http://laravel.com/docs/upgrade
Now I'm getting this error on every page:
PDOException (2002)
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '' (111)
MySQL is not running locally, but it's not supposed to be. I don't have any configuration for connecting to local MySQL, my development SQL server is remote. Why is it trying to connect to local?
Is there some config change that isn't mentioned in the upgrade guide? Everything was peachy in 4.1.
From my app/config/database.php file:
'default' => 'mysql',
...
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'myrealdb.us-east-1.rds.amazonaws.com',
'database' => 'myrealdbname',
'username' => 'myrealuser',
'password' => 'myrealpass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'database_port' => '3306',
'unix_socket' => '',
),
...
);
I tried php artisan clear-compiled and php artisan dump-autoload just in case. No effect.
Edit: I submitted a fix for this that has been merged into the 4.2 branch. You shouldn't have to worry about this error anymore.
I got it! This appears to be a change in the way Laravel uses the database configuration, I hope this answer helps others.
The short version is: if your connection is configured like mine (in the question), delete the unix_socket entry from the array.
Previously, I always copied and edited the default entries in the connections array, leaving in the unix_socket parameter as empty. Apparently now there's a check that assumes if unix_socket is present, it should use a socket DSN string. The empty string in my config passed the check. You can see how this happens in /vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php. The functions getDsn, getSocketDsn, and getHostDsn tell the story.
Pasted because this will eventually change:
protected function getDsn(array $config)
{
return isset($config['unix_socket']) ? $this->getSocketDsn($config) : $this->getHostDsn($config);
}
...
protected function getSocketDsn(array $config)
{
extract($config);
return "mysql:unix_socket={$config['unix_socket']};dbname={$database}";
}
...
protected function getHostDsn(array $config)
{
extract($config);
return isset($config['port'])
? "mysql:host={$host};port={$port};dbname={$database}"
: "mysql:host={$host};dbname={$database}";
}

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',
}