Configuring MySQL to use UTF-8
A project I’m working on at the moment is going to have multiple language options available, not all of which use the same alphabet (e.g. Russian and Chinese).
To lessen the pain commonly associated with internationalisation on the web, it’s beneficial to use the UTF-8 character set. This short summary from the may help explain better;
Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language.
…
Unicode enables a single software product or a single website to be targeted across multiple platforms, languages and countries without re-engineering. It allows data to be transported through many different systems without corruption.
Thankfully MySQL has supported Unicode for quite some time now, even if it’s not configured to use it by default.
First, let’s check what our settings are at the moment;
mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.01 sec)
mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
That’s to be expected, but it’s not really what we wanted.
Find your MySQL configuration file (on most Linux/BSD systems it’s /etc/my.cnf) and make sure it’s got the following statements under the relevant headers.
[mysqld]
default-character-set=utf8
default-collation=utf8_general_ci
character-set-server=utf8
collation-server=utf8_general_ci
init-connect='SET NAMES utf8'
[client]
default-character-set=utf8
Restart MySQL and make sure it’s working;
service mysql restart
mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
Update; pointed out that it’s worth demonstrating setting the charset and collation when creating tables too (good point!).
CREATE TABLE `content` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`language` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-630303/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Nginx Configuring HTTPS serversNginxHTTPServer
- Configuring Harbor with HTTPS AccessHTTP
- configure: error: PCRE2 library and headers are required, or use --with-pcre1 and PCRE >= 8.32 with UTF-8 supportErrorHeaderUI
- Configuring the launch of the remote virtual machine to debugREMMac
- 記住:永遠不要在 MySQL 中使用 UTF-8MySql
- 為什麼不建議在 MySQL 中使用 UTF-8?MySql
- 【轉載】Kano Model — Ways to use it and NOT use it
- nginx useNginx
- unicode vs utf-8Unicode
- utf-8和gbk
- Be better to use NPMNPM
- WireGuard Use Notes
- plsql use skillsSQL
- 如何在MySQL資料庫中使用use來切換資料庫?MySql資料庫
- xls 編碼 utf-8
- springboot設定UTF-8Spring Boot
- Use PHP7PHP
- efcore This MySqlConnection is already in useMySql
- the NTP socket is in use, exiting
- [Typescript] Use Bitwise FlagsTypeScript
- [Vue] Use render functionVueFunction
- Use IE userdata behavior
- use "jsdelivr" to host fileJSVR
- IPFS_basic_use
- utf-8的中文漢字
- leetcode393. UTF-8 ValidationLeetCode
- Maven+spring 報錯:Error configuring application listener of class org.springframework.web.contMavenSpringErrorAPPFrameworkWeb
- 嚴格模式use strict模式
- AppBuilder(一)【Use彙總】APPUI
- Linux Use ODBC Connect OracleLinuxOracle
- Use Nid to Change dbname(轉)
- Use-After-Free漏洞
- "getActivePinia()" was called but there was no active Pinia. Are you trying to use a store before calling "app.use(pinia)"?APP
- UTF-8編碼規則(轉)
- unicode和UTF-8的區別Unicode
- What is the difference between Mysql InnoDB B+ tree index and hash index? Why does MongoDB use B-tree?MySqlIndexMongoDB
- Python connect zookeeper use the kazoo modulePython
- How to use Qt Designed Ui fileQTUI
- TabError: inconsistent use of tabs and spaces in indentationError