Import a dump.sql into mysql database with puppetlabs-mysql - 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',
}

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.

Ruby connect to mysql using login path

I'm trying to create a connection to mysql from ruby script using login path.
I've set the login path in the following way:
mysql_config_editor set --login-path=login-path --host=host-name --user=user --password
Now in my ruby script I have the following line:
require 'mysql2'
$mysql_connection = Mysql2::Client.new(:default_file => '~/.mylogin.cnf',:default_group => 'login-path')
And I get the following error:
error: Found option without preceding group in config file:
/home/user/.mylogin.cnf at line: 3 Fatal error in defaults handling.
Program aborted
When I'm connecting like this, it succeed:
require 'mysql2'
$mysql_connection = Mysql2::Client.new(:host => 'host-name', :username => "user", :password => "password", :database => 'database_name')
What am I doing wrong?
I think the problem is with your 'default_group' setting. I have standard mysql setup using a typical login-path of 'client', eg:
% mysql_config_editor print
[client]
password = *****
and the following connect works with no issue:
#client = Mysql2::Client.new(host: MySQL_Host, username: MySQL_User, default_group: 'client', database: MySQL_Database)

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?

Sequel can't connect to MySQL using login parameters provided by ARGV through .cmd, but works fine from command line

init.rb:
## LOCAL DB
DB = Sequel.connect(
:adapter => 'mysql',
:user => ARGV[1],
:password => ARGV[2],
:host => '127.0.0.1',
:database => ARGV[3]
)
A.cmd:
ruby init.rb A 'root' 'MY_PASSWORD' 'DATABASE_NAME'
When I double click A.cmd, it opens and crashes, spitting out this error:
Mysql::Error: Access denied for user 'root'#'localhost' (using password: YES)
But when I open a command prompt, type A.cmd and run it, the script works fine.
Why?

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