I tried the following steps to upgrade CHARSET to utf8mb4 and to connect it to the existing backend server.
ALTER TABLE users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
update the DB connection parameters.
"user:pass@tcp(server:3306)/mydbcharset=utf8mb4&collation=utf8mb4_unicode_ci"
This is the users table schema before migration.
CREATE TABLE IF NOT EXISTS `users` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL DEFAULT '',
`password` VARCHAR(255) NOT NULL DEFAULT '',
`country_code` CHAR(4) NOT NULL DEFAULT '',
`industry` VARCHAR(255) NOT NULL DEFAULT '',
`company_size` VARCHAR(255) NOT NULL DEFAULT '',
`company_name` VARCHAR(255) NOT NULL DEFAULT '',
`last_name` VARCHAR(255) NOT NULL DEFAULT '',
`first_name` VARCHAR(255) NOT NULL DEFAULT '',
`newsletter` TINYINT(1) NOT NULL DEFAULT 0,
`company_website` VARCHAR(255) NOT NULL DEFAULT '',
`job_title` VARCHAR(255) NOT NULL DEFAULT '',
`profile_picture` VARCHAR(255) NOT NULL DEFAULT '',
`verified` TINYINT(1) NOT NULL DEFAULT 0,
`p_updated_time` INT NOT NULL DEFAULT 0,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY (`email`)
)ENGINE = InnoDB DEFAULT CHARSET=UTF8;
Everything works fine so far.
My worry is that some of the VARCHAR columns have character constrain as 255. In this article https://mathiasbynens.be/notes/mysql-utf8mb4 and the accepted answer here How to easily convert utf8 tables to utf8mb4 in MySQL 5.5 says updating varchar(255) to 191 is needed. However I did not encounter issues without doing that, existing DB has around 100 user records and a dry run of the above migration worked perfectly.
Question are
- Is it mandatory to check and update column lengths when moving from utf8 to utf8mb4, if not what kind of issues could arise?
- Is it also mandatory to change the CHARSET of the database? Why not change only the charset of the table?
--UPDATE--
For someone who runs into foreign key violations can use the below
SET FOREIGN_KEY_CHECKS=0;
-- Insert your other SQL Queries here...
ALTER TABLE tble_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
SET FOREIGN_KEY_CHECKS=1;