Mysql服务器无法存emoji表情的解决方案
2017-10-20 00:00
399 查看
使用Mysql服务器的utf8字符编码,在存入emoji表情时会报异常:
原因:
utf8是三字节,utf8mb4是4字节而emoji表情也是4字节。
解决方法:
修改mysql配置文件
my.cnf一般在
重启 MySQL Server、检查字符集
1.)重启命令参考:/etc/init.d/mysql restart
2.)输入命令:mysql,进入mysql命令行(如果提示没权限,可以尝试输入mysql -uroot -p你的密码)
3.)在mysql命令行中输入:
修改服务端的db配置文件
大于5.1.13的版本去掉characterEncoding=utf8就ok,不去掉都是这种错:
附上批量转数据库表编码的代码:
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1
原因:
utf8是三字节,utf8mb4是4字节而emoji表情也是4字节。
解决方法:
修改mysql配置文件
my.cnf(windows为my.ini)
my.cnf一般在
etc/mysql/my.cnf位置。找到后请在以下三部分里添加如下内容:
[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.)重启命令参考:/etc/init.d/mysql restart
2.)输入命令:mysql,进入mysql命令行(如果提示没权限,可以尝试输入mysql -uroot -p你的密码)
3.)在mysql命令行中输入:
SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';
修改服务端的db配置文件
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test?useUnicode=true&autoReconnect=true&rewriteBatchedStatements=TRUE username=root password=root
大于5.1.13的版本去掉characterEncoding=utf8就ok,不去掉都是这种错:
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94'
附上批量转数据库表编码的代码:
@Test public void testC() { // 获取所有表 Connection conn = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/happy_help?useUnicode=true&autoReconnect=true&rewriteBatchedStatements=TRUE", "root", "root"); ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), "root", null, new String[]{"TABLE"}); while(rs.next()) { String tableName = rs.getString("TABLE_NAME"); String sql = "ALTER TABLE "+tableName+" CONVERT TO CHARACTER SET `utf8mb4` COLLATE `utf8mb4_general_ci`"; PreparedStatement ps = conn.prepareStatement(sql); ps.executeUpdate(); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
相关文章推荐
- Mysql服务器无法存emoji表情的解决方案
- xampp下无法启动MYSQL服务器解决方案
- 远程mysql服务器无法连接解决方案
- linux主机无法连接Windows上的MySql服务器解决方案
- MySQL无法存储emoji表情解决方案分析
- 无法连接MYSQL服务器解决方案一例
- Android无法访问本地服务器(localhost)的解决方案
- IIS6.0服务器架站无法访问解决方案总结
- 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理解决方案 服务器配置错误
- NUMA导致的MySQL服务器SWAP问题分析与解决方案
- IIS6.0服务器架站无法访问解决方案总结
- mysql_connect() [function.mysql-connect]: [2002] 由于目标计算机积极拒绝,无法连接。解决方案
- phpmyadmin #2003 无法登录 MySQL服务器的解决方法
- 解决远程无法登陆mysql服务器的问题和重置密码
- phpmyadmin #1045 #2002 无法登录 MySQL 服务器的解决方法
- MySql无法远程登录到服务器如何解决
- 十七、Android无法访问本地服务器(localhost)的解决方案
- 【phpmyadmin】错误:#1045 无法登录 MySQL 服务器
- 解决 phpmyadmin #2002 无法登录 MySQL 服务器
- IIS6.0服务器架站无法访问解决方案总结