545

How can I convert entire MySQL database character-set to UTF-8 and collation to UTF-8?

8
  • 30
    To later visitors: Note the related questions in the sidebar and use utf8_unicode_ci, not utf8_general_ci.
    – fuxia
    Commented Mar 26, 2013 at 10:08
  • 20
    If you want full UTF-8 support you'll probably also want to use a character set of utf8mb4 rather than utf8 as utf8 only supports the basic multilingual plane as opposed to the full range. It requires MySQL 5.5.3 or above. Commented Dec 12, 2014 at 17:00
  • 7
    I forgot to mention in my comment above, if you switch to utf8mb4 you'll also need to switch collation to utf8mb4_unicode_ci Commented Jan 19, 2015 at 15:13
  • 3
    Even better, collation utf8mb4_unicode_520_ci, or whatever is the latest available version.
    – Rick James
    Commented Aug 19, 2016 at 18:51
  • 2
    Update to update... For MySQL 8.0, this is probably the preferred collation: utf8mb4_0900_ai_ci, which is based on Unicode standard version 9.0.
    – Rick James
    Commented Apr 19, 2017 at 16:05

20 Answers 20

829

Use the ALTER DATABASE and ALTER TABLE commands.

ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Or if you're still on MySQL 5.5.2 or older which didn't support 4-byte UTF-8, use utf8 instead of utf8mb4:

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
12
  • 22
    The CONVERT TO technique assumes that the text was correctly stored in some other charset (eg, latin1), and not mangled (such as UTF-8 bytes crammed into latin1 column without conversion to latin1).
    – Rick James
    Commented Apr 19, 2017 at 16:03
  • 2
    This rebuilds the table making it infeasible on large production systems. If it's certain that only ASCII chars are stored in the latin1 columns, is it possible to change the table charset/collation without rebuilding?
    – Andrew
    Commented Nov 30, 2017 at 15:52
  • 1
    @Andrew Large production systems usually have a mirrored DB for maintenance.
    – BalusC
    Commented Nov 30, 2017 at 16:19
  • 3
    changing charcter set to utf8 changes my datatype from text to mediumtext. Is it expected?
    – Jerry
    Commented Feb 6, 2018 at 11:41
  • 2
    @Jerry Probably, since: "If the column is converted to utf8, each character might require up to three bytes, for a maximum possible length of 3 × 65,535 = 196,605 bytes. That length does not fit in a TEXT column's length bytes, so MySQL converts the data type to MEDIUMTEXT, which is the smallest string type for which the length bytes can record a value of 196,605. Similarly, a VARCHAR column might be converted to MEDIUMTEXT." Changing the Character Set Commented Oct 21, 2019 at 17:06
145
  1. Make a backup!

  2. Then you need to set the default char sets on the database. This does not convert existing tables, it only sets the default for newly created tables.

    ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;
    
  3. Then, you will need to convert the char set on all existing tables and their columns. This assumes that your current data is actually in the current char set. If your columns are set to one char set but your data is really stored in another then you will need to check the MySQL manual on how to handle this.

    ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
    
4
  • 55
    Note: ALTER TABLE tablename CHARACTER SET utf8 only sets the default char set on a table which is used for newly created columns. It does not convert existing columns that already have a char set set.
    – newspire
    Commented May 24, 2011 at 20:15
  • I should have read the backup backup backup first... but my luck was that it was on development environment. so my upvote goes to you! Commented Dec 16, 2014 at 14:28
  • 5
    @DominikAngerer: What broke?
    – andreas
    Commented Sep 13, 2015 at 14:25
  • 24
    Note that utf8_general_ci is no longer recommended best practice. Since MySQL 5.5.3 you should use utf8mb4 rather than utf8. They both refer to the UTF-8 encoding, but the older utf8 had a MySQL-specific limitation preventing use of characters numbered above 0xFFFD.
    – u01jmg3
    Commented Jan 6, 2017 at 17:38
87

On the commandline shell

If you're one the commandline shell, you can do this very quickly. Just fill in "dbname" :D

DB="dbname"
(
    echo 'ALTER DATABASE `'"$DB"'` CHARACTER SET utf8 COLLATE utf8_general_ci;'
    mysql "$DB" -e "SHOW TABLES" --batch --skip-column-names \
    | xargs -I{} echo 'ALTER TABLE `'{}'` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;'
) \
| mysql "$DB"

One-liner for simple copy/paste

DB="dbname"; ( echo 'ALTER DATABASE `'"$DB"'` CHARACTER SET utf8 COLLATE utf8_general_ci;'; mysql "$DB" -e "SHOW TABLES" --batch --skip-column-names | xargs -I{} echo 'ALTER TABLE `'{}'` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;' ) | mysql "$DB"
7
  • 2
    Can you put more detail in this I get ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DB="dbname"
    – steros
    Commented Apr 9, 2014 at 12:41
  • @4485670 You need to run this on the command line shell. If you only have the MySQL client connection available, use the code of sdfor below. Commented May 15, 2014 at 11:36
  • 7
    This code works great, just remember to add -h [hostname] -u [username] -p[password] after mysql as necessary.
    – disperse
    Commented Jun 6, 2014 at 13:29
  • 3
    You'll probably want to disable foreign key checks on a real system: DB="db_name"; ( echo 'ALTER DATABASE '"$DB"'` CHARACTER SET utf8 COLLATE utf8_general_ci;'; mysql --uuser -ppassword -hhost "$DB" -e "SHOW TABLES" --batch --skip-column-names | xargs -I{} echo 'SET foreign_key_checks = 0; ALTER TABLE '{}' CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;' ) | mysql -uuser -ppassword -hhost "$DB"` Commented May 4, 2016 at 17:08
  • 1
    Didn't work for me until I used "show full tables where Table_Type = 'BASE TABLE'" instead of "SHOW TABLES" Commented Apr 12, 2018 at 16:59
81

You can create the sql to update all tables with:

SELECT CONCAT("ALTER TABLE ",TABLE_SCHEMA,".",TABLE_NAME," CHARACTER SET utf8 COLLATE utf8_general_ci;   ",
    "ALTER TABLE ",TABLE_SCHEMA,".",TABLE_NAME," CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;  ") 
    AS alter_sql
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "your_database_name";

Capture the output and run it.

Arnold Daniels' answer above is more elegant.

5
  • why you added two alter table query? one isn't sufficient?
    – Akshay
    Commented Apr 11, 2014 at 8:04
  • 9
    @Akshay, good question. The first alter-table query sets the default for new columns, and the second alter-table query converts the existing columns. Commented Oct 21, 2014 at 11:21
  • 5
    FYI: according to dev.mysql.com/doc/refman/5.5/en/alter-table.html MySQL documentation , the "CONVERT TO CHARACTER SET" version of the ALTER statement does both in one step: "To change the table default character set and all character columns (CHAR, VARCHAR, TEXT) to a new character set ...
    – devGuy
    Commented Mar 10, 2015 at 18:28
  • 2
    I have this error #1054 - Unknown column 'webdb_playground' in 'where clause' but I'm sure my db is the correct one Commented Apr 24, 2018 at 12:13
  • 1
    @YannisDran Your db name may not have been in a string, such that WHERE TABLE_SCHEMA=webdb_playground gave you the unknown column error but WHERE TABLE_SCHEMA="webdb_playground" would have been successful. Something to try in case anyone else runs into that. Commented Oct 21, 2019 at 16:45
18

Before proceeding, ensure that you: Have completed a full database backup!

Step 1: Database Level Changes

  • Identifying the Collation and Character set of your database

    SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM 
    information_schema.SCHEMATA S
    WHERE schema_name = 'your_database_name'
    AND
    (DEFAULT_CHARACTER_SET_NAME != 'utf8'
        OR
     DEFAULT_COLLATION_NAME not like 'utf8%');
    
  • Fixing the collation for the database

    ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    

Step 2: Table Level Changes

  • Identifying Database Tables with the incorrect character set or collation

    SELECT CONCAT(
    'ALTER TABLE ',  table_name, ' CHARACTER SET utf8 COLLATE utf8_general_ci;  ', 
    'ALTER TABLE ',  table_name, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;  ')
    FROM information_schema.TABLES AS T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` AS C
    WHERE C.collation_name = T.table_collation
    AND T.table_schema = 'your_database_name'
    AND
    (C.CHARACTER_SET_NAME != 'utf8'
        OR
     C.COLLATION_NAME not like 'utf8%')
    
  • Adjusting table columns' collation and character set

Capture upper sql output and run it. (like following)

ALTER TABLE rma CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;  
ALTER TABLE rma_history CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_history CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;  
ALTER TABLE rma_products CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_products CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;  
ALTER TABLE rma_report_period CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_report_period CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;  
ALTER TABLE rma_reservation CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_reservation CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;  
ALTER TABLE rma_supplier_return CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_supplier_return CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;  
ALTER TABLE rma_supplier_return_history CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_supplier_return_history CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;  
ALTER TABLE rma_supplier_return_product CHARACTER SET utf8 COLLATE utf8_general_ci;ALTER TABLE rma_supplier_return_product CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; 

refer to: https://confluence.atlassian.com/display/CONFKB/How+to+Fix+the+Collation+and+Character+Set+of+a+MySQL+Database

2
  • 1
    Currently this script uses 'utf8_unicode_ci' for the db but 'utf8_general_ci' for the tables - was that intentional? (I think both should use the same charset) Commented Jul 27, 2016 at 16:36
  • stackoverflow.com/questions/10957238/… have left a fuller answer here
    – V H
    Commented Apr 15, 2017 at 11:52
9

Use HeidiSQL. Its free and a very good db tool.

From tools menu, enter Bulk table editor

Select the complete database or pick tables to convert,

  • tick Change default collation: utf8mb4_general_ci
  • tick Convert to charset: utf8

Execute

This converts complete database from latin to utf8 in just a few seconds.

Works like a charm :)

HeidiSQL connects by default as utf8 so any special characters should now be seen as the character (æ ø å) and not as encoded when inspecting the table data.

The real pitfall when moving from latin to utf8 is to make sure pdo connects with utf8 charset. If not you will get rubbish data inserted to the utf8 table and question marks all over the place on your web page, making you think the table data is not utf8...

4
  • Could you elaborate please? I have exactly this issue - special characters and spaces appear as question marks. I am trying to convert the database in MAMP using PHPMyAdmin. Having developed offline I now discover that my host does not support utf8mb4. I don't have Windows so cannot use HeidiSQL. Is there a way I can achieve this with PHPMyAdmin?
    – RexTheRunt
    Commented Feb 22, 2016 at 15:00
  • like this way. especially you have plenty of table.
    – user4985526
    Commented Jul 7, 2016 at 9:09
  • I get and error when trying to convert the CHARSET: SQL Error (1025): Error on rename... but this is an amazing SQL manager tool!
    – marcolopes
    Commented Aug 4, 2017 at 1:18
  • If anyone needs a good overview and tutorial for heidisql, check out this article.
    – DougB
    Commented Mar 15, 2019 at 20:58
9
DELIMITER $$  

CREATE PROCEDURE `databasename`.`update_char_set`()  

BEGIN  
 DECLARE done INT DEFAULT 0;  
 DECLARE t_sql VARCHAR(256);  
 DECLARE tableName VARCHAR(128);  
 DECLARE lists CURSOR FOR SELECT table_name FROM `information_schema`.`TABLES` WHERE table_schema = 'databasename';  
 DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;  
 OPEN lists;  
 FETCH lists INTO tableName;  
 REPEAT  
    SET @t_sql = CONCAT('ALTER TABLE ', tableName, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci');  
    PREPARE stmt FROM @t_sql;  
    EXECUTE stmt;  
    DEALLOCATE PREPARE stmt;  
 FETCH lists INTO tableName;  
 UNTIL done END REPEAT;  
 CLOSE lists;  
END$$  

DELIMITER ;  

CALL databasename.update_char_set();
1
  • Thanks, this is one of the few answers which actually show how to do it to the whole database (i.e. each table). Worked like a charm.
    – Machisuji
    Commented Feb 15, 2019 at 17:12
6

Inspired by @sdfor comment, here is a bash script that does the job

#!/bin/bash

printf "### Converting MySQL character set ###\n\n"

printf "Enter the encoding you want to set: "
read -r CHARSET

# Get the MySQL username
printf "Enter mysql username: "
read -r USERNAME

# Get the MySQL password
printf "Enter mysql password for user %s:" "$USERNAME"
read -rs PASSWORD

DBLIST=( mydatabase1 mydatabase2 )

printf "\n"


for DB in "${DBLIST[@]}"
do
(
    echo 'ALTER DATABASE `'"$DB"'` CHARACTER SET utf8 COLLATE `'"$CHARSET"'`;'
    mysql "$DB" -u"$USERNAME" -p"$PASSWORD" -e "SHOW TABLES" --batch --skip-column-names \
    | xargs -I{} echo 'ALTER TABLE `'{}'` CONVERT TO CHARACTER SET utf8 COLLATE `'"$CHARSET"'`;'
) \
| mysql "$DB" -u"$USERNAME" -p"$PASSWORD"

echo "$DB database done..."
done

echo "### DONE ###"
exit
5

from utf8 to utf8mb4:

1.show all DATABASE default characterset:

SELECT SCHEMA_NAME 'YOUR_DATABASE_NAME', 
default_character_set_name 'charset', 
DEFAULT_COLLATION_NAME 'collation' 
FROM information_schema.SCHEMATA;

2.show all tables status(character set), focus on column 'collation':

use YOUR_DATABASE_NAME;    
SHOW TABLE STATUS ;

3.generate convert sql: convert database & all tables to utf8mb4,utf8mb4_unicode_ci

USE information_schema;
SELECT CONCAT("ALTER DATABASE `",table_schema,"` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;") AS _sql
FROM `TABLES` WHERE table_schema LIKE "YOUR_DATABASE_NAME" AND TABLE_TYPE='BASE TABLE' GROUP BY table_schema UNION
SELECT CONCAT("ALTER TABLE `",table_schema,"`.`",table_name,"` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") AS _sql  
FROM `TABLES` WHERE table_schema LIKE "YOUR_DATABASE_NAME" AND TABLE_TYPE='BASE TABLE' GROUP BY table_schema, TABLE_NAME 
/*include all columns, commonly don't need this.*/
/*
UNION
SELECT CONCAT("ALTER TABLE `",`COLUMNS`.table_schema,"`.`",`COLUMNS`.table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type,"(",character_maximum_length,") CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci",IF(is_nullable="YES"," NULL"," NOT NULL"),";") AS _sql 
FROM `COLUMNS` INNER JOIN `TABLES` ON `TABLES`.table_name = `COLUMNS`.table_name WHERE `COLUMNS`.table_schema like "YOUR_DATABASE_NAME" and data_type in ('varchar','char') AND TABLE_TYPE='BASE TABLE' UNION
SELECT CONCAT("ALTER TABLE `",`COLUMNS`.table_schema,"`.`",`COLUMNS`.table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type," CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci",IF(is_nullable="YES"," NULL"," NOT NULL"),";") AS _sql 
FROM `COLUMNS` INNER JOIN `TABLES` ON `TABLES`.table_name = `COLUMNS`.table_name WHERE `COLUMNS`.table_schema like "YOUR_DATABASE_NAME" and data_type in ('text','tinytext','mediumtext','longtext') AND TABLE_TYPE='BASE TABLE';
*/

4.run the sql generated.

5.refresh your database.

6.check:

SHOW TABLE STATUS ;
4

In case the data is not in the same character set you might consider this snippet from http://dev.mysql.com/doc/refman/5.0/en/charset-conversion.html

If the column has a nonbinary data type (CHAR, VARCHAR, TEXT), its contents should be encoded in the column character set, not some other character set. If the contents are encoded in a different character set, you can convert the column to use a binary data type first, and then to a nonbinary column with the desired character set.

Here is an example:

 ALTER TABLE t1 CHANGE c1 c1 BLOB;
 ALTER TABLE t1 CHANGE c1 c1 VARCHAR(100) CHARACTER SET utf8;

Make sure to choose the right collation, or you might get unique key conflicts. e.g. Éleanore and Eleanore might be considered the same in some collations.

Aside:

I had a situation where certain characters "broke" in emails even though they were stored as UTF-8 in the database. If you are sending emails using utf8 data, you might want to also convert your emails to send in UTF8.

In PHPMailer, just update this line: public $CharSet = 'utf-8';

4

For databases that have a high number of tables you can use a simple php script to update the charset of the database and all of the tables using the following:

$conn = mysqli_connect($host, $username, $password, $database);

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$alter_database_charset_sql = "ALTER DATABASE ".$database." CHARACTER SET utf8 COLLATE utf8_unicode_ci";
mysqli_query($conn, $alter_database_charset_sql);

$show_tables_result = mysqli_query($conn, "SHOW TABLES");
$tables  = mysqli_fetch_all($show_tables_result);

foreach ($tables as $index => $table) {
  $alter_table_sql = "ALTER TABLE ".$table[0]." CONVERT TO CHARACTER SET utf8  COLLATE utf8_unicode_ci";
  $alter_table_result = mysqli_query($conn, $alter_table_sql);
  echo "<pre>";
  var_dump($alter_table_result);
  echo "</pre>";
}
3
  • Where do we run this script from? Commented Apr 24, 2018 at 12:30
  • 1
    @YannisDran It shouldn't matter, as long as the IP that your executing it from has access to the database. Make sure you take a backup first!
    – Dan Lucas
    Commented May 3, 2018 at 18:29
  • Good, and how do we run it? Do we have to upload it on server and then run enter the path to its position? Commented May 7, 2018 at 14:11
4

The safest way is to modify the columns first to a binary type and then modify it back to it type using the desired charset.

Each column type have its respective binary type, as follows:

  1. CHAR => BINARY
  2. TEXT => BLOB
  3. TINYTEXT => TINYBLOB
  4. MEDIUMTEXT => MEDIUMBLOB
  5. LONGTEXT => LONGBLOB
  6. VARCHAR => VARBINARY

Eg.:

ALTER TABLE [TABLE_SCHEMA].[TABLE_NAME] MODIFY [COLUMN_NAME] VARBINARY;

ALTER TABLE [TABLE_SCHEMA].[TABLE_NAME] MODIFY [COLUMN_NAME] VARCHAR(140) CHARACTER SET utf8mb4;

I tried in several latin1 tables and it kept all the diacritics.

You can extract this query for all columns doing this:

SELECT
CONCAT('ALTER TABLE ', TABLE_SCHEMA,'.', TABLE_NAME,' MODIFY ', COLUMN_NAME,' VARBINARY;'),
CONCAT('ALTER TABLE ', TABLE_SCHEMA,'.', TABLE_NAME,' MODIFY ', COLUMN_NAME,' ', COLUMN_TYPE,' CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;')
FROM information_schema.columns
WHERE TABLE_SCHEMA IN ('[TABLE_SCHEMA]')
AND COLUMN_TYPE LIKE 'varchar%'
AND (COLLATION_NAME IS NOT NULL AND COLLATION_NAME NOT LIKE 'utf%');

After you do this on all your columns then you do it on all tables:

ALTER TABLE [TABLE_SCHEMA].[TABLE_NAME] CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

To generate this query for all your table, use the following query:

SELECT
CONCAT('ALTER TABLE ', TABLE_SCHEMA, '.', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_COLLATION NOT LIKE 'utf8%'
and TABLE_SCHEMA in ('[TABLE_SCHEMA]');

And now that you modified all your columns and tables, do the same on the database:

ALTER DATABASE [DATA_BASE_NAME] CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;
3
mysqldump -uusername -ppassword -c -e --default-character-set=utf8 --single-transaction --skip-set-charset --add-drop-database -B dbname > dump.sql
cp dump.sql dump-fixed.sql
vim dump-fixed.sql


:%s/DEFAULT CHARACTER SET latin1/DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci/
:%s/DEFAULT CHARSET=latin1/DEFAULT CHARSET=utf8/
:wq

mysql -uusername -ppassword < dump-fixed.sql
1

If you cannot get your tables to convert or your table is always set to some non-utf8 character set, but you want utf8, your best bet might be to wipe it out and start over again and explicitly specify:

create database database_name character set utf8;
1

To change the character set encoding to UTF-8 for the database itself, type the following command at the mysql> prompt. Replace DBNAME with the database name:

ALTER DATABASE DBNAME CHARACTER SET utf8 COLLATE utf8_general_ci;
0

The only solution that worked for me: http://docs.moodle.org/23/en/Converting_your_MySQL_database_to_UTF8

Converting a database containing tables

mysqldump -uusername -ppassword -c -e --default-character-set=utf8 --single-transaction --skip-set-charset --add-drop-database -B dbname > dump.sql

cp dump.sql dump-fixed.sql
vim dump-fixed.sql

:%s/DEFAULT CHARACTER SET latin1/DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci/
:%s/DEFAULT CHARSET=latin1/DEFAULT CHARSET=utf8/
:wq

mysql -uusername -ppassword < dump-fixed.sql
0

alter table table_name charset = 'utf8';

This is a simple query i was able to use for my case, you can change the table_name as per your requirement(s).

1
  • Normally an answer should be accompanied by some explanation of what you suggest doing.
    – RiggsFolly
    Commented Mar 16, 2015 at 10:14
0

You can also DB tool Navicat, which does it more easier.

  • Siva.

Right Click Your Database & select DB Properties & Change as you desired in Drop Down

enter image description here

0

Command Line Solution and Exclude Views

I am simply completing @Jasny's answer for others like @Brian and I who have views in our database.

If you have an error like this:

ERROR 1347 (HY000) at line 17: 'dbname.table_name' is not of type 'BASE TABLE'

It's because you probably have views and you need to exclude them. But when trying to exclude them, MySQL returns 2 columns instead of 1.

SHOW FULL TABLES WHERE Table_Type = 'BASE TABLE';
-- table_name1  BASE TABLE
-- table_name2  BASE TABLE

So we have to adapt Jasny's command with awk to extract only the 1st column which contains the table name.

DB="dbname"
(
    echo 'ALTER DATABASE `'"$DB"'` CHARACTER SET utf8 COLLATE utf8_general_ci;'
    mysql "$DB" -e "SHOW FULL TABLES WHERE Table_Type = 'BASE TABLE'" --batch --skip-column-names \
    | awk '{print $1 }' \
    | xargs -I{} echo 'ALTER TABLE `'{}'` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;'
) \
| mysql "$DB"

One-liner for simple copy/paste

DB="dbname"; ( echo 'ALTER DATABASE `'"$DB"'` CHARACTER SET utf8 COLLATE utf8_general_ci;'; mysql "$DB" -e "SHOW FULL TABLES WHERE Table_Type = 'BASE TABLE'" --batch --skip-column-names | awk '{print $1 }' | xargs -I{} echo 'ALTER TABLE `'{}'` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;' ) | mysql "$DB"
-1

To change the character set encoding to UTF-8 follow simple steps in PHPMyAdmin

  1. Select your Database SS

  2. Go To Operations SS

  3. In operations tab, on the bottom collation drop down menu, select you desire encoding i.e(utf8_general_ci), and also check the checkbox (1)change all table collations, (2) Change all tables columns collations. and hit Go.

SS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.