disabling ssl with mysql2 gem - mysql

I am using the mysql2 gem in a Sinatra app, and when I try to connect, I get the error:
SSL connection error: socket layer receive error
If I use the mysql command-line client, I get the same error, unless I use the --skip-ssl option. This is the command that works with the --skip-ssl arg, and fails without it with the above error:
mysql --host=localhost --user=root --password=root --port=3306 -p --protocol=tcp --skip-ssl
Is there similar 'skip-ssl' option I can add to my establish_connection() method
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "127.0.0.1",
:username => "root",
:password => "root",
:database => "db",
:port => 3306,
:protocol => "tcp"
)
to disable ssl in the gem?

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)

connect mysql with ruby without rails gives error in ruby version 2

I have included given code in my db.rb file
require "mysql2" # if needed
#db_host = "localhost"
#db_user = "root"
#db_pass = "root"
#db_name = "farji"
client = Mysql::Client.new(:host => #db_host, :username => #db_user, :password => #db_pass, :database => #db_name)
#cdr_result = client.query("SELECT * from employee')
and in terminal i have install
sudo apt-get install libmysqlclient15-dev
gem install mysql2
and run ruby db.rb it gives me error db.rb:8:in `': uninitialized constant Mysql (NameError)
my ruby version is ruby-2.1.5
Please guide me how to solve this.

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?

Ruby, connect to sql server

I'm trying to connect to a SQL server in a domain, but I don't know why I have an error.
My environnement is Ubuntu 14.04, and I use mysql2.
I think my problem is :server="myServer" but I have to specify what server.
My request is :
db = Mysql2::Client.new(:host => "xxx.xxx.xxx.xxx",:server="myServer" ,:username => "myname", :password => "SECRET", :port => 3306, :database => "test" )
which gives me this error: Mysql2::error (111)
This request is working:
db = Mysql2::Client.new(:host => "192.168.1.1", :username => "root", :password => "SECRET", :port => 3306, :database => "test" )
Take a look at this:
Can't connect to MySQL server error 111
Probably you are facing the same issue.