Ruby 'mysql' gem segmentation fault - mysql

I'm using Ruby 1.9.3 and I'm experiencing this strange problem with the 'mysql' Ruby gem going on segmentation fault exactly after the execution of 20 queries.
This is the code which executes the query:
def load
dbh = Mysql::new($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME)
begin
res = dbh.query("SELECT word, type FROM words WHERE word = '#{dbh.escape_string(word)}';")
rescue Mysql::Error => e
puts "Error occurred during SQL query"
end
res.each do |row|
#word = row[0]
#type = row[1]
end
dbh.close if dbh
end
This code gets called everytime the user enters a word, for the first 20 words the query works right and goes on segfault on the 21st no matter what I put in there.
I'm on Windows, using Ruby 1.9.3p0 and ruby 'mysql' gem version 2.8.1, with MySQL 5.5.16. I've already checked that the libmysql.dll file I copied into the Ruby bin folder is the same version of the MySQL I have installed.
Do you have any other hint about what could be going wrong?

Here's the solution for anyone who comes across this in the future:
It seems the reason is that this version of mysql gem does not work
with MySQL 5.1 lib. [...] Download MySQL 5.0 noinstall
version mysql-noinstall-5.0.89-win32.zip from
http://dev.mysql.com/downloads/mysql/5.0.html. Extract libmysql.dll
and copy it to C:\Ruby\bin. Then, the problem is solved. I am still
running MySQL 5.1. But Ruby uses this MySQL 5.0 version dll.
Source: http://fuyun.org/2010/01/ruby-mysql-adapter-on-windows/

Related

Unknown column 'tokens.update_on' when importing mysql dump to new Redmine instance

I'm in the process of upgrading an installation of Redmine from 3.0.3 to 3.3.3.
The process I always follow for this is to install a fresh Redmine on a new machine, import and sqldump from the current one, then copy the important stuff (files/config.yml/database.yml, plugins) over and run all the necessary steps. This has generally worked well in the past.
At the moment, after importing the sqldump, Redmine isn't starting and I'm getting an error I'm not able to figure out.
The mysql import appears to work:
mysql -u 'user' -p'mypassword' redmine < /home/redmine20170608.sql
Then I do the usual steps which all run with no errors:
bundle exec rake redmine:plugins:migrate RAILS_ENV=production
bundle exec rake db:migrate RAILS_ENV=production
bundle exec rake tmp:sessions:clear
bundle exec rake tmp:cache:clear
sudo service httpd restart
When I navigate to myredmine.com I get the "Internal Error" message. Check the logs and the out put is:
ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'tokens.updated_on' in 'field list': UPDATE `tokens` SET `tokens`.`updated_on` = '2017-06-09 07:10:56.515511' WHERE `tokens`.`user_id` = 1 AND `tokens`.`value` = '5a229e24fe73e8a43768c46af2275a8b4a60c9b3' AND `tokens`.`action` = 'session'):
app/models/user.rb:425:in `verify_session_token'
app/controllers/application_controller.rb:77:in `session_expired?'
app/controllers/application_controller.rb:67:in `session_expiration'
Migrating to CreateRolesManagedRoles (20150528092912)
Started GET "/" for 72.155.92.149 at 2017-06-09 07:16:14 +0000
Processing by WelcomeController#index as HTML
Completed 500 Internal Server Error in 25ms (ActiveRecord: 1.8ms)
ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'tokens.updated_on' in 'field list': UPDATE `tokens` SET `tokens`.`updated_on` = '2017-06-09 07:16:14.896744' WHERE `tokens`.`user_id` = 1 AND `tokens`.`value` = '5a229e24fe73e8a43768c46af2275a8b4a60c9b3' AND `tokens`.`action` = 'session'):
app/models/user.rb:425:in `verify_session_token'
app/controllers/application_controller.rb:77:in `session_expired?'
app/controllers/application_controller.rb:67:in `session_expiration'
This is the code from line 425 of that file:
scope.update_all(:updated_on => Time.now) == 1
Which is inside this section:
# Returns true if token is a valid session token for the user whose id is user_id
def self.verify_session_token(user_id, token)
return false if user_id.blank? || token.blank?
scope = Token.where(:user_id => user_id, :value => token.to_s, :action => 'session')
if Setting.session_lifetime?
scope = scope.where("created_on > ?", Setting.session_lifetime.to_i.minutes.ago)
end
if Setting.session_timeout?
scope = scope.where("updated_on > ?", Setting.session_timeout.to_i.minutes.ago)
end
scope.update_all(:updated_on => Time.now) == 1
end
I usually find the error output for these to be relatively self explanatory but I don't know how to interpret this one.
I've deleted all of the plugins to make sure its not a compatibility issue and still getting the same problem.
The current Redmine is 3.0.3, running on Ruby 1.9.3-p551, Rails 4.2.1 and AWS Linux AMI 2010.03 (which I am advised to move away from).
The new Redmine is 3.3.3, running on Ruby 2.2.5-p319, Rails 4.2.7.1 and CentOS 7.
Any help greatly appreciated.
As discussed in the comments the error is
ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'tokens.updated_on'
There's no column called updated_on in Token model and you are trying to update it on line 425
scope.update_all(:updated_on => Time.now) == 1
You need to add migration for that column.
run following command in your terminal from app's root folder,
rails g migration AddUpdatedOnToToken updated_on:datetime
rake db:migrate
The answer was to manually add the required column. The way described in the first answer didn't work - as noted I kept getting permission denied. This is weird because there are only two mysql users and both have access to that database.
So the way to fix this was to log into mysql as the Redmine user and run these commands:
USE mydatabase;
ALTER TABLE tokens ADD updated_on VARCHAR(60);
And the issue was resolved - I was able to continue and access Redmine with no issues.

Broken MySql driver for PySide2 on Windows?

I've been forced to try to port to PySide2 from PySide because I'm developing code (on Windows 7) used inside Maya and Maya 2017 has moved to PySide2.
PySide2 ships with Maya but only comes with the driver to talk to sqlite DBs. So I need to tell Qt about a new DIR that contains the MySql driver, like this:
from PySide2 import QtWidgets
QtWidgets.QApplication.addLibraryPath("C:\temp\plugins")
This DIR contains a sub sqldrivers DIR which contains the driver. This works- when I call QtSql.QSqlDatabase.drivers() the new one is there so I've got the right driver and it's being picked up.
However when I create a new DB and call open:
db = QtSql.QSqlDatabase.addDatabase("QMYSQL3")
db.setDatabaseName("my_db")
db.setHostName("host")
db.setUserName("user")
db.setPassword("password")
db.open()
the open command returns False and the DB is not accessible. I call:
db.lastError()
and get:
# Result: <PySide2.QtSql.QSqlError("", "Driver not loaded", "Driver not loaded") at 0x0000000075410C08> #
NB: we successfully did the exact same thing with the previous PySide version.
Other threads mention similar things and it turned out to be missing the actual libmysql.dll, which is the actual MySql DLL and can result in the same error if missing. However I definitely have this on my system and it's definitely in the PATH. It's actually the same file that is being successfully picked up through PySide. But the PySide2 driver just wont open a connection to the DB.
I suspect maybe a dodgy driver but wondered if anyone has any experience with this or any advice?
I had the same error, but with postgres.
I solved by placing the libpq.dll connector library next to my script.py.
Then for mysql it would be libmysql.dll.
In my case I had to build the libpq.dll from the source of postgres 9.6
It seems that with PySide2 it is not necessary to use:
QtWidgets.QApplication.addLibraryPath ("C: \ path_to\ plugins")
using:
print (PySide2.QtCore.QLibraryInfo.location (PySide2.QtCore.QLibraryInfo.PluginsPath)), I could see that the path was correct.
a greeting

Ruby on Rails 4 select schema_migrations Error

Guy today i was open old Ruby project
after copy the files to my Sites folder then run this commands
bundle
rake db:create
rake db:migrate
rails s
when i was open my project on the browser i found the project gives me errors because
MySQL not work with me also i got this error on the console
ActiveRecord::SchemaMigation Load (68.0ms) select 'schema_migrations'.* from 'schema_migrations'
processing by HomeController#index as HTML
Completed 500 Internal Server Error in 92ms
log writing faild. invalid byte sequence in US-ASCII
so why i got this error and how i can solve that ??
I solved it now
just to solve the US-ASCII issue
i added this line to the first line in my model page
#encoding: utf-8
also i added the lines
if RUBY_VERSION =~ /1.9/
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
end
in my Gemfile
then i got another error told me use protected_attributes
so i added the line
gem 'protected_attributes'
in my Gemfile and then execute:
$ bundle

ruby mysql wrong number of arguments

I have installed mysql v2.8.1 with ruby v 1.9.3p125.
When I try and connect to a mysql database with:
m = Mysql.new('localhost','root','','dbname')
I get the following error message:
in `initialize': wrong number of arguments(4 for 0) (ArgumentError)
I have tested that mysql is installed by checking gem list and also by issuing the command require 'mysql' from the irb prompt which shows "true".
I have also uninstalled and installed mysql a number of times and tried to install using the instructions shown here: http://thinlight.org/2010/05/12/how-to-install-ruby-mysql-on-mac-os-x-10-6-snow-leopard/
Does anyone have any other suggestions to try?
Try using v0.3.11 of the mysql2 gem instead
I found the solution. I added 'export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH"' to my .bash_profile and it worked. – pdoak Jun 12 at 16:49

Multiple MySQL queries with Ruby

I'm having troubles with MySQL queries in Ruby. I use 'mysql' gem.
Configuration is stored in a separate yml file and loaded into #conf variable.
This is my code:
# connect to the database
Mysql::new(#conf['sql_host'], #conf['sql_user'], #conf['sql_password'], #conf['sql_base'])
# it's ok when we're doing this
my.query("SELECT * FROM `my_table`") do |e|
# code
end
# Maybe, I've missed something here...
# really this query will insert value into other table, used SELECT just for testing
# this throws exception: 'query: not connected'
my.query("SELECT * FROM `my_table_2`")
Windows XP
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
mysql (2.8.1, 2.7.3)
MySQL client version: 5.0.51a
Second query throws 'query: not connected'.
First of all, your program looks ok. I am 100% sure you have libmysql.dll version problem.
I can reproduce this with libmysql.dll provided from Mysql installer.
Download this file and replace in c:\ruby\bin\
http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll
and re-run your program without any change.
Related issue and credit to here
I have run into immeasurable problems when attempting to use Ruby's mysql gem on any version of Windows. From what I can tell, it simply doesn't work unless you can figure out how to compile it yourself (which is a royal pain in the ass).
Have you considered using ActiveRecord as your ORM (object relational mapping) layer and doing development with SQLite or some other database on Windows and then running the production environment on Linux with MySQL? This is the solution that I took a while back and it worked out quite well.