一.查询字符集
1.查询Mysql数据库系统字符集
show variables like '%character%';
MariaDB [information_schema]> show variables like '%character%';
+--------------------------+----------------------------+
| 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 | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
2.查询Mysql支持的字符集
show charset;
MariaDB [information_schema]> show charset;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
+----------+-----------------------------+---------------------+--------+
3.查询数据库字符集
show create database zabbix;
MariaDB [(none)]> show create database zabbix;
+----------+-------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------+
| zabbix | CREATE DATABASE `zabbix` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
4.查询表字符集
select TABLE_NAME,TABLE_TYPE,TABLE_COLLATION from information_schema.tables where TABLE_SCHEMA='zabbix';
MariaDB [(none)]> select TABLE_NAME,TABLE_TYPE,TABLE_COLLATION from information_schema.tables where TABLE_SCHEMA='zabbix';
+----------------------------+------------+-------------------+
| TABLE_NAME | TABLE_TYPE | TABLE_COLLATION |
+----------------------------+------------+-------------------+
| acknowledges | BASE TABLE | latin1_swedish_ci |
| actions | BASE TABLE | latin1_swedish_ci |
| alerts | BASE TABLE | latin1_swedish_ci |
| application_discovery | BASE TABLE | latin1_swedish_ci |
| application_prototype | BASE TABLE | latin1_swedish_ci |
| application_template | BASE TABLE | latin1_swedish_ci |
| applications | BASE TABLE | latin1_swedish_ci |
| auditlog | BASE TABLE | latin1_swedish_ci |
| auditlog_details | BASE TABLE | latin1_swedish_ci |
.......
5.查询列字符集
select COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,CHARACTER_SET_NAME,COLLATION_NAME from information_schema.columns where TABLE_SCHEMA='zabbix' and CHARACTER_SET_NAME='latin1';
MariaDB [(none)]> select COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,CHARACTER_SET_NAME,COLLATION_NAME from information_schema.columns where TABLE_SCHEMA='zabbix' and CHARACTER_SET_NAME='latin1';
+------------------------+-----------+--------------------------+--------------------+-------------------+
| COLUMN_NAME | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | CHARACTER_SET_NAME | COLLATION_NAME |
+------------------------+-----------+--------------------------+--------------------+-------------------+
| message | varchar | 255 | latin1 | latin1_swedish_ci |
| name | varchar | 255 | latin1 | latin1_swedish_ci |
| esc_period | varchar | 255 | latin1 | latin1_swedish_ci |
| def_shortdata | varchar | 255 | latin1 | latin1_swedish_ci |
......
二.修改字符集
1.修改数据库字符集
在 /etc/my.ini中加入character-set-server = utf8 ,重启mysql
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server = utf8
2.修改database字符集
ALTER DATABASE zabbix CHARACTER SET=utf8 COLLATE=utf8_general_ci;
MariaDB [(none)]> show create database zabbix;
+----------+-------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------+
| zabbix | CREATE DATABASE `zabbix` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> ALTER DATABASE zabbix CHARACTER SET=utf8 COLLATE=utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show create database zabbix;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| zabbix | CREATE DATABASE `zabbix` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.01 sec)
3.修改表的字符集
alter table zabbix.users character set = utf8;
MariaDB [(none)]> show create table zabbix.users;
| Table | Create Table
| users | CREATE TABLE `users` (
`userid` bigint(20) unsigned NOT NULL
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
MariaDB [(none)]> alter table zabbix.users character set = utf8;
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
MariaDB [(none)]> show create table zabbix.users;
| Table | Create Table
| users | CREATE TABLE `users` (
`userid` bigint(20) unsigned NOT NULL
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
1 row in set (0.00 sec)
批量修改表字符集,执行以下语句后,会将表以及表中所有列的字符集均修改为utf8
select concat('alter table ',table_name,' convert to character set utf8;') from information_schema.tables where table_schema='zabbix';
批量生成修改语句
MariaDB [zabbix]> select concat('alter table ',table_name,' convert to character set utf8;') from information_schema.tables where table_schema='zabbix';
+------------------------------------------------------------------------+
| concat('alter table ',table_name,' convert to character set utf8;') |
+------------------------------------------------------------------------+
| alter table acknowledges convert to character set utf8; |
| alter table actions convert to character set utf8; |
| alter table alerts convert to character set utf8; |
| alter table application_discovery convert to character set utf8; |
| alter table application_prototype convert to character set utf8; |
......
4.修改列字符集