开发者社区> 问答> 正文

原来是utf8的字符集,如何批量更改mysql表中字段的字符集为utf8mb4?,数据库报错

"<span style=""font-family:宋体, 'Lucida Grande', 'Lucida Sans', Verdana, Arial, sans-serif;font-size:13px;line-height:18.2px;background-color:#FFFFFF;"">在mint18上sudo apt-get install mysql-server  
<span style=""font-family:宋体, 'Lucida Grande', 'Lucida Sans', Verdana, Arial, sans-serif;font-size:13px;line-height:18.2px;background-color:#FFFFFF;"">  
<span style=""font-family:宋体, 'Lucida Grande', 'Lucida Sans', Verdana, Arial, sans-serif;font-size:13px;line-height:18.2px;background-color:#FFFFFF;"">然后默认安装的5.7版本,很多教程说5.7版本会在/root/目录下有个 隐藏文件 .mysql_secret 
<span style=""font-family:宋体, 'Lucida Grande', 'Lucida Sans', Verdana, Arial, sans-serif;font-size:13px;line-height:18.2px;background-color:#FFFFFF;"">  
<span style=""font-family:宋体, 'Lucida Grande', 'Lucida Sans', Verdana, Arial, sans-serif;font-size:13px;line-height:18.2px;background-color:#FFFFFF;"">然而我切刀root目录 <span style=""font-family:宋体, 'Lucida Grande', 'Lucida Sans', Verdana, Arial, sans-serif;font-size:13px;line-height:18.2px;background-color:#FFFFFF;"">并没有找到,求教初始的root密码从哪里找? 
<span style=""font-family:宋体, 'Lucida Grande', 'Lucida Sans', Verdana, Arial, sans-serif;font-size:13px;line-height:18.2px;background-color:#FFFFFF;"">  
<span style=""font-family:宋体, 'Lucida Grande', 'Lucida Sans', Verdana, Arial, sans-serif;font-size:13px;line-height:18.2px;background-color:#FFFFFF;"">附带:安装过程也没有提示设置root用户密码的地方 ,那是 <span style=""font-family:宋体, 'Lucida Grande', 'Lucida Sans', Verdana, Arial, sans-serif;font-size:13px;line-height:18.2px;background-color:#FFFFFF;"">mysql5.6的安装方式" image.png

展开
收起
python小菜菜 2020-06-01 19:41:24 1263 0
1 条回答
写回答
取消 提交回答
  • "alter table test_str modify column str_desc varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;######

    JadePool轻松修改MySQL数据库的字符集


    import cn.jadepool.sql.Field;
    import cn.jadepool.sql.Jade;
    import cn.jadepool.sql.Table;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    /**
     * 修改MySQL数据库字符集
     *
     * @author 上海皆普信息科技有限公司 胡开明
     */
    public class ChangeCharset {

        //ALTER SCHEMA `jadepool_dictionary` DEFAULT COLLATE utf8mb4_unicode_ci ;
        //ALTER TABLE `jadepool_dictionary`.`dic_emoji` COLLATE = utf8mb4_unicode_ci ;
        //ALTER TABLE `jadepool_dictionary`.`dic_emoji` CHANGE COLUMN `code` `code` VARCHAR(32) CHARACTER SET 'utf8mb4'  ;
        //ALTER TABLE `fwfw_jiben`.`fwfw_dw` CHARACTER SET = utf8mb4 , COLLATE = DEFAULT ,CHANGE COLUMN `jianjie` `jianjie` TEXT CHARACTER SET 'utf8mb4'  ;

        
        public static void main(String[] args) {
            Jade j = new Jade(getMySQLCon());
            String schema = j.getDb().getCatalog();//数据库的名称
            j.execute("ALTER SCHEMA `" + schema + "` DEFAULT COLLATE utf8mb4_unicode_ci");
            String[] tables = j.getDb().getTableNames();
            for (String tableName : tables) {
                j.execute("ALTER TABLE " + tableName + " COLLATE = utf8mb4_unicode_ci ");
                Table table = j.getDb().getTable(tableName);
                String[] fields = table.getFields();
                for (String fieldName : fields) {
                    Field field = j.getDb().getField(tableName, fieldName);
                    String typeName = field.getTypeName();
                    String size = field.getSize();
                    if ("text".equals(typeName)) {
                        j.execute("ALTER TABLE " + tableName + " CHARACTER SET = utf8mb4 , COLLATE = DEFAULT ,CHANGE COLUMN `"+fieldName+"` `"+fieldName+"` TEXT

    CHARACTER SET 'utf8mb4' ");
                    }
                    if ("varchar".equals(typeName)) {
                        j.execute("ALTER TABLE " + tableName + "  CHANGE COLUMN `"+fieldName+"` `"+fieldName+"` VARCHAR("+size+") CHARACTER SET 'utf8mb4'  ");
                    }
                }
            }
            j.commit();
        }

        public static synchronized Connection getMySQLCon() {
            Connection con = null;
            String url = "jdbc:mysql://localhost:3306/db_name_xxxxxx?

    useOldAliasMetadataBehavior=true&zeroDateTimeBehavior=convertToNull";//useUnicode=true&characterEncoding=utf8mb4&
            String userName = "xxxxxx";
            String password = "xxxxxx";
            try {
                Class.forName("com.mysql.jdbc.Driver");//com.mysql.jdbc.Driver
                con = DriverManager.getConnection(url, userName, password);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ChangeCharset.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(ChangeCharset.class.getName()).log(Level.SEVERE, null, ex);
            }
            return con;
        }

    }

     

    " ![image.png](https://ucc.alicdn.com/pic/developer-ecology/041af1e8d46943cb9a51dfb1b8c225a4.png)
    2020-06-01 19:41:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载

相关镜像