Changing the MySQL Table Character Code for Tables That Always Have Updates

Asked 1 years ago, Updated 1 years ago, 38 views


MySQL default character code during production has been found to be different from before. I would like to modify the character code of the table created during that time

Most of the tables could be modified, but

In a table with a unique column user_id and frequently updated

ALTER TABLE user_states DEFAULT CHARACTER SET utf8

When I tried to run it
Error Code: 1062.Duplicate entryxxxxxx`for key'index_user_states_on_user_id'
I get the error

Of course, there is only one record because it has unique restrictions.

String comparison failed because it was written while changing the character code
I'm guessing something strange is happening
Is there any way to update it?

Requirements are
There is no problem if this table itself is missing a little bit (for a few seconds), but
It is not allowed to stop or disconnect the writing API side or the entire MySQL from the network

If SQL explicitly locks on a per-table basis,

LOCK;
ALTER TABLE user_states DEFAULT CHARACTER SET utf8;
UNLOCK;

I think I can complete the character code change by suppressing writing for a few seconds like this...
Can't you do that?

Note:

Full Rails Migration Error

 execute("ALTER TABLE user_states DEFAULT CHARACTER SET utf8")
rake stderr —rake aborted!
StandardError: Error has occurred, all later migrations cancelled:
Mysql2::Error: Duplicate entry 'xxxxxxxxxxxx' for key' index_user_states_on_user_id': ALTER TABLE user_states DEFAULT CHARACTER SET utf8

xxxxxxxxxxxxx

because changes every time you run and the user's data has been updated just before. I guessed that the character code change and INSERT overlap and it is strange.
Of course, there are unique constraints, so there is no duplicate data

INSERT INTO user_states (user_id, state) VALUES (xxxxxx, xxx)
ON DUPLICATEKEY UPDATE state = state 

This is what the update query looks like when it comes in large numbers at the same time

mysql

2022-09-29 21:25

1 Answers

Use LOCK TABLES to lock tables in MySQL.
https://dev.mysql.com/doc/refman/5.6/ja/lock-tables.html


2022-09-29 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.