Skip to content

Instantly share code, notes, and snippets.

@bazo
Created April 7, 2017 13:15
Show Gist options
  • Save bazo/3a0f3393d936d3edfb10b51798bb5387 to your computer and use it in GitHub Desktop.
Save bazo/3a0f3393d936d3edfb10b51798bb5387 to your computer and use it in GitHub Desktop.
convert mysql db to utf8mb4
delimiter //
DROP PROCEDURE IF EXISTS convert_database_to_utf8mb4 //
CREATE PROCEDURE convert_database_to_utf8mb4()
BEGIN
DECLARE table_name VARCHAR(255);
DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR
SELECT t.table_name FROM information_schema.tables t WHERE t.table_schema = DATABASE() AND t.table_type='BASE TABLE';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
tables_loop: LOOP
FETCH cur INTO table_name;
IF done THEN
LEAVE tables_loop;
END IF;
SET @sql = CONCAT("ALTER TABLE ", table_name, " CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
PREPARE stmt FROM @sql;
EXECUTE stmt;
DROP PREPARE stmt;
END LOOP;
CLOSE cur;
END //
delimiter ;
call convert_database_to_utf8mb4();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment