MySQL keeps getting same error "Packets out of order" - mysql

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.

Related

Can't connect to database via Ruby and MySQL2

When I try to connect to database via Ruby I'm getting this error :
conn.rb:16:in `<main>': undefined method `query=' for #<Mysql2::Client:0x2ee5190> (NoMethodError)
Did you mean? query
_query
My code is :
require 'mysql2'
connection = Mysql2::Client.new(:host => "localhost", :username => "root",:password => "",:database => "ruby")
result = connection.query = ("INSERT INTO datacheck(#{info.keys}) VALUES #{info.values}")
Seems that everything is working until this line
result = connection.query = ("INSERT INTO datacheck(#{info.keys}) VALUES #{info.values}")
I'm looking for someone who can help.
The error message is pretty clear: it does not exist a query= method, but query does. Try
result = connection.query("INSERT INTO datacheck(#{info.keys}) VALUES #{info.values}")

Ruby-mysql troubles - Getting error while trying to use mysql module for Ruby

I'm using Ruby v2.0 AND i'm using the Ruby-mysql v2.9.14 connector from here: https://rubygems.org/gems/ruby-mysql
I'm getting a number of errors while running this code:
require 'mysql'
begin
connection = Mysql.new 'localhost', 'root', 'root'
connection.list_dbs.each do |db|
puts db
end
rescue Mysql::Error => e
puts e.errno
puts e.error
ensure
connection.close if connection
end
ERROR:
No such file or directory - "/tmp/mysql.sock" (Errno::ENOENT)
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:150:in `new'
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:150:in `block in initialize'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql/protocol.rb:147:in `initialize'
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:115:in `new'
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:115:in `connect'
from /Library/Ruby/Gems/2.0.0/gems/ruby-mysql-2.9.14/lib/mysql.rb:50:in `new'
from /Users/Joakim/Google Drive/Skole/IT/Ruby/Modul 6/ruby-mysql.rb:4:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
My teacher tells me that it's the 'driver' crashing, but I'm not sure - is there anything I can do about this?
The driver tried to connect via socket file /tmp/mysql.sock, which is not available in your system (You can check for the file). The driver assumed you wanted to connect via socket file because you specified localhost.
The is what the driver does:
#host_info = (host.nil? || host == "localhost") ? 'Localhost via UNIX socket' : "#{host} via TCP/IP"
Try changing localhost to 127.0.0.1.
EDITED
It seems that you use a custom port for mysql, 8889. So you need to specify that when connecting to the database:
connection = Mysql.new 'localhost', 'root', 'root', nil, 8889
Here is the snippet for the method definition:
# Connect to mysqld.
# #param [String / nil] host hostname mysqld running
# #param [String / nil] user username to connect to mysqld
# #param [String / nil] passwd password to connect to mysqld
# #param [String / nil] db initial database name
# #param [Integer / nil] port port number (used if host is not 'localhost' or nil)
# #param [String / nil] socket socket file name (used if host is 'localhost' or nil)
# #param [Integer / nil] flag connection flag. Mysql::CLIENT_* ORed
# #return self
def connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, socket=nil, flag=0)

Ruby and mysql2: Looping Until Connection is Error Free

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.

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.

Retrieving multiple datasets using the Sequel gem

I'm using the Sequel gem in the most basic manner possible - connecting to a local database and extracting the results.
The one catch is that my code relies on me being able to execute multiple query statements at once (splitting them up is not an option).
I am
using the mysql2 adapter
using the MULTIPLE_STATEMENTS flag to execute multiple statements
Below is my script.
require 'sequel'
conn = Sequel.connect(:adapter => 'mysql2', :database=>'my_test_db', :user => 'some_user', :password => 'xxxx', :host => 'localhost', :flags => ::Mysql2::Client::MULTI_STATEMENTS)
# res will be a Sequel::Mysql2::Dataset object
res = conn["select * from table_1; select * from table_2;"]
I can easily enough get the results of the first query (selecting from table_1) by simply doing
res.all OR res.each{ |r| puts r }
My problem is, how do I get the next set of results (selecting from table_2) ? Is there some way to store/cache the existing result and move on to the next dataset?
Attempting to do run res.all again results in an error
Sequel::DatabaseDisconnectError: Mysql2::Error: Commands out of sync; you can't run this command now
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/mysql2.rb:77:in `query'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/mysql2.rb:77:in `block in _execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/database/logging.rb:33:in `log_yield'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/mysql2.rb:77:in `_execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/shared/mysql_prepared_statements.rb:34:in `block in execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/database/connecting.rb:229:in `block in synchronize'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/connection_pool/threaded.rb:104:in `hold'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/database/connecting.rb:229:in `synchronize'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/shared/mysql_prepared_statements.rb:34:in `execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/dataset/actions.rb:795:in `execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/mysql2.rb:181:in `execute'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/adapters/mysql2.rb:152:in `fetch_rows'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/dataset/actions.rb:143:in `each'
from /Users/lefthandpisces/.rvm/gems/ruby-1.9.3-p448/gems/sequel-4.6.0/lib/sequel/dataset/actions.rb:46:in `all'
from (irb):14
from /Users/lefthandpisces/.rvm/rubies/ruby-1.9.3-p448/bin/irb:12:in `<main>'
Thanks!
You can't do this with the mysql2 adapter. I believe the mysql adapter supports it, but even then it's a bad idea. Use separate datasets or a single dataset with a UNION.