utf8mb4和utf8到底有什么区别呢?原来以往的mysql的utf8一个字符最多3字节,而utf8mb4则扩展到一个字符最多能有4字节,所以能支持更多的字符集。
将Mysql的编码从utf8转换成utf8mb4。
需要 >= MySQL 5.5.3版本、从库也必须是5.5的了、低版本不支持这个字符集、复制报错
停止MySQL Server服务
修改 my.cnf或者mysql.ini
?1234567891011 [client] default-character-set = utf8mb4 [mysql] default-character-set = utf8mb4 [mysqld] character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4'
重启 MySQL Server、检查字符集。
查看服务器字符集设置
?1 mysql>SHOW VARIABLES WHERE Variable_name LIKE 'character%' OR Variable_name LIKE 'collation%'
?1234567891011121314 +--------------------------+--------------------+ | Variable_name | Value | +--------------------------+--------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8| | collation_connection | utf8mb4_unicode_ci | | collation_database| utf8mb4_unicode_ci | | collation_server | utf8mb4_unicode_ci | +--------------------------+--------------------+
查看数据库字符集
?1 mysql>select * from SCHEMATA where SCHEMA_NAME='ttlsa'
?12345 +--------------+-------------+----------------------------+------------------------+----------+ | CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH | +--------------+-------------+----------------------------+------------------------+----------+ | def | ttlsa | utf8mb4 | utf8mb4_unicode_ci | NULL | +--------------+-------------+----------------------------+------------------------+----------+
查看表字符集
?1 mysql>select TABLE_SCHEMA,TABLE_NAME,TABLE_COLLATION from information_schema.TABLES
?123 +--------------------+----------------------------------------------------+--------------------+ | TABLE_SCHEMA| TABLE_NAME | TABLE_COLLATION | +--------------------+----------------------------------------------------+--------------------+
查看列字符集
?1 mysql>select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLLATION_NAME from COLUMNS
?123 +--------------------+----------------------------------------------------+--------------------------------------------+--------------------+ | TABLE_SCHEMA| TABLE_NAME | COLUMN_NAME| COLLATION_NAME | +--------------------+----------------------------------------------------+--------------------------------------------+--------------------+
转换字符集语句
?123456789 use information_schemaSELECT concat("ALTER DATABASE `",table_schema,"` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci") as _sql FROM `TABLES` where table_schema like "DB_NAME" group by table_schemaSELECT concat("ALTER TABLE `",table_schema,"`.`",table_name,"` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci") as _sql FROM `TABLES` where table_schema like "DB_NAME" group by table_schema, table_nameSELECT concat("ALTER TABLE `",table_schema,"`.`",table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type,"(",character_maximum_length,") CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci") as _sql FROM `COLUMNS` where table_schema like "DB_NAME" and data_type in ('varchar')SELECT concat("ALTER TABLE `",table_schema,"`.`",table_name, "` CHANGE `",column_name,"` `",column_name,"` ",data_type," CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci") as _sql FROM `COLUMNS` where table_schema like "DB_NAME" and data_type in ('text','tinytext','mediumtext'
从mysql
5.5.3
开始,mysql
支持一种
utf8mb4
的字符集,这个字符集能够支持
4
字节的
utf8
编码的字符。
utf8mb4
字符集能够完美地向下兼容
utf8
字符串。在数据存储方面,当一个普通中文字符存入数据库时仍然占用
3
个字节,在存入一个
unified
emoji
表情的时候,它会自动占用
4
个字节。所以在输入输出时都不会存在乱码的问题了。由于
utf8mb4
是
utf8
的超集,从
utf8
升级到
utf8mb4
不会有任何问题,直接升级即可;如果从别的字符集如
gb2312
或者
gbk
转化而来,一定要先备份数据库。然后,修改
mysql
的配置文件
/etc/my.cnf,修改连接默认字符集为
utf8mb4
,然后在连接数据库以后首先执行一句
sql:
set
names
utf8mb4。
1.
修改my.cnf
或
my.ini
[mysqld]
character-set-server=utf8mb4
[mysql]
default-character-set=utf8mb4
修改后重启mysql
登陆mysql,
show
variables
like
'character%'
可以查看编码是否已经修改成功。
2、修改数据库和数据表为相应的
utf8mb4
格式。
alter
table
table_name
convert
to
character
set
utf8mb4
collate
utf8mb4_bin
3、在代码中,连接数据库成功后,执行
"set
names
utf8mb4"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)