Skip to content

Add instructions on manually fixing MySQL Charset problems#25938

Merged
potiuk merged 1 commit into
apache:mainfrom
potiuk:add-manual-charset-collation-fixing-instructions-for-mysql
Aug 25, 2022
Merged

Add instructions on manually fixing MySQL Charset problems#25938
potiuk merged 1 commit into
apache:mainfrom
potiuk:add-manual-charset-collation-fixing-instructions-for-mysql

Conversation

@potiuk

@potiuk potiuk commented Aug 24, 2022

Copy link
Copy Markdown
Member

Depending on the way how and when you MySQL DB was created, it might
have problems with the right CHARACTER SET and COLLATION used.

This PR attempts to describe a process that users can follow to
fix such a problem.

Also a chapter was added to recommend taking a backup before
the migration.

Based on discussions and user input from #25866, #24526

Closes: #24526


^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

@potiuk
potiuk requested a review from kaxil as a code owner August 24, 2022 14:27
@potiuk
potiuk force-pushed the add-manual-charset-collation-fixing-instructions-for-mysql branch from 5b367c5 to 672df33 Compare August 24, 2022 14:28
Comment thread docs/apache-airflow/howto/set-up-database.rst Outdated
@potiuk
potiuk force-pushed the add-manual-charset-collation-fixing-instructions-for-mysql branch 4 times, most recently from 24be4ec to fbe3d24 Compare August 24, 2022 17:03
@potiuk
potiuk requested a review from mik-laj as a code owner August 24, 2022 17:03
Depending on the way how and when you MySQL DB was created, it might
have problems with the right CHARACTER SET and COLLATION used.

This PR attempts to describe a process that users can follow to
fix such a problem.

Also a chapter was added to recommend taking a backup before
the migration.

Based on discussions and user input from apache#25866, apache#24526

Closes: apache#24526
@potiuk
potiuk force-pushed the add-manual-charset-collation-fixing-instructions-for-mysql branch from fbe3d24 to 616bf80 Compare August 25, 2022 12:21

@eladkal eladkal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment on lines +74 to +80
Wrong Encoding in MySQL database
................................

If you are using old Airflow 1.10 as a database created initially either manually or with previous version of MySQL,
depending on the original character set of your database, you might have problems with migrating to a newer
version of Airflow and your migration might fail with strange errors ("key size too big", "missing indexes" etc).
The next chapter describes how to fix the problem manually.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one might even be an item in https://airflow.apache.org/docs/apache-airflow/stable/upgrading-from-1-10/index.html and we can link from here to there.
this feels more like a step users were suppose to do when they migrated from 1 to 2?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not really something that is 1.10 specific. The way we treated encoding for IDs changed over time - at some point of time it was recommended, and at some point of time it turned into automated/mandatory and the way it works is that I think depends more on when you do certain steps rather than Airflow 1.10 vs Airflow 2.

It's hard to wrap your head around the possible number of scenarios users might have here especialy when you add the fact that people could have (and did) migrates from MySQL 5.7 to MySQL 8 somwhere bettween 1.10 and 2.4.0 which basicallly could have changed the default encoding (from latin1/swedish to utf8) or they could have a completely different encoding set in general.

Then - depending which version you were at when you did the migration, you could end up with pretty much random set of intermixed encodings in your columns. That's why I ended up with "if you have a problem - you need to end up in this state and here are useful hints that can bring you to the right state" rather than trying to fix it automatically. Also see the answer I gave to @itayB in #25866 (reply in thread) why we do not try to automate the fix :)

What we have currently is that encoding is set to uf8mb3 automatically for those ID fields if you do not override it. So once you fix it, it should not happen in the future.

Small rant though... With MySQL 9 or 10 this might return and hit us back again. But then we should likely only enable Mysql 9, 10 (if we decide to) when we test the migration scenarios. The problem with successor of MySQL 8 is that utf8 (which is not default encoding - utf8mb4 is) is an alias to ut8mb3. But documentation of MySQL mentions that in the future versions the utf8 alias will point to utf8mb4 and .... utf8mb3 will be gone. Officially utf8mb3 is deprecated now. I have no idea how we are going to handle this, because this opens up a whole host of various scenarios again.

But I decided to not to worry about this. I sincerely think we should drop MySQL at some point of time, and maybe lack of support for whatever new version of MySQL comes (if it comes) might be a good way to handle dropping MySQL altogether. I even think that instead of supporting MySQL 9 (or whatever) we should develop a tool that will help people to migrate to Postgres from MySQL.

Let's see how much less of MySQL we will have in the next survey.

@itayB itayB Aug 25, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@potiuk until a migration tool is provided 😄, do you have any recommendations for existing tools for migration? I am currently trying pgloader and I wonder if that's a good choice (I want to migrate from MySql 8 to Postgres 13).

@potiuk potiuk Aug 25, 2022

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good choice (of Postgres). No I have not used any myself but I saw people reporting successes using simple export/import scripts. Actually migrating out of MySQL to postgres is such a popular thing that there are PLENTY tools out there (and actually I would love to hear more concrete success stories from people like you :).

I personally think that the best approach is to create the database using Airlfow db cli and only migrate the data via export/import through .csv files (after earlier cleanup of historcal data to decrease the size of the data to migrate and speed it up). Migrating structure is prone to errors but the data in both databases should be exactly the same.

Of course assuming the export from MySQL will use the right encoding - which is not at all given. Unfortunately MySQL complicates encodings in so many strange ways that it's very likely this is not gonna work first time. For example when you use mysql client to connect to mysql server, the default encoding used by the client is NOT the server encoding but your machine encoding - so you can get weird encoding issues if encoding on your "terminal' machine is different than the one in the server (now you might slowly understand my hatred for encoding approach mess in MySQL).

Airflow has suprisingly little number of tables, so this can be done entirely by writing hand-written scripts, there is no need to use specialized tools for that. The only thing that you have to remember is to do the import in the right sequence due to foreign keys and likely speed up the import by temporarily disabling the foreign keys while importing and re-enabling them after.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @potiuk ! I will try to migrate as you suggested and I will share the results ;)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to approve blog post "How I migrated our Airflow from MySQL to PostgresSQL and you can too" in our Medium publication :D

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In progress (it will take me a few days) - stay tuned ;)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tuning in ...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@potiuk
potiuk merged commit e259262 into apache:main Aug 25, 2022
@potiuk
potiuk deleted the add-manual-charset-collation-fixing-instructions-for-mysql branch August 25, 2022 14:15
@eladkal eladkal added this to the Airflow 2.3.5 milestone Aug 26, 2022
@eladkal eladkal added the type:doc-only Changelog: Doc Only label Aug 26, 2022
@ashb ashb modified the milestones: Airflow 2.3.5, Airflow 2.4.0 Sep 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

upgrading from 2.2.3 or 2.2.5 to 2.3.2 fails on migration-job

4 participants