Ruby and mysql2: Looping Until Connection is Error Free - mysql

Context: I am writing a simple dynamic query in Ruby using the mysql2 gem.
Attempt:
#!/usr/local/bin/ruby
require "mysql2"
puts "Please enter the title of this Report:"
title = gets.chomp
Mysql2::Client.default_query_options.merge!(:as => :array)
puts "Please enter the host, username, password and database in order:"
hst = gets.chomp
user = gets.chomp
pass = gets.chomp
db = gets.chomp
begin
mysql = Mysql2::Client.new(:host => hst, :username => user, :password => pass, :database => db)
rescue Mysql2::Error => e
puts e.errno
puts e.error
retry
puts "Error: please try again."
puts "Enter the host, username, password and database:"
hst = gets.chomp!
user = gets.chomp!
pass = gets.chomp!
db = gets.chomp!
end
puts "Successfully accessed #{db}!"
Note that:
rescue Mysql2::Error => e
puts e.errno
puts e.error
works with the mysql gem, but:
rescue Mysql2::StandardError => e
puts e.errno
puts e.error
does not work with the mysql2 gem.
Finally, the error in Terminal:
iMac:workspace guy$ ruby File.rb
Please enter the title of this Report:
title
Please enter the host, username, password and database in order:
1.2.3.4
username15
password123
db_one
File.rb:19:in `rescue in <main>': uninitialized constant Mysql2::StandardError (NameError)
Did you mean? StandardError
from File.rb:17:in `<main>'
Edit after answer: leaving :host as :hst gave me the following error:
2002
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

mysql2 gem has defined Mysql2::Error, not `Mysql2::StandardError.
You need to rescue Mysql2::Error
Refer to mysql2 Github source for more info.

Related

Disconnecting from mysql database in rufus scheduler doesnt work (dashing.io)

I am using dashing.io and want to grab some data from a mysql database using rufus scheduler to push them.
I have this code for testing the connection closing in one .rb job file:
require 'rubygems'
require 'mysql2'
SCHEDULER.every '12h', :first_in => 0 do |job|
begin
db = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "rainer-zufall", :port => 2971, :database => "test" )
open_connections = db.query("show status like 'Connections';")
open_connections.each do |row|
puts "connections: #{row}"
end
rescue
ensure
db.close
end
end
The database connection do not close. The connection counter still increases and soon there will be an error stating max_connections reached from the mysql-server.
Can anybody help?

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.

How to connect and accessing mysqldb with ruby programming

#!/usr/bin/ruby -w
require "rubygems"
require "mysql"
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:TESTDB:localhost",
"nyros", "root")
# get server version string and display it
row = dbh.select_one("SELECT VERSION()")
puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
I want to connect and access MySQL database through ruby programming. But i am getting this error when executing the ruby code in my terminal.
Error:
/.rvm/rubies/ruby-1.9.3 p547/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in 'require': cannot load such file -- mysql (LoadError)
from /home/nyros/.rvm/rubies/ruby-1.9.3-p547/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in 'require'
from task.rb:3:in '<main>'
I suggest using mysql2. It is a mysql library for Ruby.
For example:
require 'mysql2'
client = Mysql2::Client.new(:host => HOST, :username => USERNAME, :database => DATABASE)
client.query("select * from tabel_name;")
If you don't want to use mysql2, you also can refer to this link.

MySQL keeps getting same error "Packets out of order"

I have read through the similar questions and nothing seems to fix my issue. The exact error I get is:
/mysql.rb:1019:in `read': Packets out of order: 0<> (RuntimeError)
from C:/Ruby193/lib/ruby/1.9.1/mysql.rb:444:in `read'
from C:/Ruby193/lib/ruby/1.9.1/mysql.rb:110:in `real_connect'
from C:/Ruby193/lib/ruby/1.9.1/mysql.rb:91:in `initialize'
from testmysql.rb:6:in `new'
from testmysql.rb:6:in `<main>'
My code is just a test script to check out how to use this:
#!/usr/bin/ruby
require 'mysql'
begin
con = Mysql.new 'localhost', 'username', 'password'
puts con.get_server_info
rs = con.query 'SELECT VERSION()'
puts rs.fetch_row
rescue Mysql::Error=> e
puts e.errno
puts e.error
ensure
con.close if con
end
I know there has to be something I have overlooked.