首頁 > 軟體

更改MySQL資料庫的編碼為utf8mb4問題

2022-11-09 14:02:40

utf-8編碼可能1個位元組、2個位元組、3個位元組、4個位元組的字元,但是MySQL的utf8編碼只支援最多3位元組的資料,也就是說mysql最開始的時候,對utf-8的支援是不完整的。而emoji表情字元是4個位元組的字元。

如果直接往採用utf-8編碼的資料庫中插入表情資料

java程式中將報SQL異常:

java.sql.SQLException: Incorrect string value: ‘xF0x9Fx92x94’ for column ‘name’ at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1662)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1581)

可以對4位元組的字元進行編碼儲存,然後取出來的時候,再進行解碼。但是這樣做會使得任何使用該字元的地方都要進行編碼與解碼。

為了支援完整的utf-8編碼,mysql推出了utf8mb4,它是mysql平臺上utf8編碼的超集,相容utf8,並且能儲存4位元組的表情字元。

採用utf8mb4編碼的好處是:儲存與獲取資料的時候,不用再考慮表情字元的編碼與解碼問題。

更改資料庫的編碼為utf8mb4:

1. MySQL的版本

utf8mb4的最低mysql版本支援版本為5.5.3+,若不是,請升級到較新版本。

2. MySQL驅動

5.1.34可用,最低不能低於5.1.13

3. 修改MySQL組態檔

修改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'

4. 重啟資料庫,檢查變數

SHOW VARIABLES WHERE Variable_name LIKE  'character_set_%'  OR Variable_name LIKE 'collation%';
Variable_nameValue
character_set_clientutf8mb4
character_set_connectionutf8mb4
character_set_databaseutf8mb4
character_set_filesystembinary
character_set_resultsutf8mb4
character_set_serverutf8mb4
character_set_systemutf8
collation_connectionutf8mb4_unicode_ci
collation_databaseutf8mb4_unicode_ci
collation_serverutf8mb4_unicode_ci

collation_connection 、collation_database 、collation_server是什麼沒關係。

但必須保證下面幾個變數必須是utf8mb4。

系統變數描述
character_set_client(使用者端來源資料使用的字元集)
character_set_connection(連線層字元集)
character_set_database(當前選中資料庫的預設字元集)
character_set_results(查詢結果字元集)
character_set_server(預設的內部操作字元集)

5. 資料庫連線的設定

資料庫連線引數中:

characterEncoding=utf8會被自動識別為utf8mb4,也可以不加這個引數,會自動檢測。

而autoReconnect=true是必須加上的。

6. 將資料庫和已經建好的表也轉換成utf8mb4

更改資料庫編碼:

ALTER DATABASE caitu99 CHARACTER SET `utf8mb4` COLLATE `utf8mb4_general_ci`;

更改表編碼:

ALTER TABLE `TABLE_NAME` CONVERT TO CHARACTER SET `utf8mb4` COLLATE `utf8mb4_general_ci`;

如有必要,還可以更改列的編碼

可以寫程式碼批次進行轉換:

//獲取所有表
Connection conn = null;

try {
    conn = DbUtilsTool.openConn("MySQL", "127.0.0.1", "3306", "caitu99", "root", "root");
    String sql = "show tables";
    QueryRunner qr = new QueryRunner();
    List<String> tblNameList= (List<String>) qr.query(conn, sql, new ColumnListHandler(1));
    sql = "ALTER DATABASE caitu99 CHARACTER SET `utf8mb4` COLLATE `utf8mb4_general_ci`";
    qr.update(conn,sql);
    for (String str:tblNameList)
    {
        sql = "ALTER TABLE "+str+" CONVERT TO CHARACTER SET `utf8mb4` COLLATE `utf8mb4_general_ci`";
        qr.update(conn,sql);
    }
}
catch (Exception e)
{
            e.printStackTrace();
            throw new RuntimeException(e);
}
finally {
    if(conn!=null) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
   }
}

DbUtilsTool類:

package com.mysql.chartest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.KeyedHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

public class DbUtilsTool {
    private static final QueryRunner runner = new QueryRunner();

    /* 開啟資料庫連線(type: MySQL,Oracle,SQLServer) */
    public static Connection openConn(String type,      //資料庫型別
                                      String host,      //主機ip
                                      String port,      //主機埠
                                      String name,      //資料庫名
                                      String username,  //使用者名稱
                                      String password)//密碼
     {
        Connection conn = null;
        try {
            String driver;
            String url;
            if (type.equalsIgnoreCase("MySQL")) {
                driver = "com.mysql.jdbc.Driver";
                url = "jdbc:mysql://" + host + ":" + port + "/" + name;
            } else if (type.equalsIgnoreCase("Oracle")) {
                driver = "oracle.jdbc.driver.OracleDriver";
                url = "jdbc:oracle:thin:@" + host + ":" + port + ":" + name;
            } else if (type.equalsIgnoreCase("SQLServer")) {
                driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
                url = "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + name;
            } else {
                throw new RuntimeException();
            }
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    /* 關閉資料庫連線 */
    public static void closeConn(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* 查詢(返回Array結果) */
    public static Object[] queryArray(Connection conn, String sql, Object... params) {
        Object[] result = null;
        try {
            result = runner.query(conn, sql, new ArrayHandler(), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /* 查詢(返回ArrayList結果) */
    public static List<Object[]> queryArrayList(Connection conn, String sql, Object... params) {
        List<Object[]> result = null;
        try {
            result = runner.query(conn, sql, new ArrayListHandler(), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /* 查詢(返回Map結果) */
    public static Map<String, Object> queryMap(Connection conn, String sql, Object... params) {
        Map<String, Object> result = null;
        try {
            result = runner.query(conn, sql, new MapHandler(), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /* 查詢(返回MapList結果) */
    public static List<Map<String, Object>> queryMapList(Connection conn, String sql, Object... params) {
        List<Map<String, Object>> result = null;
        try {
            result = runner.query(conn, sql, new MapListHandler(), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /* 查詢(返回Bean結果) */
    public static <T> T queryBean(Class<T> cls, Map<String, String> map, Connection conn, String sql, Object... params) {
        T result = null;
        try {
            if (map != null) {
                result = runner.query(conn, sql, new BeanHandler<T>(cls, new BasicRowProcessor(new BeanProcessor(map))), params);
            } else {
                result = runner.query(conn, sql, new BeanHandler<T>(cls), params);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /* 查詢(返回BeanList結果) */
    public static <T> List<T> queryBeanList(Class<T> cls, Map<String, String> map, Connection conn, String sql, Object... params) {
        List<T> result = null;
        try {
            if (map != null) {
                result = runner.query(conn, sql, new BeanListHandler<T>(cls, new BasicRowProcessor(new BeanProcessor(map))), params);
            } else {
                result = runner.query(conn, sql, new BeanListHandler<T>(cls), params);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /* 查詢指定列名的值(單條資料) */
    public static <T> T queryColumn(String column, Connection conn, String sql, Object... params) {
        T result = null;
        try {
            result = runner.query(conn, sql, new ScalarHandler<T>(column), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /* 查詢指定列名的值(多條資料) */
    public static <T> List<T> queryColumnList(String column, Connection conn, String sql, Object... params) {
        List<T> result = null;
        try {
            result = runner.query(conn, sql, new ColumnListHandler<T>(column), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /* 查詢指定列名對應的記錄對映 */
    public static <T> Map<T, Map<String, Object>> queryKeyMap(String column, Connection conn, String sql, Object... params) {
        Map<T, Map<String, Object>> result = null;
        try {
            result = runner.query(conn, sql, new KeyedHandler<T>(column), params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /* 更新(包括UPDATE、INSERT、DELETE,返回受影響的行數) */
    public static int update(Connection conn, String sql, Object... params) {
        int result = 0;
        try {
            result = runner.update(conn, sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com