Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set character_set_database and collation_database to utf8 in my.ini?

I've googled a lot about this problem. To sum up, this is what my my.ini looks like:

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

[client]
database = abcdef
user = root
password = XXXXXX
default-character-set = utf8

[mysql]
default-character-set=utf8

[mysqld]
character_set_server=utf8
max_connections = 200
init_connect='SET collation_connection = utf8_general_ci' 
init_connect='SET NAMES utf8' 

When I get into mysql via cmd and issue: show variables like "%character%";show variables like "%collation%";, this is what I got:

+--------------------------+---------------------------------+
| Variable_name            | Value                           |
+--------------------------+---------------------------------+
| character_set_client     | utf8                            |
| character_set_connection | utf8                            |
| character_set_database   | latin1                          |
| character_set_filesystem | binary                          |
| character_set_results    | utf8                            |
| character_set_server     | utf8                            |
| character_set_system     | utf8                            |
| character_sets_dir       | D:\env\MySQL5.6\share\charsets\ |
+--------------------------+---------------------------------+
8 rows in set (0.00 sec)

+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | utf8_unicode_ci   |
+----------------------+-------------------+

I've already restart the MySQL service, so could anyone give me some idea on how to change character_set_database as well as collation_database to utf8? Thanks a lot.

like image 599
Judking Avatar asked Mar 22 '14 02:03

Judking


People also ask

How do I change utf8mb4 to UTF-8?

To solve the problem open the exported SQL file, search and replace the utf8mb4 with utf8 , after that search and replace the utf8mb4_unicode_520_ci with utf8_general_ci . Save the file and import it into your database. After that, change the wp-config. php charset option to utf8 , and the magic starts.

How do I set MySQL database to UTF-8?

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: Copy ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci; To exit the mysql program, type \q at the mysql> prompt.

How do I change the default character set in MySQL?

The MySQL server has a compiled-in default character set and collation. To change these defaults, use the --character-set-server and --collation-server options when you start the server.

What is the difference between UTF-8 and utf8mb4?

The difference between utf8 and utf8mb4 is that the former can only store 3 byte characters, while the latter can store 4 byte characters. In Unicode terms, utf8 can only store characters in the Basic Multilingual Plane, while utf8mb4 can store any Unicode character.


2 Answers

This actually isn't a setting in the my.cnf (or my.ini in this case). mySQL gets this setting from the database's own collation (when it was created). Inorder to get this inline with the utf8 encoding you want, do this:

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;

then do a restart on mysql (cant remember if its needed though), followed by a:

SHOW VARIABLES;

All should be well, Hope that helps!


side note: i think default-character-set is deprecated now-a-days (mySQL 5.5+) and seems to make the config file fidgety.

like image 147
Chris J Avatar answered Oct 24 '22 21:10

Chris J


I do a summary:

determine which charset/collations are available

SHOW CHARSET;
SHOW COLLATION;

check charset

SHOW VARIABLES LIKE '%character%';
SHOW VARIABLES LIKE '%collation%';

set charset (in configure file -> my.cnf)

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

check database/table charset

SHOW CREATE DATABASE databasename;
SHOW CREATE TABLE tablename;

change the database/table charset

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

set when create database/table:

CREATE DATABASE new_db CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;
CREATE TABLE new_table (id INT) CHARSET utf8mb4 COLLATE utf8mb4_general_ci;

Note: I heard that in Mysql utf8 is not the true utf8,utf8mb4 is the real utf8. so, if you have special character that can't save to mysql, maybe you should use utf8mb4 and utf8mb4_general_ci

like image 27
tinyhare Avatar answered Oct 24 '22 23:10

tinyhare