1. 程式人生 > >mysql5.6修改字符編碼,ERR:Illegal mix of collations for operation 'concat'

mysql5.6修改字符編碼,ERR:Illegal mix of collations for operation 'concat'

查看 AD lob 步驟 creat 處理 col min rep

mysql5.6修改字符編碼,ERR:Illegal mix of collations for operation ‘concat‘

1.問題起因:搭建環境初始化mysql的時候看到mysql配置文件[cloent]下有設置編碼為default-character-set = utf8,大意誤以為是全局配置了,其實還需要在[mysqld]下添加character_set_server = utf8參數的

2.嘗試在線處理:

  mysql> show variables like ‘collation_%‘;

  mysql> show variables like ‘character_set_%‘;

  看到編碼為:latin1

  便嘗試使用set命令修改:set GLOBAL Variable_name=utf8_general_ci; set GLOBAL Variable_name=utf8;

  結果在執行存儲過程中:call xx_ya_stats_xx_daily(2018-05-18,3); 仍然報錯:Illegal mix of collations for operation ‘concat‘

  最後再查看數據庫的編碼,自動恢復為latin1

3.個人結論:mysql在初始化時使用latin1字符編碼,導致了裏面的庫表數據都是latin1字符編碼,雖然改了全局的編碼,但庫表數據編碼還是沒有改到,導致失敗。

4.最終解決辦法:把數據庫備份導出,重新初始化mysql實例,再導入數據,恢復正常。

5.步驟:

  導出:mysqldump -uroot -h‘127.0.0.1‘ -P3306 -pxxx-E -R db_name > db_name.sql

  修改sql:cat db_name.sql | grep -v "ALTER DATABASE \`db_name\` CHARACTER SET latin1 COLLATE latin1_swedish_ci ;" > db_name_new.sql

  初始化同樣的新實例:mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=xxxxx

  設置密碼:mysql> grant all privileges on *.* to ‘root‘@‘%‘ IDENTIFIED BY ‘xxxx‘;

       mysql> flush privileges;

  修改密碼:mysqladmin -h127.0.0.1 -uroot -p --port=3336 password "new_passwd"

  開啟事件:SET GLOBAL event_scheduler = ON;
  查看事件是否開啟:mysql> show variables like ‘event_scheduler‘;

  創建數據庫並指定字符編碼:mysql> CREATE database db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

  導入數據庫:mysql -uroot -h‘127.0.0.1‘ -P3306 -pxxxx db_name < db_name_new.sql

  測試:call xx_ya_stats_xxxx_daily(2018-05-18,3);

  查看編碼:

    mysql> show variables like ‘collation_%‘;

    mysql> show variables like ‘character_set_%‘;

mysql5.6修改字符編碼,ERR:Illegal mix of collations for operation 'concat'