-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix malformed packet error in MySQL statement for connection configuration #41232
Conversation
Actually using Can you just remove diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index e065721eef..defd66e63a 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -770,15 +770,6 @@ def configure_connection
end
sql_mode_assignment = "@@SESSION.sql_mode = #{sql_mode}, " if sql_mode
- # NAMES does not have an equals sign, see
- # https://dev.mysql.com/doc/refman/en/set-names.html
- # (trailing comma because variable_assignments will always have content)
- if @config[:encoding]
- encoding = +"NAMES #{@config[:encoding]}"
- encoding << " COLLATE #{@config[:collation]}" if @config[:collation]
- encoding << ", "
- end
-
# Gather up all of the SET variables...
variable_assignments = variables.map do |k, v|
if defaults.include?(v)
@@ -789,8 +780,12 @@ def configure_connection
# or else nil; compact to clear nils out
end.compact.join(", ")
+ if @config[:collation]
+ variable_assignments << ", @@collation_connection = #{@config[:collation]}"
+ end
+
# ...and send them all in one query
- execute("SET #{encoding} #{sql_mode_assignment} #{variable_assignments}", "SCHEMA")
+ execute("SET #{sql_mode_assignment} #{variable_assignments}", "SCHEMA")
end
def column_definitions(table_name) # :nodoc: See also #34347 (comment). |
@kamipo thanks! I've added a commit that addresses your feedback. I am assigning the collation value (if present) to
For future reference, the character set is taken from the |
Looks good to me. Can you squash commits into one? https://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#squashing-commits |
Fixes malformed packet error that occurred with MariaDB client connecting to RDS Aurora (MySQL 5.7) database.
874cfbb
to
1d09067
Compare
@kamipo done |
Fix malformed packet error in MySQL statement for connection configuration
Looking this patch again I think it is not right. It is only setting |
Summary
After upgrading our AWS RDS MySQL database from version
5.6.mysql_aurora.1.22.2
to version5.7.mysql_aurora.2.09.1
I got the following ActiveRecord::StatementInvalid error while starting Rails:This error is specific to MySQL client that is used. When using the client from within our Alpine (v3.12) container it failed, client version:
When using an older client the query worked, version
10.1.41-r0
(part of Alpine v3.7).When using an older client outside of the container, the query worked as well:
The error did not occur when I removed one or more of the session variables. For example, the following queries worked:
Digging further I found the following MySQL documentation re: the syntax for
SET variables
andSET NAMES
statements:According to this documentation, the
SET NAMES ..., variable1 = x, variable2 = y
syntax that is used by Rails is not correct.To verify, I ran the SET variables statement on its own and added extra variables to rule out an issue with the length of the statement. This worked fine:
I suspected that the newer MariaDB client (v10.4.15) that is present in our Alpine container somehow could not process or send the query from Rails correctly. I adjusted the code in
abstract_mysql_adapter.rb#configure_connection
to use two separate calls instead of one.Using that code, the error no longer happened when starting Rails.